具有NativeSQL的Nhibernate

具有NativeSQL的Nhibernate,nhibernate,Nhibernate,我的sql是: SELECT Branch.BranchName, Department.DepartmentName, Designation.DesignationName, EmpType.EmpType, Shift.ShiftName, Employee.CardNo,Employee.EmployeeStatus, Employee.JoiningDate, Employee.EmpName FROM Branch INNE

我的sql是:

SELECT Branch.BranchName, Department.DepartmentName, 
       Designation.DesignationName,   EmpType.EmpType, Shift.ShiftName,
       Employee.CardNo,Employee.EmployeeStatus, Employee.JoiningDate, Employee.EmpName
FROM   Branch 
          INNER JOIN Department ON Branch.BranchId = Department.BranchId 
          INNER JOIN Designation ON Branch.BranchId = Designation.BranchId 
             AND Department.DepartmentId = Designation.DepartmentId 
          INNER JOIN Employee ON Branch.BranchId = Employee.BranchId 
             AND Department.DepartmentId = Employee.DepartmentId 
             AND Designation.DesignationId = Employee.DesignationId 
          INNER JOIN EmpType ON Employee.EmpTypeId = EmpType.EmpTypeId 
          INNER JOIN Shift ON Employee.ShiftId = Shift.ShiftId
数据访问代码为:

IQuery query = Session.GetISession().CreateSQLQuery(sql).AddEntity(typeof(Branch));
return query.List<Branch>();
上面几乎没有域对象(例如分支机构、部门、空类型、名称和员工)。但是我想返回一个分支对象的列表类型

请任何人告诉我解决办法

谢谢
Rusho

如果您想在本机sql语句上使用
CreateSQLQuery
,您必须告诉Nhibernate您要选择哪个实体,因为Nhibernate在实体或对象方面工作,那么应该这样编写sql语句:

SELECT {Entity.*}, Department.DepartmentName, 
   Designation.DesignationName,   EmpType.EmpType, Shift.ShiftName,
   Employee.CardNo,Employee.EmployeeStatus, Employee.JoiningDate, Employee.EmpName
FROM Branch {Entity}
      INNER JOIN Department ON {Entity.BranchId} = Department.BranchId 
      INNER JOIN Designation ON {Entity.BranchId} = Designation.BranchId 
         AND Department.DepartmentId = Designation.DepartmentId 
      INNER JOIN Employee ON Branch.BranchId = Employee.BranchId 
         AND Department.DepartmentId = Employee.DepartmentId 
         AND Designation.DesignationId = Employee.DesignationId 
      INNER JOIN EmpType ON Employee.EmpTypeId = EmpType.EmpTypeId 
      INNER JOIN Shift ON Employee.ShiftId = Shift.ShiftId
然后您需要像这样告诉query这个实体的名称

CreateSQLQuery(sql).AddEntity("Entity", typeof(Branch))

但是像这样使用原生sql查询不是一个好主意,因为Nhibernate的优点就在这里,这就是我们应该使用Nhibernate的原因,要删除这些庞大的sql语句,并以某种OOP风格表达我们的表。因此,对于这样的基本查询,最好使用查询语言或。看看您的sql语句,您写了多少行,但使用Nhibernate,只需几句话,对于使用NHibernate无法实现的查询,只能使用本机sql语句。

这看起来像一个简单的查询,可以使用NHibernate的条件或HQL(或QueryOver)轻松表示。那么,首先为什么选择本机SQL呢?
CreateSQLQuery(sql).AddEntity("Entity", typeof(Branch))