Java 如何在Hibernate中进行内部联接?
我的表格如下: id |贷款概况| id |从|州|到|州|日期 对于特定的贷款配置文件id,将有多个条目 我想使用Hibernate获取一组loan_profile_id在特定时间之前的最新条目。我将在SQL中这样做:Java 如何在Hibernate中进行内部联接?,java,mysql,hibernate,Java,Mysql,Hibernate,我的表格如下: id |贷款概况| id |从|州|到|州|日期 对于特定的贷款配置文件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
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;
在hibernate中我将如何执行此操作 您可以通过使用Hibernate HQL来实现 从Hibernate手册: 假设有一个Cat实体引用另一个Cat实体作为配偶
from Cat as cat inner join cat.mate as mate
也可以缩写为:
from Cat as cat join cat.mate as mate
使用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"));
请注意,它不会进行内部联接,但从执行计划的角度来看,它是等效的…:似乎是完美的重复:使用子查询是否有效?取决于您的表结构、索引和维度,YOU可以运行解释以查看您的查询是否有效意味着当查询数据库时,它将显示为内部联接,是吗?否,它将显示:select*from t1,其中id位于您的内部联接中