在NHibernate Criteria查询中加入多个字段 [英] Joining on multiple fields in a NHibernate Criteria query
问题描述
我有一个 Dept
表和一个 Emp
表。
我需要以这样一种方式连接这两个表:其中
子句看起来像这样:
I need to join these two table in such a way that the where
clause looks something like this:
where dept.deptId = emp.DeptId and dept.deptName = emp.empTrainingName
我试过这个:
I tried this:
Criteria criteria = session.createCriteria(Dept.class).createAlias("empMap","id");
使用这个,第一个条件即 dept.deptId = emp.DeptId执行
。但我不确定如何比较 dept.deptName
和 emp.empTrainingName
。
Using this, the first where condition i.e. dept.deptId = emp.DeptId
is performed. But I am not sure how to compare dept.deptName
with emp.empTrainingName
.
我如何在NHibernate中使用Criteria API来完成这项工作?
How do I do this using the Criteria API in NHibernate?
推荐答案
Criteria criteria = session.createCriteria(Dept.class, "department")
.createAlias("empMap", "employee")
.add(Restrictions.eqProperty("department.deptName",
"employee.empTrainingName"));
您也可以使用with子句,这在左连接的情况下是必需的:
You may also use a with clause, which would be necessary in case of a left join:
Criteria criteria =
session.createCriteria(Dept.class, "department")
.createAlias("empMap",
"employee",
Criteria.LEFT_JOIN,
Restrictions.eqProperty("department.deptName",
"employee.empTrainingName"));
注意:您的名字选择很糟糕。而不是 Dept.deptId
,为什么不使用 Department.id
?而不是 Emp.empTrainingName
,为什么不选择 Employee.trainingName
?
Side note: your choices of names are awful. Instead of Dept.deptId
, Why not use Department.id
? Instead of Emp.empTrainingName
, why not choose Employee.trainingName
?
这篇关于在NHibernate Criteria查询中加入多个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!