monojohnny
monojohnny

Reputation: 6181

Hibernate: Build a list of Unique Strings - not entities

I am storing and retrieving Java Entities in a DB using Hibernate.

I need to build up (for the purposes of creating web-page 'drop-downs' / 'lookups') a list of unique strings.

NB: I don't really want to retrieve back Entities here as such - I want to run the equivalent of a SQL 'SELECT DISCTINCT(column) FROM table;' and get back a list of strings.

Is there a standard Hibernate Idiom for doing this - or should I use another mechanism ?

Upvotes: 4

Views: 8972

Answers (1)

mino.me
mino.me

Reputation: 305

Hibernate query is supporting that query, you can use either hql or native query to get String.

Query query = session.createQuery("select distinct user.firstname from User as user");

or

Query query = session.createNativeQuery("select distinct user.firstname from User user");

List<String> list = (List<String>) query.list();

Reference: Hibernate Query

Upvotes: 11

Related Questions