[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 18769
  • 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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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