views:

7228

answers:

4

Hello there. Does hibernate HQL queries support using select min, max, count and other sql functions?

like

select min(p.age) from person p

Thanks

+1  A: 

Some aggregate functions are supported: look in the manual

Henrik Paul
+3  A: 

Yes, min(), max() and count() are supported in HQL

Galwegian
+1  A: 

thats how I am using max in Hibernate :

public long getNextId(){
long appId;         
try{
            Session session = HibernateUtil.getAdmSessionFactory().getCurrentSession();
            Transaction t = session.beginTransaction();
            String sequel = "Select max(JAdmAppExemptionId) from JAdmAppExemption";
            Query q = session.createQuery(sequel);
            List currentSeq = q.list();
            if(currentSeq == null){
                return appId;
            }else{
            appId = (Long)currentSeq.get(0);
            return appId+1;
            }

        }catch(Exception exc){
            System.out.print("Unable to get latestID");
            exc.printStackTrace();

        }
        return 0;

    }
Saher
+1  A: 

And how can I execute more complex query, for example:

SELECT count(*) AS c, t.status AS s from ProductEntity AS t GROUP BY t.status

The question is how to cast result of execution to Java objects? Can I create simple (Long, Long) object and cast each result to it?

George Mavchun