代码改变世界

NHibernate Step By Step(11)-命名SQL查询

2010-08-26 23:59  Clingingboy  阅读(1122)  评论(0编辑  收藏  举报

通过一个配置文件,往往无法满足所有的查询,nh提供了sql命名查询,即把sql查询语句定义在配置文件中,如下

<sql-query name="orgNamesOnly">
     <return-scalar column="NAME" type="string"/>
     SELECT org.NAME FROM ORGANIZATION org
 </sql-query>

1.通过GetNamedQuery方法获取IQuery

IList result = s.GetNamedQuery("orgNamesOnly").List();
image_2

<return-scalar>表示返回的数据字段类型

2.返回多个字段

<sql-query name="orgIdsAndOrgNames">
     <return-scalar column="orgid" type="long"/>
     <return-scalar column="thename" type="string"/>
     SELECT NAME AS thename, ORGID AS orgid
     FROM ORGANIZATION
     ORDER BY thename
 </sql-query>
 
返回结果
image_4 
3.返回类型
<sql-query name="AllEmploymentAsMapped">
     <return class="Employment"/>
     SELECT * FROM EMPLOYMENT
 </sql-query>

4.返回多个对象

    <sql-query name="EmploymentAndPerson">
         <return class="Employment"/>
         <return class="Person"/>
         SELECT * FROM EMPLOYMENT, PERSON
     </sql-query>
 


返回结果

image_6

5.返回对象和字段

<sql-query name="orgsAndOrgNames">
     <return alias="org" class="Organization"/>
     <return-scalar column="thename" type="string"/>
     SELECT org.NAME AS thename, org.NAME AS {org.name}, org.ORGID AS {org.id}
     FROM ORGANIZATION org
     ORDER BY thename
 </sql-query>

返回结果

image_8

6.返回对象具体字段

<return alias="org" class="Organization">
     <return-property name="id" column="ORGID"/>
     <return-property name="name" column="NAME"/>
 </return>

7.连接关联查询return-join

<sql-query name="organizationreturnproperty">
     <return alias="org" class="Organization">
         <return-property name="id" column="ORGID"/>
         <return-property name="name" column="NAME"/>
     </return>
     <return-join alias="emp" property="org.employments">
         <return-property name="key" column="EMPLOYER"/>
         <return-property name="element" column="EMPID"/>
         <return-property name="element.employee" column="EMPLOYEE"/>
         <return-property name="element.employer" column="EMPLOYER"/>
         <return-property name="element.startDate" column="XSTARTDATE"/>
         <return-property name="element.endDate" column="ENDDATE"/>
         <return-property name="element.regionCode" column="REGIONCODE"/>
         <return-property name="element.employmentId" column="EMPID"/>
         <return-property name="element.salary">
             <return-column name="AVALUE"/>
             <return-column name="CURRENCY"/>
         </return-property>
     </return-join>
     SELECT org.ORGID as orgid,
     org.NAME as name,
     emp.EMPLOYER as employer,
     emp.EMPID as empid,
     emp.EMPLOYEE as employee,
     emp.EMPLOYER as employer,
     emp.STARTDATE as xstartDate,
     emp.ENDDATE as endDate,
     emp.REGIONCODE as regionCode,
     emp.AVALUE as AVALUE,
     emp.CURRENCY as CURRENCY
     FROM ORGANIZATION org
     LEFT OUTER JOIN EMPLOYMENT emp ON org.ORGID = emp.EMPLOYER
 </sql-query>

查询结果 合并成一个对象,不再是两条分开的

image_8

8.load-collection

9.使用存储过程

见此http://www.cnblogs.com/lyj/archive/2009/08/12/1328240.html

10.结果集映射

可将结果映射和sql-query分开,通过指定resultset-ref来关联,代码则通过setResultSetMapping方法来设置

<resultset name="spaceship-vol">
   <return alias="sps" class="SpaceShip">
     <return-property name="id" column="id"/>
     <return-property name="name" column="name"/>
     <return-property name="model" column="model"/>
     <return-property name="speed" column="speed"/>
     <return-property name="dimensions.length" column="length"/>
     <return-property name="dimensions.width" column="width"/>
   </return>
   <return-scalar column="surface" type="double" />
   <return-scalar column="volume" type="double" />
 </resultset>
 
   <sql-query name="spaceship" resultset-ref="spaceship-vol">
       select id as id,
       fld_name as name,
       fld_model as model,
       fld_speed as speed,
       fld_length as flength,
       fld_width as width,
       fld_length * fld_width as surface,
       fld_length * fld_width *10 as volume
       from SpaceShip
   </sql-query>