首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用NativeSQL的Nhibernate

问使用NativeSQL的Nhibernate
EN

Stack Overflow用户
提问于 2011-10-08 12:10:34
回答 1查看 830关注 0票数 0

我的sql是:

代码语言:javascript
复制
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

数据访问代码为:

代码语言:javascript
复制
IQuery query = Session.GetISession().CreateSQLQuery(sql).AddEntity(typeof(Branch));
return query.List<Branch>();

堆栈跟踪:

代码语言:javascript
复制
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很少(例如分支机构、部门、EmpType、指定和员工)。但是我想返回一个Branch对象的列表类型。

请任何人给我建议什么是解决方案。

谢谢Rusho

EN

回答 1

Stack Overflow用户

发布于 2011-10-08 14:00:43

如果你想使用CreateSQLQuery而不是原生sql语句,你必须告诉Nhibernate你想选择哪个实体,因为Nhibernate在实体或对象方面工作,然后应该像这样编写sql语句:

代码语言:javascript
复制
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这个实体的名称

代码语言:javascript
复制
CreateSQLQuery(sql).AddEntity("Entity", typeof(Branch))

但是使用像这样的原生sql查询并不是一个好主意,因为这里有Nhibernate的美,这就是我们应该使用NHibernate的原因,为了消除这些庞大的sql语句,并在一些OOP flavor.So中表达我们的表对于这样的基本查询,你最好在sql语句中使用HQL查询语言或QueryOver.Look,不管你写了多少行,但是使用NHibernate你可以用几个单词就能做到,你只使用原生sql语句来处理你不能用Nhibernate实现的查询。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7694771

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档