HQL: How to select all entities distinct by some column?
You can also use Criteria and Projection together :
Criteria criteria = session.createCriteria( MyEntity.class );
criteria.setProjection( Projections.distinct( Projections.property( "id" ) ) );
Hope it help someone.
Below is the sample query :
select e from Message e
where e.msgFrom IN (select distinct m.msgFrom
from Message m
WHERE m.msgTo = ?
AND m.msgCheck = "0");
Alternatively, you can also use Criteria API.