When using joins(outer join fetch or join fetch) in HQL or when using Criteria… you may end up having duplicate objects in your result list. The way is simple to filter the results. Simply use a HashSet or LinkedHashSet or TreeSet

Example:

List list = session.session.getNamedQuery(“My.Named.Query.With.Joins”).list();
// you might have duplicates now
Set set = new HashSet(list);
list.clear();
list.addAll(set);
// no duplicates now

You might use LinkedHashSet or TreeSet if you really care about the order of results, else cheers – BYE.



2 Responses to “How to remove duplicate objects returned in Hibernate HQL Criteria Queries”  

  1. Maybe this can help you:

    query.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)

  2. 2 sanchit6

    Thats certainly a better way. I didnt knew this way. Thanks for sharing across


Leave a Reply