-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: How to initialize the child of parent using projection
PostPosted: Tue Dec 17, 2013 6:21 am 
Newbie

Joined: Tue Dec 17, 2013 5:06 am
Posts: 1
I have the requirement to load only some properties of parent(DepartmentTestDTO) object and find the no of child(EmployeeTestDTO) objects using projections in Hibernate Criteria/HQL. is it possible to initialize child(EmployeeTestDTO) collection using projection. so that i can find the size of child collection

Code:
public class DepartmentTestDTO {   
    private Long deptId;
    private String deptName;
    private String deptLocation;

    private Long noOfEmployees;

    private Set<EmployeeTestDTO> empTestDTOlist;
        //setters and getters
}


public class EmployeeTestDTO {


    private Long empId;
    private String empName;
    private String empAddress;
    private String email;
     //setters and getters

}


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 8 Jul, 2013 7:58:38 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.OneToMany.EmployeeTestDTO" table="EMPLOYEE_TEST">
        <id name="empId" type="java.lang.Long">
            <column name="EMP_ID" />
            <generator class="increment" />
        </id>
        <property name="empName" type="java.lang.String">
            <column name="EMP_NAME" />
        </property>
        <property name="empAddress" type="java.lang.String">
            <column name="EMP_ADDRESS" />
        </property>
        <property name="email" type="java.lang.String">
            <column name="EMAIL" />
        </property>
    </class>
</hibernate-mapping>


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 8 Jul, 2013 7:58:38 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.OneToMany.DepartmentTestDTO" table="DEPARTMENT_TEST">
        <id name="deptId" type="java.lang.Long">
            <column name="DEPT_ID" />
            <generator class="increment" />
        </id>
        <property name="deptName" type="java.lang.String">
            <column name="DEPT_NAME" />
        </property>
        <property name="deptLocation" type="java.lang.String">
            <column name="DEPT_LOCATION" />
        </property>
        <set name="empTestDTOlist" table="EMPLOYEE_TEST" inverse="false" lazy="false"  fetch="select"
          cascade="save-update">
            <key>
                <column name="DEPT_ID" />
            </key>
            <one-to-many class="com.OneToMany.EmployeeTestDTO" />
        </set>
    </class>
</hibernate-mapping>


I am trying the below code to execute
Code:
Criteria criteria = session.createCriteria(DepartmentTestDTO.class);

        criteria.createAlias("empTestDTOlist", "emp");

        ProjectionList projectionList = Projections.projectionList()
                 .add(Projections.property("deptId").as("deptId"))
                 .add(Projections.property("deptName").as("deptName"))
                 .add(Projections.property("empTestDTOlist").as("empTestDTOlist"));     


        criteria.add(Restrictions.eq("deptName", "HR"));
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

        criteria.setProjection(projectionList);

        criteria.setResultTransformer(Transformers
                .aliasToBean(DepartmentTestDTO.class));

        List list = criteria.list();


but getting the below error


Code:
Hibernate: select this_.DEPT_ID as y0_, this_.DEPT_NAME as y1_, this_.DEPT_ID as y2_ from DEPARTMENT_TEST this_ inner join EMPLOYEE_TEST emp1_ on this_.DEPT_ID=emp1_.DEPT_ID where this_.DEPT_NAME=?
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
    at org.hibernate.loader.criteria.CriteriaLoader.getResultRow(CriteriaLoader.java:166)


any help... appreciated in advance.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.