Go to SQL Query to Business Object in Nhibernate

I want to match an SQL query Business objectwith using Nhibernate. There are many fields in the Employee table, but I get only three and want them to display only those.

here is my sql query

<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>

here I am creating a constructor to display these columns

public Employee(int Id, string FirstName, string LastName)
{
    this.Id = Id;
    this.FirstName = FirstName;
    this.LastName = LastName;            
}

DB Employee Table Column Names: Eid, FirstName, LastName, ............

I get this exception

failed to execute the request [select (emp.Eid) as Eid1_0 _, (emp.FirstName) as FirstName1_0 _, (emp.LastName) as LastName1_0_ from Employee emp

Edit: if I select all columns, it will work perfectly -Select * from Employee emp

Thank you for your help.

+2
source share
1 answer

, 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...
}

Ad-hoc-:

IList<Employee> employees = session
                              .GetNamedQuery("findEmployeesInfo")
                              .SetResultTransformer(Transformers.AliasToBean(typeof(Employee)))
                              .List<Employee>();
+4

All Articles