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:

Thumbs Up to GitHub Copilot and JetBrains Resharper

Having used AI tool GitHub Copilot since 08/16/2023, I’ve realized that learning GitHub Copilot is like learning a new framework or library ...