I've "Student" and "Class" in relation many to many. In between there is a linking table.
If I want to retrieve all the students with HQL in the following way, everything is fine:
Query queryObject = getSession().createQuery("from Student");
return queryObject.list();
With the above code if there are three students I get a list of three students.
If I use criterias, however, it's fine only as long as there are no associations in the linking table.
Criteria crit = getSession().createCriteria(Student.getClass());
return crit.list();
With the second code, I get three results only when the linking table is empty. However when I add an association, I get 6 results. Looking at the log, Hibernate generates multiple selects. How is it possible?
Can someone explain why does this happen? How can I fix the criteria in order to return the three records only once?
EDIT
In the Student class I mapped in this way:
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(
name="room_student_rel"
, joinColumns={
@JoinColumn(name="id_student")
}
, inverseJoinColumns={
@JoinColumn(name="id_room")
}
)
private List<Room> rooms;
In the room (class) I mapped in this way:
@OneToMany(fetch=FetchType.EAGER, mappedBy="room")
@Fetch(FetchMode.SELECT)
private List<RoomStudent> roomStudents;