hibernate @ManyToMany bidirectional eager fetching

I have a question which I think should be pretty common but I can't find an answer.

I have 2 objects: Group and User. My classes look something like this:

class Group
{
  @ManyToMany(fetch = FetchType.EAGER)
  List<User> users;
}

class User
{
  @ManyToMany(fetch = FetchType.EAGER)
  List<Group> groups;
}

Now, when I try to get a User from the database it brings all its groups and all groups bring all its users and so on. Finally, I'm getting a stackoverflow exception.

How can I solve this issue and still have my bidirectional association and the ability to reach the objects in the lists?

7
задан Rubens Mariuzzo 6 February 2015 в 18:38
поделиться