• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 18815
  • Last Modified:

Hibernate - Adding "distinct" in hql query


this is my query :

String query =
      "from MyBank b where "
            + "b.institution LIKE UPPER('"
            + startsWith + "%')";

I want to add a distinct on the institution field.
Thank you for any help.
2 Solutions
Use Criteria, Projections
Look at this question (Hibernate does not return distinct results for a
query with outer join fetching enabled for a collection (even if I use the
distinct keyword)?) here
List result = session.createQuery("select o from Order o left join fetch

// Yes, really!

Bart CremersJava ArchitectCommented:
You can not put a distinct in the clause.

You can select distinct rows though:

select distinct b.institution from MyBank b where b.institution LIKE UPPER(:startsWith)

This gives all institutions attached to a MyBank matching the startsWith.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Tackle projects and never again get stuck behind a technical roadblock.
Join Now