我的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>();
堆栈追踪:
at System.Data.SqlClient.SqlDataReader.GetOrdinal(String name)
at NHibernate.Driver.NHybridDataReader.GetOrdinal(String name)
at NHibernate.Type.NullableType.NullSafeGet(IDataReader rs, String name)
at NHibernate.Type.NullableType.NullSafeGet(IDataReader rs, String[] names, SessionImplementor session, Object owner)
at NHibernate.Loader.Loader.GetKeyFromResultSet(Int32 i, ILoadable persister, Object id, IDataReader rs, ISessionImplementor session)
at NHibernate.Loader.Loader.GetRowFromResultSet(IDataReader resultSet, ISessionImplementor session, QueryParameters queryParameters, LockMode[] lockModeArray, EntityKey optionalObjectKey, IList hydratedObjects, EntityKey[] keys, Boolean returnProxies)
at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters)
上面的DomainObjects很少(例如Branch,Department,EmpType,Designation和Employee)。但我想返回一个列表类型的Branch对象。
请有人建议我解决方案。
由于 Rusho
答案 0 :(得分:1)
如果你想在本机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
然后您需要告诉查询此实体的名称,如此
CreateSQLQuery(sql).AddEntity("Entity", typeof(Branch))
但是使用这样的原生sql查询并不是一个好主意,因为这里有Nhibernate的美丽,这就是我们应该使用NHibernate,来消除这些庞大的sql语句并用一些OOP风格表达我们的表。所以对于像这样的基本查询,你最好使用HQL查询语言或QueryOver。看看你的sql语句你写了多少行但是用Nhibernate你可以用几句话来做,你只使用本机sql使用NHibernate无法实现的查询语句。