问题
I am writing small app, using Play Framework 2.0 which uses Ebean as ORM. So I need many-to-many relationship between User class and UserGroup class. Here is some 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();
}
Database scheme generator generates good scheme for that code with intermediate table and all work quite ok, till I using many-to-many.
So I am adding group in one request:
user.groups.add(UserGroup.find.byId(groupId));
user.update();
And trying output them to System.out in another:
System.out.println(user.groups);
And this returns:
BeanSet deferred
Quick search show that BeanSet is lazy-loading container from Ebean. But seems like it doesn't work in proper way or I missed something important.
So is there any ideas about what I am doing wrong?
回答1:
You need to save associations manually
user.groups.add(UserGroup.find.byId(groupId));
user.saveManyToManyAssociations("groups");
user.update();
来源:https://stackoverflow.com/questions/11176013/avaje-ebean-manytomany-deferred-beanset