Reputation: 625
I have many rows in table with next format:
id name localSize city country
1 Robert 12 NY USA
2 Bill 12 California USA
3 Artem 12 Kiev Ukraine
Is it possible get only one row from table where localSize is unique (first and last)? Using criterion or something from hibernate.
Upvotes: 0
Views: 69
Reputation: 2068
You can do method like following for getting first or last row.
public List<YourClass> getList() {
List<YourClass> list = new ArrayList<YourClass>();
Criteria subCriteria = session.createCriteria(YourClass.class);
subCriteria.setProjection(Projections.groupProperty("localSize"));
subCriteria.setProjection(Projections.min("id"));
List<Integer> mins = subCriteria.list();
Criteria criteria = session.createCriteria(YourClass.class);
criteria.add(Restrictions.in("id", mins));
list = criteria.list();
return list;
}
Upvotes: 1