我有一张表格如下:
id | loan_profile_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;
我如何在休眠状态下执行此操作?
答案 0 :(得分:0)
您可以使用Hibernate HQL
来完成答案 1 :(得分:0)
来自Hibernate手册:
假设您有一个“Cat”实体将另一个“Cat”实体称为“配偶”,
from Cat as cat inner join cat.mate as mate
它也可以缩写为:
from Cat as cat join cat.mate as mate
答案 2 :(得分: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"));
(请注意,它不会执行内部联接,但从执行计划的角度来看是等效的)