Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Criteria -- return records where column is distinct

Sample database table:

  1. ID = 1, msgFrom = 'Hello', foobar = 'meh'
  2. ID = 2, msgFrom = 'Goodbye', foobar = 'comments'
  3. ID = 3, msgFrom = 'Hello', foobar = 'response'

Sample desired output (generated by hibernate query):

  1. ID = 1, msgFrom = 'Hello', foobar = 'meh'
  2. ID = 2, msgFrom = 'Goodbye', foobar = 'comments'

In the above example, the third record would be excluded from the results since the msgFrom column is the same. Let's say the Java/Hibernate class is called Message. I would like the results to be returned as a list of Message objects (or Objects that can be cast to Message, anyway). I want to use the Criteria API if possible. I saw this example on SO and it seems similar but I cannot implement it correctly as of yet.

 select e from Message e 
    where e.msgFrom IN (select distinct m.msgFrom 
                          from Message m
                          WHERE m.msgTo = ? 
                          AND m.msgCheck = 0");

The reason I am doing this is to have the filtering of distinct records done on the database, so I am not interested in answers where I have to filter anything on the application server.

edit: Article showing basically what I want to do. http://oscarvalles.wordpress.com/2008/01/28/sql-distinct-on-one-column-only/

like image 937
KyleM Avatar asked Aug 13 '12 23:08

KyleM


People also ask

How to select distinct results from a hibernate result?

Hibernate criteria is pretty easy to select distinct results. If you want single result to be returned in the projected result, you may want to use: If you want the result to include entire Message class with all its property set, you can use Hibernate result Transformer,

What is criteria query in hibernate?

The Criteria API allows you to build up a criteria query object programmatically; the org.hibernate.Criteria interface defines the available methods for one of these objects. The Hibernate Session interface contains several overloaded createCriteria () methods.

How to use distinct fields in Hibernate Query Language?

I have got a answer for Hibernate Query Language to use Distinct fields. You can use SELECT DISTINCT (TO_CITY) FROM FLIGHT_ROUTE. If you use SQL query, it return String List.

How do I sort Hibernate query results?

Hibernate criteria – sort query results Sorting the query’s results works much the same way with criteria as it would with HQL or SQL. The Criteria API provides the org.hibernate.criterion.Order class to sort your result set in either ascending or descending order, according to one of your object’s properties.


1 Answers

Please try this and let me know

DetachedCriteria msgFromCriteria = DetachedCriteria.forClass(Message.class);
ProjectionList properties = Projections.projectionList();
properties.add(Projections.groupProperty("messageFrom"));
properties.add(Projections.min("id"),"id");
msgFromCriteria.setProjection(properties);

Criteria criteria = s.createCriteria(Message.class);
criteria.add(Subqueries.propertiesIn(new String[]{"messageFrom","id"}, 
    msgFromCriteria));
List<Message> list = criteria.list();

for(Message message:list){
    System.out.println(message.getId()
        +"-------"
        +message.getMessageFrom()
        +"-----"
        +message.getFoobar());
}
like image 93
Java P Avatar answered Oct 13 '22 01:10

Java P