Avaje Ebean. ManyToMany deferred BeanSet

╄→гoц情女王★ 提交于 2019-12-10 16:59:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!