views:

94

answers:

2

Hi,

How to get max value record from table in hibernate, advance in thanks

+2  A: 

You could use a projection:

Criteria criteria = session
    .createCriteria(Person.class)
    .setProjection(Projections.max("age"));
Integer maxAge = (Integer)criteria.uniqueResult();
Darin Dimitrov
Criteria criteria = getSession() .createCriteria(Borrower.class) .setProjection(Projections.max("brwrid")); Long maxBrwrid = (Long)criteria.uniqueResult();
Ganesamoorthy
its working fine. but null value returns, if null returns set value is 1 else max+1, how to do it.
Ganesamoorthy
`if (maxBrwrid == null) { maxBrwrid = 1; } else { maxBrwrid = maxBrwrid + 1; }`
Darin Dimitrov
+3  A: 

Use the max(...) aggregate function:

select max(cat.weight) from Cat cat

Reference

Pascal Thivent