New to Hibernate.
I have User Group many to many relation. Three tables : User , Group and UserGroup mapping table.
Entities:
@Entity
@Table(name = "user")
public class User {
@Id
@Column (name = "username")
private String userName;
@Column (name = "password", nullable = false)
private String password;
@ManyToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
@JoinTable(name="usergroup", 
            joinColumns={@JoinColumn(name="username")}, 
            inverseJoinColumns={@JoinColumn(name="groupname")})
private Set<Group> userGroups = new HashSet<Group>();
... setter and getters
@Entity
@Table(name = "group")
public class Group {
@Id
@Column(name = "groupname")
private String groupName;
@Column(name = "admin", nullable = false)
private String admin;
@ManyToMany(mappedBy = "userGroups", fetch = FetchType.EAGER)
private Set<User> users = new HashSet<User>();
... setter and getters
Notice that in Group Entity I'm using fetch method EAGER. Now, when I'm calling my DAO to retrive all the groups in the system using the following criteria:
  Criteria criteria = session.createCriteria(Group.class);
  return criteria.list();
I'm getting all the rows from the mappgin table (usergroup) instead of getting the actual number of groups...
for example if i have in user table
 username password
 -----------------
 user1     user1
 user2     user2
in group table
 groupname admin
 ---------------
 grp1      user1
 grp2      user2
in usergroup table
 username groupname
 ------------------
 user1     grp1
 user2     grp2
 user1     grp2
 user2     grp1
The result will be the following list - {grp1,grp2,grp2,grp1}
Instead of {grp1,grp2}
If I change in Group Entity the fetch method to LAZY I'm getting the correct results but hibernate throws me LazyException in another place...
Please assist what fetch method should I use and why ?
Thanks!
 
     
     
     
    