-->
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.  [ 2 posts ] 
Author Message
 Post subject: Hibernate criteria generates additional select query
PostPosted: Sat Aug 06, 2016 2:56 am 
Newbie

Joined: Sat Aug 06, 2016 2:51 am
Posts: 1
Hibernate createalias() method is used to create the joins. When I am calling s.iterator() method hibernate is generating another select query to get the child record? As per my understanding it generates the single select query to get the both parent and child records using join.

Criteria ct = sn.createCriteria(Vendor.class);
Criteria ct1= ct.createAlias("children", "cust");
List<Vendor> l = ct.list();
Iterator<Vendor> i = l.iterator();
while (i.hasNext()) {
Vendor v = (Vendor) i.next();
System.out.println("name " + v.getVendorname());
Set<Customer> s = v.getChildren();
Iterator<Customer> it = s.iterator();
while (it.hasNext()) {
Customer c = it.next();
System.out.println("custname " + c.getCustomername());
}
}
Mapping files;
<hibernate-mapping>
<class name="com.criteria.example.Vendor" table="vendor">
<id name="vendorid" column="vid">
<generator class="native" />
</id>
<property name="vendorname" type="string">
<column name="vname" length="10" not-null="true" />
</property>
<set name="children" inverse="false" cascade="all" lazy="false">
<key column="vendid" not-null="true"/>

<one-to-many class="com.criteria.example.Customer"/>
</set>

</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.criteria.example.Customer" table="customer" >
<id name="customerid" column="cid">
<generator class="native" />
</id>
<property name="customername" type="string">
<column name="cname" length="10" not-null="true" />
</property>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: Hibernate criteria generates additional select query
PostPosted: Sat Aug 06, 2016 10:57 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1638
Location: Romania
Try it like this:

Code:
Criteria ct = sn.createCriteria(Vendor.class);
ct.setFetchMode("children", FetchMode.JOIN).
Criteria ct1= ct.createAlias("children", "cust");

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


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

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.