-->
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.  [ 5 posts ] 
Author Message
 Post subject: Loading recursive trees using a single query
PostPosted: Mon Jun 12, 2006 7:23 am 
Newbie

Joined: Mon Jun 12, 2006 5:32 am
Posts: 3
Location: Germany
I have the following table to store structured free-form data:

Code:
ID    GROUP_ID   PARENT_ID   TYPE       VALUE
-------------------------------------------------------
1     1          null        LIST       null
2     1          1           RECORD     null
3     1          2           TEXT       bla
4     1          2           INTEGER    123
5     1          2           ...


The table is mapped to a hibernate class OrderData with fields:

List<OrderData> children;
OrderData parent;

using lazy="false" and cascade="all-delete-orphan". This works without problems but causes hibernate to send a lot of queries to validate that every children-List is initialized.

Is there a way to read the complete tree and initialize the parent/children properties using a simple query like "from OrderData where group.id = 1" (perhaps using the internal methods of the hibernate PersistentCollection classes) and keeping the cascade functionality?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 12, 2006 9:30 am 
Newbie

Joined: Wed Feb 08, 2006 3:57 pm
Posts: 16
As far as I understand if your class and mapping define child-parent relationship (one-to-many in your case) it will be loaded automatically.

If you use lazy initialization then loading of children may be delayed until you use them. In this case you should have a valid session associated with the parent.


Top
 Profile  
 
 Post subject: Lazy Loading
PostPosted: Mon Jun 12, 2006 9:51 am 
Newbie

Joined: Mon Jun 12, 2006 5:32 am
Posts: 3
Location: Germany
Hi xflower, thanks for your reply.

Lazy loading does not help here, as the full tree is traversed in detached state afterwards, so I need everything loaded anyways.

This is the snippet from my OrderData.hbm.xml:

Code:
<many-to-one name="parent" class="OrderData" cascade="none"
    outer-join="auto" update="true" insert="true" column="PARENT_ID" />

<list name="children" lazy="false" inverse="true" cascade="all-delete-orphan">
    <key column="PARENT_ID"></key>
    <index column="SUB_INDEX" type="integer" />
    <one-to-many class="OrderData" />
<list>


[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 12, 2006 10:44 am 
Senior
Senior

Joined: Sun Jun 04, 2006 1:58 am
Posts: 136
Not sure if this what you are asking , anyways

You can use fetch in your query

from OrderData o where o.group.id = 1 left join fetch o.children........





----------------
Dont forget to rate


Top
 Profile  
 
 Post subject: left join fetch
PostPosted: Mon Jun 12, 2006 11:45 am 
Newbie

Joined: Mon Jun 12, 2006 5:32 am
Posts: 3
Location: Germany
Hi scarface,

yes, thats correct, but in this particular case there are already multiple (7) joins and fetch joins with other directly and indirectly linked tables in my query, and using left join fetch would end in 7 additional left joins for every layer of the tree.

It would greatly reduce the database load and traffic if the above can be realized.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.