Wednesday, August 02, 2006

Hibernate Customized Query To Get Max Values

This is the query to how to get the max string value from a table.

 

Option 1

Use max function inside the DBMS.

 

Double max = (Double) sess.createSQLQuery("select max(cat.weight) as maxWeight  from cats cat where extLicenseID < '5000-0000' ")

        .addScalar("extLicenseID", Hibernate.STRING);

        .uniqueResult();

 

Option 2

Use setMaxResults of the hibernate and addOrder of asc or desc.

 

List cats = sess.createCriteria(Cat.class)

    .add( Property.forName("name").like("F%") )

    .addOrder( Property.forName("name").asc() )

    .addOrder( Property.forName("age").desc() )

    .setMaxResults(50)

    .list();

 

Tony

No comments:

Is High Quality Software Worth the Cost?

I can't agree more with Martin Fowler's post on "Is High Quality Software Worth the Cost?". Here is just a repost. https:/...