Search code examples
javahibernatehql

Unique in hql query


I did a hql query and I am getting this:

Persona con id=42: Hannah Abbott
Persona con id=1: Euan Abercrombie
Persona con id=56: Stewart Ackerley
Persona con id=56: Stewart Ackerley
Persona con id=73: Malcolm Baddock
Persona con id=57: Marcus Belby
Persona con id=57: Marcus Belby
Persona con id=57: Marcus Belby

As you can see, I am getting duplicates.

ScrollableResults sc = session.createQuery("from Person as p join p.courses_1 as c order by p.lastName asc ").scroll();

This is the query.

I've tried putting distinct on the hql query but it doesn't work.

Many thanks for the help.


Solution

  • For the case of an inner join, the distinct keyword should work here:

    String hql = "select distinct p from Person as p inner join p.courses_1 as c order by p.lastName";
    ScrollableResults sc = session.createQuery(hql).scroll();