Avajay Ebe. ManyToMany Delayed BeanSet

I am writing a small application using Play Framework 2.0 that uses Ebean as an ORM. Therefore, I need a many-to-many relationship between the User class and the UserGroup class. Here is the code:

@Entity public class User extends Domain { @Id public Long id; public String name; @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL) public Set<UserGroup> groups = new HashSet(); } @Entity public class UserGroup extends Domain { @Id public Long id; public String name; @ManyToMany(mappedBy="groups") public Set<User> users = new HashSet(); } 

The database schema generator creates a good schema for this code with a staging table, and everything works fine until I use many-to-many.

So, I add the group with one request:

 user.groups.add(UserGroup.find.byId(groupId)); user.update(); 

And try to output them in System.out to another:

 System.out.println(user.groups); 

And this returns:

BeanSet Postponed

A quick search reveals that BeanSet is a lazy container to load from Ebean. But it looks like this is not working properly or I missed something important.

So are there any ideas on what I am doing wrong?

+4
source share
1 answer

You need to save associations manually

 user.groups.add(UserGroup.find.byId(groupId)); user.saveManyToManyAssociations("groups"); user.update(); 
+9
source

All Articles