Reputation: 1919
I have a table with 4 columns : id, column_a and column_b
My initial query is basicaly
Session session = initialize my hibernate session;
Criteria criteria = session.createCriteria(MytableBean.class);
// add some constraints
return criteria.list()
And it returns some results like :
id | column_a | column_b |
1 | A | 1 |
2 | A | 1 |
3 | A | 2 |
4 | B | 3 |
5 | B | 3 |
But I don't want to get several times the same couple (column_a, column_b), even if I need to have it many times in my database.
So, I'm looking for a query which returns a result similar to:
id | column_a | column_b |
| A | 1 |
| A | 2 |
| B | 3 |
In sql I would use SELECT DISTINCT column_a, column_b FROM ....
But I can't use sql (and either don't want to use hql, as far as possible).
Upvotes: 3
Views: 6523
Reputation: 3607
The following will accomplish this. A list of Object[] will be returned.
Criteria criteria = session.createCriteria(MytableBean.class);
ProjectionList projList = Projections.projectionList();
projList.add(Projections.property("column_a"));
projList.add(Projections.property("column_b"));
projList.add(Projections.groupProperty("column_a"));
projList.add(Projections.groupProperty("column_b"));
criteria.setProjection(projList);
return criteria.list();
Upvotes: 2