Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avaje Ebean. ManyToMany deferred BeanSet

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?

like image 553
Alex Povar Avatar asked Jan 17 '23 07:01

Alex Povar


1 Answers

You need to save associations manually

user.groups.add(UserGroup.find.byId(groupId));
user.saveManyToManyAssociations("groups");
user.update();
like image 80
biesior Avatar answered Jan 27 '23 04:01

biesior