Joining on multiple fields in a NHibernate Criteria query

血红的双手。 提交于 2019-12-02 10:09:43

问题


I have a Dept table and a Emp table.

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");

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.

How do I do this using the Criteria API in NHibernate?


回答1:


Criteria criteria = session.createCriteria(Dept.class, "department")
                           .createAlias("empMap", "employee")
                           .add(Restrictions.eqProperty("department.deptName", 
                                                        "employee.empTrainingName"));

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"));

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?



来源:https://stackoverflow.com/questions/10967695/joining-on-multiple-fields-in-a-nhibernate-criteria-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!