我想使用Business object
将SQL查询映射到Nhibernate
。 Employee表中有很多字段,但我只有三个字段,只想要映射那些字段。
这是我的SQL查询
<sql-query name="findEmployeesInfo">
<return alias="emp" class="Calibr.BusinessDocuments.BOs.Employee, BusinessDocuments"/>
<![CDATA[
select (emp.Eid) as {emp.Id},(emp.FirstName) as {emp.FirstName},(emp.LastName) as {emp.LastName} from Employee emp
]]>
</sql-query>
这里是我制作构造函数来映射这些列
public Employee(int Id, string FirstName, string LastName)
{
this.Id = Id;
this.FirstName = FirstName;
this.LastName = LastName;
}
数据库员工表列名: Eid,FirstName,LastName,............
我收到此异常
无法执行查询 [select(emp.Eid)作为Eid1_0 _,(emp.FirstName)作为FirstName1_0 _,(emp.LastName)作为LastName1_0_来自Employee emp
修改:如果我选择所有列,它将完美无缺 - Select * from Employee emp
感谢您的帮助。
答案 0 :(得分:4)
我认为您可以使用NHibernate的ad-hoc mapping功能获益:
命名查询:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<sql-query name="findEmployeesInfo">
select emp.Eid as Id, emp.FirstName FirstName, emp.LastName as LastName from Employee emp
</sql-query>
</hibernate-mapping>
员工类:
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
// etc...
}
即席查询:
IList<Employee> employees = session
.GetNamedQuery("findEmployeesInfo")
.SetResultTransformer(Transformers.AliasToBean(typeof(Employee)))
.List<Employee>();