简体   繁体   English

每个组的休眠条件maxResults

[英]Hibernate Criteria maxResults per Group

I have a table with rows internalId, value and date. 我有一个表,其中包含行internalId,值和日期。 There are multiple entries with the same internalId but different date. 有多个条目具有相同的internalId但日期不同。 I'm only interested in the most recent value of each internalId. 我只对每个internalId的最新值感兴趣。 How do I manage it with Criteria? 如何使用“标准”进行管理? This is the my current base code: 这是我当前的基本代码:

Criteria criteria = session.createCriteria(myClass.class); 

ProjectionList projList = Projections.projectionList(); 
projList.add(Projections.property("internalId"));
projList.add(Projections.property("value")); 
projList.add(Projections.property("date")); 

criteria.setProjection(projList);
criteria.add(Restrictions.in("internalId", internalIds));
List list = criteria.list();

From what I understand, what you want is to accomplish i something like this: 据我了解,您想要实现的目标是这样的:

SELECT 
    internalId, 
    value, 
    max(date) as date 
FROM 
    table 
WHERE 
    internalId IN (1, 2, 3, 4 ...) 
GROUP BY 
    internalId

This can be achieved with the Projections max function: 这可以通过Projections max函数来实现:

projList.add(Projections.max("date"));

and the groupProperty function 和groupProperty函数

projList.add(Projections.groupProperty("internalId"))

For reference: http://docs.jboss.org/hibernate/orm/3.5/reference/en/html_single/#querycriteria 供参考: http : //docs.jboss.org/hibernate/orm/3.5/reference/en/html_single/#querycriteria

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM