views:

83

answers:

2

I have an entity that has few fields. One of them is city name. Now I want to get list of all distinct cities from that table. How can I archive that. I tried using DISTINCT keyword, but it doesn't work.

I'm using Hibernate as JPA provider but I would like to get it in pure JPA Query.

A: 

use Distinct criteria or go for distinct aggregate function

edit:
added simple snippet from here

String queryString = "select distinct f from Foo f inner join foo.bars as b" +
                " where f.creationDate >= ? and f.creationDate < ? and b.bar = ?";
        return getHibernateTemplate().find(queryString, new Object[] {startDate, endDate, bar});
org.life.java
Is there any way I can archive it in pure JPA way?
Migol
"pure JPA", can you please elaborate it
org.life.java
In QL that will be implementation independent - i.e. not bound to Hibernate.
Migol
edited.........
org.life.java
A: 

Have you tried:

SELECT DISTINCT t.city FROM MyTable t
mtpettyp
Yes, but it threw an exception that there is no [id] so an object cannot be created.
Migol
This sounds like an issue with your entity. Does it have a PK?
mtpettyp