-->
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: lazy trouble, i'm lost
PostPosted: Tue Apr 06, 2004 10:20 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
hi all,
i have A many-to-many B
and i want to construct a DOM like this:
<a1>
<b1/>
<b2/>
</a1>
<a2>
<b3/>
<b4/>
</a2>

the goal is to use only one (sql) query so i made this:
Code:
<mapping of class A>
       <set name="bs" table="B" lazy="true" outer-join="true"
       <key column="x" />
              <many-to-many class="B" column="xx" outer-join="true"
              />
        </set>
</mapping of class A>


one shot query:
Code:
"select a,b from A as a join a.bs as b" --> correct all instances are loaded and i have a collection of Objects[]

a1 b1
a1 b2
a2 b3
a2 b4

but i notice that the collections bs of a1 and a2 are not loaded and then 3000 queries are executed when calling
Databinder db = sessionFactory.openDatabinder();
db.setInitializeLazy(true);
db.bindAll((Collection)result);
Document dom = db.toGenericDOM();

1- if i replace
db.setInitializeLazy(true); by db.setInitializeLazy(false);
the db isn't hit but i do not have my 'bs' in my dom

2- if i replace lazy="true" by lazy="false" , i my mapping file, the 3000 sql queries are called just after the first query


I do not understand since all instances are in the sessions ;(

Thanks for help


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 06, 2004 11:55 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
grrrrr, always reading 100 times the doc and never notice a small keyword

Quote:
The inner join, left outer join and right outer join constructs may be abbreviated.

from eg.Cat as cat
join cat.mate as mate
left join cat.kittens as kitten
In addition, a "fetch" join allows associations or collections of values to be initialized along with their parent objects, using a single select. This is particularly useful in the case of a collection.

from eg.Cat as cat
inner join fetch cat.mate
left join fetch cat.kittens
A fetch join does not usually need to assign an alias, because the associated objects should not be used in the where clause (or any other clause). Also, the associated objects are not returned directly in the query results. Instead, they may be accessed via the parent object.


and the keyword is FETCH and of course it is explained in the doc

so in my case if i keep lazy=true and change my query to
Code:
"select a from A as a left fetch join a.bs as b"


the result is what i need:
a1 b1
b2
a2 b3
b4

And thanks to hibernate to provide this doc,


Anthony


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.