Joining on multiple fields in a NHibernate Criteria query

后端 未结 1 1621
予麋鹿
予麋鹿 2021-01-24 04:20

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:

相关标签:
1条回答
  • 2021-01-24 04:31
    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?

    0 讨论(0)
提交回复
热议问题