Get record with max id, using Hibernate Criteria

后端 未结 8 1658
后悔当初
后悔当初 2020-12-15 16:18

Using Hibernate\'s Criteria API, I want to select the record within a table with the maximum value for a given column.

I tried to use Projections, creating an alias

相关标签:
8条回答
  • 2020-12-15 16:57
        Date maxDateFromDB = null;
        Session session = (Session) entityManager.getDelegate();
    //Register is and Entity and assume maxDateFromDB is a column.
    //Status is another entity with Enum Applied.
    //Code is the Parameter for One to One Relation between Register and Profile entity.
        Criteria criteria = session.createCriteria(Register.class).setProjection(Projections.max("maxDateFromDB") )
        .add(Restrictions.eq("status.id", Status.Name.APPLIED.instance().getId()));
        if(code != null && code > 0) {
            criteria.add(Restrictions.eq("profile.id", code));
        }
        List<Date> list = criteria.list();
    
        if(!CollectionUtils.isEmpty(list)){
            maxDateFromDB = list.get(0);
        }
    
    0 讨论(0)
  • 2020-12-15 16:58

    For the max() function in hibernate:

    criteria.setProjection(Projections.max("e.encounterId"));
    
    0 讨论(0)
  • 2020-12-15 16:59

    You can use a DetachedCriteria to express a subquery, something like this:

    DetachedCriteria maxId = DetachedCriteria.forClass(Foo.class)
        .setProjection( Projections.max("id") );
    session.createCriteria(Foo.class)
        .add( Property.forName("id").eq(maxId) )
        .list();
    

    References

    • Hibernate Core Reference Guide
      • 15.8. Detached queries and subqueries
    0 讨论(0)
  • 2020-12-15 17:05

    I found that using addOrder and setMaxResults together worked for me.

    Criteria c = session.createCriteria(Thingy.class);
    c.addOrder(Order.desc("id"));
    c.setMaxResults(1);
    return (Thingy)c.uniqueResult();
    

    Using the MySQL dialect, this generates a SQL prepared statement about like this (snipping out some of the fields):

    select this_.id ... from Thingy this_ order by this_.id desc limit ?
    

    I am not sure if this solution would be effective for dialects other than MySQL.

    0 讨论(0)
  • 2020-12-15 17:08

    Use

    addOrder(Order.desc("id"))
    

    and fetch just the first result :)

    0 讨论(0)
  • 2020-12-15 17:13

    HQL:

    from Person where person.id = (select max(id) from Person)
    

    Untested. Your database needs to understand subselects in the where clause.

    Too lazy to find out if/how such a subselect can be expressed with the criteria api. Of course, you could do two queries: First fetch the max id, then the entity with that id.

    0 讨论(0)
提交回复
热议问题