-->
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.  [ 9 posts ] 
Author Message
 Post subject: Why multiple selects instead of a join?
PostPosted: Wed Mar 17, 2004 3:50 pm 
Newbie

Joined: Thu Mar 04, 2004 9:38 pm
Posts: 4
I have a two tables, basically a parent table and a child table. I want the parent object to contain a set of child ids as opposed to references to the child objects themselves. I feel I set this up propertly in the hbm.xml file (see below). However, when I access all the parent objects I see that hibernate executes a select statement for every parent row, instead of a single join! Can this be right?

Code:
<hibernate-mapping>
      <class name="eg.Parent" table="PARENT">
           <id name="id" column="ParentID" type="int">
               <generator class="identity"/>
           </id>           
           <set name="childIDs" table="CHILD" cascade="all" lazy="false">
              <key column="ParentID"/>
              <element column="ChildID" type="int"/>
           </set>
      </class>
</hibernate-mapping>


In my DAO object I execute: session.find(from Parent);

The resuling sql is basically:
select * from PARENT
select * from CHILD where ParentID = ?
select * from CHILD where ParentID = ?
select * from CHILD where ParentID = ?
.
.

I would expect it to be something more like:
select * from Parent p inner join Child c on p.id=c.parentID


Using hibernate 2.1.

Thanks,
Thaddeus


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 18, 2004 10:54 am 
Beginner
Beginner

Joined: Thu Jan 01, 2004 11:36 am
Posts: 23
Location: Belgium
According to me you have to use the join fetch clausule in your statement but watch out, it has some strange side-effects. Try to do a test with parents with and without children; you will end up by having as many parents as there are children, similar to the result of an sql-statement, if I remember correctly.

At some point proxies come in here, but I don't know how it works; also check out the lightweight/heavyweigth design pattern on the wiki area.

Hope it helps,

Jan


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 18, 2004 11:18 am 
Newbie

Joined: Wed Mar 17, 2004 4:51 pm
Posts: 6
Hi,

I have a similar issue.I would like to impleent parent child functionality.
There is a set of students with one classRep for the class.
I made this as a subclass of the "student" class.
Now I want to write a query that would pull out all students under a single particular classRep

i am writing my query as:

select * from Parent p inner join Child c on p.id=c.parentID ...

so my actual query is:

final String QUERY =
"from Student s inner join classRep c on t.id=c.parentID where d.class=" + false +"where d.id =?";

Using Hibernate ersion:

version 2.0.3, 27 August 2003


But this gives me error:

com.dynetics.gmpm.db.DatabaseException: net.sf.hibernate.JDBCException: Could not execute query: Syntax error or access violation, message from server: "You have an error in your SQL syntax near 'student1_.ID='gdavid' and student0_.classRep=student_.id)' at line 1"

Any tips....
Thanks much in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 18, 2004 12:30 pm 
Newbie

Joined: Wed Mar 17, 2004 4:51 pm
Posts: 6
As a addendum to above post, how to access subclass (classRep) and use classRep.id = student.id????

(problem is i dont have a table per se named "classRep" but just a subclass,, so I guess I cant use classRep.id in my SQL query...if so, looking for alternate ways to rephrase my query....

Thanks a lot!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 18, 2004 12:36 pm 
Newbie

Joined: Wed Mar 17, 2004 4:51 pm
Posts: 6
1 more addition.....classRep is the classRepresentative....

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 18, 2004 1:19 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Just use outer-join=true and set hibernate.max_fetch_depth to positive value.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 18, 2004 1:49 pm 
Newbie

Joined: Wed Mar 17, 2004 4:51 pm
Posts: 6
Thanks for the reply.
Will I be able to set these values in my query...
my query is as follows:

final String QUERY = "from Student s inner join ClassRep c on t.id=c.parentID where d.class=" + false +"where d.id =?";


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 18, 2004 1:58 pm 
Newbie

Joined: Wed Mar 17, 2004 4:51 pm
Posts: 6
Thanks for the reply.
Will I be able to set these values in my query...
my query is as follows:

final String QUERY = "from Student s inner join ClassRep c on t.id=c.parentID where d.class=" + false +"where d.id =?";


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 18, 2004 3:30 pm 
Newbie

Joined: Wed Mar 17, 2004 4:51 pm
Posts: 6
ok...i added the above (fech_size and outer_join in my hiberntae.properties file).
Now I am getting :
net.sf.hibernate.QueryException: outer or full join must be followed by path expression


Thanks for the hel!


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