-->
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: Reduce number of queries when using bags
PostPosted: Fri Nov 15, 2013 9:43 am 
Newbie

Joined: Fri Nov 15, 2013 7:21 am
Posts: 2
Code:
<hibernate-mapping package="sandbox">

   <class name="person" table="PERSON">
      <id name="id" />
      <property name="name" />

      <joined-subclass name="child" table="CHILD">
         <key column="id" />

         <bag name="certificates" table="CERTIFICATES" lazy="false"
            fetch="subselect" cascade="all">
            <key column="id" />
            <composite-element class="Certificate" >
               <property name="board" />
               <property name="grade" />
            </composite-element>
            
         </bag>

      </joined-subclass>

   </class>


</hibernate-mapping>



If my child table has 10 rows. Hibernate does 11 queries. 1 to get all the children and then 1 per child to find certificates for each child.

This does not scale very well, is there a way to use join. I have tried using different combinations of cascade/fetch/lazy etc, but have not managed to reduce the queries?

Any help is much appreciated

Thanks

Chris


Top
 Profile  
 
 Post subject: Re: Reduce number of queries when using bags
PostPosted: Fri Nov 15, 2013 12:21 pm 
Newbie

Joined: Fri Nov 15, 2013 7:21 am
Posts: 2
This is a one to many relationship. Ie each child can have multiple certificates.

If I was to do this manually in jdbc I would query each table once and then add certificates to children:

For example:

Code:
List<Child> children = listChildren();
//map of certifcated by child id
Map<Integer,List<Certificate>> certs = listCertificates();

for(Child c: children){
c.addCertificates(certs.get(c.getId());
}


I thought I would be able to configure hibernate to do this. Is it possible?


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.