0

我有一张如下表:

编号 | 贷款资料ID | from_state | to_state | 日期

特定的loan_profile_id 将有多个条目。

我想使用 Hibernate 来获取一组loan_profile_id 的特定时间之前的最新条目。这就是我在 SQL 中的做法:

select lps.* from loan_profile_state_changes lps inner join 
(select max(id) as maxId from loan_profile_state_changes where date < '2012-09-13 00:00:00' and loan_profile_id in (15744,15745,15746,15747,15748,15750,15751) group by loan_profile_id)maxIds
on maxIds.maxId = lps.id order by lps.id desc;

我将如何在休眠状态下执行此操作?

4

3 回答 3

0

您可以使用 Hibernate HQL 来做到这一点

Hibernate 查询语言:子查询

于 2012-09-14T08:16:34.540 回答
0

从休眠手册:

假设您有一个“Cat”实体将另一个“Cat”实体引用为“mate”,

from Cat as cat inner join cat.mate as mate

也可以简写为:

from Cat as cat join cat.mate as mate
于 2012-09-14T08:24:59.787 回答
0

使用 DetachedCriteria :

DetachedCriteria subQuery = DetachedCriteria.forClass(LoanProfileStateChanges.class);
subQuery.add(Restrictions.lt("date", new Date());
subQuery.add(Restrictions.in("id", Arrays.asList(1,2,3));
ProjectionList pl = Projections.projectionList();
pl.add(Projections.max("id")).add(Projections.groupProperty("loanProfileId"));
subQuery.setProjection(pl);

DetachedCriteria dc = DetachedCriteria.forClass(LoanProfileStateChanges.class);
dc.add(Subqueries.propertyIn("id", subQuery);
dc.addOrder(Order.desc("id"));

(请注意,它不会进行内部连接,但从执行计划的角度来看是等效的)

于 2012-09-14T09:29:31.410 回答