-->
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.  [ 3 posts ] 
Author Message
 Post subject: Parent - child relationship, problem with lazy loading
PostPosted: Tue Oct 14, 2003 10:32 am 
Newbie

Joined: Tue Oct 14, 2003 4:34 am
Posts: 2
Location: Cracow
Hi,

I am using Hibernate 2.1 beta 1 with JBoss and I have a problem with lazy loading.

I have a class "Task" which can contain another tasks so I've created bidirectional association between task and its children.
Here is a part of my mapping file:

<class name="Task" table="TASKS" proxy="Task">
<many-to-one name="parentTask" column="PARENT" outer-join="false"/>
<set name="subtasks" lazy="true" inverse="true" cascade="all">
<key column="PARENT"/>
<one-to-many class="Task"/>
</set>
</class>

When I try to load "task" object from a database it is created in two db queries with a collection of its children even if "lazy-loading" flag is set to true, but I have to receive task much faster without its children in this case.

Can anybody help me?
Tomek


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 14, 2003 1:09 pm 
Regular
Regular

Joined: Fri Sep 05, 2003 12:01 am
Posts: 80
Location: Bogot
uhm... what code are you using to load the Task object?

_________________
Mauricio Hern


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 15, 2003 5:42 am 
Newbie

Joined: Tue Oct 14, 2003 4:34 am
Posts: 2
Location: Cracow
Hmmm, strange...

Let me give you additional info about mappings.
My "Task" object is a part of "Job" object. "Job" can contain many "Task" objects.

Here is a part of "Job" mapping file:

Code:
<set name="tasks" lazy="true" inverse="true" cascade="all" where="parent is null">
      <key column="JOB_ID"/>
      <one-to-many class="Task"/>
</set>


and adequate part of "Task" mapping file:

Code:
<many-to-one name="job" column="JOB_ID" not-null="true"/>



I create "Task" object using

Code:
task = (Task) session.load(Task.class, new Long(id))


I've set showSql flag to true and I can not see any sql statement on my console after execution of that line. Why ???

And now I try to execute

Code:
task.getJob()


and I can see sql statements similiar to this:

Code:
select a, b, c from Tasks where id=?
select a, b, c from Tasks where parent=?


Why? I supposed to see only first sql statement. I don't understand why Hibernate instantiates task's children.

and now I try to execute

Code:
task.getJob().getTasks()


I can see now a statement:

Code:
select a, b, c from Jobs where id=?


Great, but now I try to execute ...

Code:
task.getJob().getTasks().iterator()


and I have statement

Code:
select a, b, c from Tasks where job_id=?


Excellent, it is exactly what I wanted but after such statement I have a list of statements similiar to

Code:
select a, b, c from Tasks where parent=?
.

Number of such statements is the same as quantity of tasks in my "Job" object. My "Job" contains 300 tasks so my application sends to db 300 useless sql statements and it takes 8 seconds to execute my method!

Is there any way to avoid such situation or should I shoot myself?


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