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

Hibernate - Adding "distinct" in hql query

Hello,

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.
0
matthew016
Asked:
matthew016
2 Solutions
 
Ajay-SinghCommented:
Use Criteria, Projections
 
 
 <http://www.hibernate.org/hib_docs/v3/api/org/hibernate/Criteria.html>
http://www.hibernate.org/hib_docs/v3/api/org/hibernate/Criteria.html 
 
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
 
http://www.hibernate.org/117.html?cmd=prntdoc 
 
List result = session.createQuery("select o from Order o left join fetch
o.lineItems")

                      .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
// Yes, really!

                      .list();
0
 
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.
0
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