Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 463
monojohnny Avatar asked Sep 06 '25 03:09

monojohnny


1 Answers

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

like image 121
mino.me Avatar answered Sep 07 '25 22:09

mino.me