16.2.1. 使用return-property来明确地指定字段/别名

使用<return-property>你可以明确的告诉Hibernate使用哪些字段别名,这取代了使用{}-语法 来让Hibernate注入它自己的别名.

  1. <sql-query name="mySqlQuery">
  2. <return alias="person" class="eg.Person">
  3. <return-property name="name" column="myName"/>
  4. <return-property name="age" column="myAge"/>
  5. <return-property name="sex" column="mySex"/>
  6. </return>
  7. SELECT person.NAME AS myName,
  8. person.AGE AS myAge,
  9. person.SEX AS mySex,
  10. FROM PERSON person WHERE person.NAME LIKE :name
  11. </sql-query>

<return-property>也可用于多个字段,它解决了使用{}-语法不能细粒度控制多个字段的限制

  1. <sql-query name="organizationCurrentEmployments">
  2. <return alias="emp" class="Employment">
  3. <return-property name="salary">
  4. <return-column name="VALUE"/>
  5. <return-column name="CURRENCY"/>
  6. </return-property>
  7. <return-property name="endDate" column="myEndDate"/>
  8. </return>
  9. SELECT EMPLOYEE AS {emp.employee}, EMPLOYER AS {emp.employer},
  10. STARTDATE AS {emp.startDate}, ENDDATE AS {emp.endDate},
  11. REGIONCODE as {emp.regionCode}, EID AS {emp.id}, VALUE, CURRENCY
  12. FROM EMPLOYMENT
  13. WHERE EMPLOYER = :id AND ENDDATE IS NULL
  14. ORDER BY STARTDATE ASC
  15. </sql-query>

注意在这个例子中,我们使用了&lt;return-property&gt;结合{}的注入语法. 允许用户来选择如何引用字段以及属性.

如果你映射一个识别器(discriminator),你必须使用&lt;return-discriminator&gt; 来指定识别器字段