c# - Map SQL Query to Business object in Nhibernate -
i want map sql query business object
using nhibernate
. there lot fields in employee table, getting 3 , want map one.
here sql query
<sql-query name="findemployeesinfo"> <return alias="emp" class="calibr.businessdocuments.bos.employee, businessdocuments"/> <![cdata[ select (emp.eid) {emp.id},(emp.firstname) {emp.firstname},(emp.lastname) {emp.lastname} employee emp ]]> </sql-query>
here have make constructor map 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 getting exception
could not execute query [ select (emp.eid) eid1_0_,(emp.firstname) firstname1_0_,(emp.lastname) lastname1_0_ employee emp
edit: if select columns, work perfect -- select * employee emp
thanks help.
i think benefit using nhibernate's ad-hoc mapping features:
named query:
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"> <sql-query name="findemployeesinfo"> select emp.eid id, emp.firstname firstname, emp.lastname lastname employee emp </sql-query> </hibernate-mapping>
employee class:
public class employee { public int id { get; set; } public string firstname { get; set; } public string lastname { get; set; } // etc... }
ad-hoc query:
ilist<employee> employees = session .getnamedquery("findemployeesinfo") .setresulttransformer(transformers.aliastobean(typeof(employee))) .list<employee>();
Comments
Post a Comment