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?