[英]Hibernate - Adding properties of another class in Criteria Restrictions
I have a table Loan application whose pojo is 我有一个表贷款申请,其pojo是
class LoanApplication{
int id;
int loanNo;
...
}
I have another pojo 我还有另一本
class LoanFlow{
int id;
int loanId;
date reviewDate;
...
}
Here loanId of loanFlow is the foreign key mapped to the id of LoanApplication. 这里的loanFlow的loanId是映射到LoanApplication的ID的外键。
I have to fetch all the loan applications with the reviewDate. 我必须使用reviewDate获取所有贷款申请。
I am trying to write a criteria like: 我正在尝试写一个类似的标准:
Criteria criteria = getSession().createCriteria(LoanApplication.class);
criteria.add(Restrictions.eq("id", someId));
How can I fetch the reviewDate also from LoanFlow with this criteria. 我如何使用此条件也可以从LoanFlow中获取reviewDate。
Criteria criteria = getSession().createCriteria(LoanApplication.class, "loApp");
criteria.createAlias("loApp.loanFlow", "flow");
criteria.add(Restrictions.eq("flow.id", 1));
You can directlly use HQL also.
您也可以直接使用HQL。 using createQuery()
使用createQuery()
You can do it with subqueries, if there isn't ManyToOne or OneToMany annotations to the relationship between entities: 如果实体之间的关系没有ManyToOne或OneToMany批注,则可以使用子查询来实现:
DetachedCriteria subQuery = DetachedCriteria.forClass(LoanFlow.class, "loanFlow");
/*Here there is a between, but you can change it to your necessity*/
subQuery.add(Restrictions.between("loanFlow.reviewDate", dateBegin, dateEnd));
subQuery.setProjection(Projections.property("loanFlow.loanId"));
Criteria criteria = getSession().createCriteria(LoanApplication.class, "loanApplication");
Subqueries.propertyIn("loanApplication.id", subQuery);
List<LoanApplication> list = criteria.list();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.