Wednesday, March 17, 2010

Hibernate Distinct Rows

In Hibernate when we do left outer join, sometimes you may get us duplicate rows, as Hibernate entities that we are joining, are themselves joined with some other entities.

For example:

We are joining Class A and Class B but class A already has a join with Class C which may give us
duplicate rows. In cases like these Hibernate has DISTINCT_ROOT_ENTITY for rescue, like below.

 @Transactional(readOnly = true)
@SuppressWarnings("unchecked")
public List<Group> getGroups (String id) {
logger.info("Id is " + id);
Session session = sessionFactory.getCurrentSession();
List<Group> groups = session.createCriteria(Group.class)
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
.add(Restrictions.eq("id", id
.list();
return groups;
}
Check out my site seenuonjava.com for more.

1 comment:

Anonymous said...

i without a doubt adore your posting kind, very exciting.
don't give up and keep posting in all honesty , because it simply nicely to follow it.
impatient to browse through a lot more of your current writing, have a good day ;)