javahibernatehqlnhibernate-projections

Fetch data by applying where clause on list which contains premitive data type elements in Hibernate


I have hibernate mapping like shared bellow

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

As you can see,I have two tables EMPLOYEE and REF_NUMBERS. There is POJO for EMPLOYEE table but REF_NUMBERS is just mapping table. Now I want to fetch employee name on the basis of list of reference numbers which I have with me. I don't want full Employee object just employee name and corresponding reference number. How I can write HQL for this? Is there any way to achieve with Projection?


Solution

  • 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];
    }