by Manfred 09 Jul 01:45
some problems

Hallo,

I have some problems with maps.

the classes:
-----------------------------------------
public class Company {
@Id
Long id;
String name;

@OneToMany( cascade=CascadeType.ALL )
@JoinColumn(name="company_id")
@MapKey(name="key")
Map String,Contact contacts
}

public class Contact {
@Id
Long id;
String name;
String key;
@ManyToOne
Company company;
}

The Code:
----------------------------------
Company company = new Company();
company.setName( "CompanyName1" );
Contact contact = new Contact();
contact.setName( "ContactName1" );

company.contacts.put( "key1", contact )
contact.setCompany( company );
Ebean.save( company )


The first problem:
This seems me not ok in AnnotationAssocManys.java(line 92)

MapKey mapKey = (MapKey) get(prop, MapKey.class);
if (mapKey != null) {
prop.setFetchOrderBy(orderBy.value()); ----
}

i fixed this to
prop.setMapKey(mapKey.name());

but after this is it also not possible
to save a map.


Manfred

09 Jul 07:47
by Rob

RE: AnnotationAssocManys.java(line 92)
You are correct, this is a bug and you have made the correct change.

In regards saving maps... I have just tried it and it worked fine for me. Can you specify what your problem is (an exception or is it just not saving?)

I notice that in your code, you are not creating and setting the actual map company.contacts. That is, I'd expect to see code like...

Map contacts = new LinkedHashMap();
company.setContacts(contacts);

Assuming that you are doing that... then I can't see any problems with the code you have put in here.

09 Jul 07:48
by Rob

The code I just tested was...

User u = (User)Ebean.getReference(User.class, 1);
		
		CommentLink cl = new CommentLink();
		cl.setLinkUrl("http://none");
		
		Comment cm = new Comment();
		cm.setTitle("t2");
		cm.setBody("body2");
		cm.setUser(u);
		

		Map comments = new LinkedHashMap();
		comments.put(cm.getTitle(), cm);

		cl.setComments(comments);

		//List list = new ArrayList();
		//list.add(cm);
		//cl.setComments(list);

		Ebean.save(cl);
09 Jul 07:50
by Rob

And the CommentLink entity bean (parent) was... (I changed comments from a List to a Map, and I made no changes to the Comment entity bean).

@Entity
@Table(name="c_comment_link")
public class CommentLink {


    @Id
    Integer id;

    String linkUrl;

    Timestamp cretime;

    @Version
    Timestamp updtime;

    @OneToMany(cascade=CascadeType.ALL)
    @MapKey(name="title")
    Map comments;
    //List comments;
09 Jul 10:05
by Manfred

Yes i can also save the map.
But i can not see the map-key in the database.
In my case
company.contacts.put( "key1", contact ).
----

Manfred

09 Jul 21:02
by Rob

So you see the row in the DB, but the 'key' column is null and you expect it to have the 'key1' value?

Do you do...

contact.setKey("key1");

before you save?

09 Jul 21:05
by Rob

So, I guess you would be hoping that Ebean takes the key value from the Map and sets it to the bean property before the save. That sounds reasonable... Ebean is not currently doing that (you have to set the property yourself currently).

09 Jul 22:13
by Manfred

Yes i understand now, but for what is the @MapKey Annotation?

10 Jul 01:44
by Rob

It is used to populate the Map via a query.

What you have found may well be a bug though. Certainly the current behaviour is not obvious and requires duplicate effort (setting the key both into the bean and the map).

So right now, I think this is a bug... I should check the specs again to see if there is a mention of this. The only issue I see currently is if the Map key type different (to the property) then an error will occur - but that seems reasonable to me. Hmmm...

10 Jul 20:43
by Rob

Logged as bug http://www.avaje.org/bugdetail-21.html ... and fixed it, so from 0.9.3 the map key will automatically be set to the bean on save cascade.

Note: this only occurs on save cascade, so saving the child beans individually you need to make sure that the mapKey property is set. However, I think that is ok as the child bean may not even be in a map at the time.

10 Jul 23:20
by Manfred

very good.
what is your estimated release time for 0.9.3?

Create a New Topic

Title:
Body:
 
Introduction User Guide (pdf) Install/Configure Public JavaDoc Whitepapers
General Database Specific Byte Code Deployment Annotations Features
Top Bugs Top Enhancements
woResponse