Example database table:
- ID = 1, msgFrom = 'Hello', foobar = 'meh'
- ID = 2, msgFrom = 'Goodbye', foobar = 'comments'
- ID = 3, msgFrom = 'Hello', foobar = 'response'
An example of the desired output (generated by a sleep request):
- ID = 1, msgFrom = 'Hello', foobar = 'meh'
- ID = 2, msgFrom = 'Goodbye', foobar = 'comments'
In the above example, the third record will be excluded from the results, since the msgFrom column is the same. Let them say that 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 passed to Message, anyway). I want to use the criteria API, if possible. I saw this example on SO and it seems similar, but so far I can not implement it correctly.
select e from Message e
where e.msgFrom IN (select distinct m.msgFrom
from Message m
WHERE m.msgTo = ?
AND m.msgCheck = 0");
, , - , , - .
edit: , , . http://oscarvalles.wordpress.com/2008/01/28/sql-distinct-on-one-column-only/