I have got two entity objects Bug and User associated with each other by Many To Many relations
The association is defined in this fashion in both Bug.java and User.java
@ManyToMany(targetEntity = User.class, cascade = { CascadeType.PERSIST,
CascadeType.MERGE })
@JoinTable(name = "BUG_USER", joinColumns = { @JoinColumn(name = "BUG_ID") }, inverseJoinColumns = { @JoinColumn(name = "USER_ID") })
public List<User> getUsers() {
return users;
}
@ManyToMany(cascade = { CascadeType.MERGE, CascadeType.PERSIST }, mappedBy = "users", targetEntity = Bug.class)
public List<Bug> getBugs() {
return bugs;
}
When i tried to fetch the all the Bugs along with its associated result using the following code snippet:
@SuppressWarnings("unchecked")
public List<Bug> findAllBugs() {
Session session = getHibernateTemplate().getSessionFactory()
.getCurrentSession();
String SQL_QUERY = "from Bug bug inner join fetch bug.users";
log.info(SQL_QUERY);
Query query = session.createQuery(SQL_QUERY);
return query.list();
}
it returned duplicate rows.
For example if i assign a bug say A to two users say B and C,
then two rows are returned for the same bug id and same assigned users.
If i assign one bug to three users,then three duplicate rows are returned.
_________________ .::hIBERNATe rOCKs::.
|