通过在列表中应用where子句来获取数据,该子句包含Hibernate

时间:2015-07-22 11:10:31

标签: java hibernate hql nhibernate-projections

我有hibernate映射,如shared below

<class name="com.service.Employee" table="EMPLOYEE" lazy="true">
    <id name="empId" type="java.lang.String" column="emp_id" length="50">
        <generator class="assigned" />
    </id>
    <property name="name" column="name">
    <property name="mobNumber" column="mob_number">
    <list name="refNumber" table="REF_NUMBERS">  
        <key column="refId"></key>  
        <list-index column="type"></index>  
        <element column="ref_number" type="string"></element>  
    </list>  
</class>

如您所见,我有两个表EMPLOYEE和REF_NUMBERS。 EMPLOYEE表有POJO,但REF_NUMBERS只是映射表。现在我想根据我的参考编号列表获取员工姓名。我不希望完整的Employee对象只是员工姓名和相应的参考编号。我怎么能为此写HQL?有没有办法实现Projection

1 个答案:

答案 0 :(得分:0)

select e.name, rn from Employee e join refNumber rn where rn in (:refNumbers)

query.setParameter("refNumbers", refNumbers);

for (Object[] row : (List<Object[]>) query.list()) {
   String name = (String) row[0];
   String rn = (String) row[1];
}