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.  [ 6 posts ] 
Author Message
 Post subject: many-to-one join question
PostPosted: Mon Aug 06, 2007 3:40 pm 
Newbie

Joined: Mon Aug 06, 2007 12:57 pm
Posts: 4
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 1.2.0.GA

Name and version of the database you are using: MS Sql Server 2000

Consider the Parent, Child relationship using this mapping:

Code:
<class name="Parent" table="parents">
      <id name="id" column="id" type="int">
         <generator class="identity" />
      </id>
      <property name="firstName" column="firstName"  />
      <property name="lastName" column="lastName"  />

      <bag name="children">
         <key column="parentId"/>
         <one-to-many class="Child"/>
      </bag>
   </class>

   <class name="Child" table="children">
      <id name="id" column="id" type="int">
         <generator class="identity" />
      </id>
      <property name="firstName" column="firstName"  />
      <property name="parentId" column="parentId" />

      <many-to-one name="parent" class="Parent" column="parentId" fetch="join" lazy="false"/>

   </class>


If I retrieve a list of Children, why does it create a SELECT for every Parent when I use the fetch="join" attribute? Or do I miss-understand the "fetch" attribute? If so, is there a better way I should be doing this?

Ulitmate goal: I want to display all the children's names (thousands):

Code:
child.FirstName + " " + child.Parent.LastName;


or maybe???:

Code:
child.FirstName + " " + child.ParentLastName;


and have the initial list of children created using a join instead of a select for each child's parent.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 06, 2007 4:53 pm 
Regular
Regular

Joined: Fri Jan 27, 2006 2:32 pm
Posts: 102
Location: California, USA
I'm not sure I understand what the problem is.

Are you asking for explanation on how the fetch attribute works,
or, are you having a problem making a list of the full name of the child objects?

--Brian


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 06, 2007 5:53 pm 
Newbie

Joined: Mon Aug 06, 2007 12:57 pm
Posts: 4
pelton wrote:
Are you asking for explanation on how the fetch attribute works,
or, are you having a problem making a list of the full name of the child objects?


Basically when I retrieve the collection of Children, it does not do a JOIN for the Parent data.

I have found out more, if I use

Code:
IQuery query = session.CreateQuery("from Children");

it does not do a JOIN, however if I use

Code:
ICriteria c = session.CreateCriteria(typeof (Child));

it does create the JOIN. So I guess its a matter of me learning why now.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 07, 2007 10:27 am 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
The "fetch" attribute does not have any effect when using HQL. With HQL, you have to do this:
Code:
IQuery query = session.CreateQuery("from Children c left join fetch c.parent");

If you want to learn more about fetching strategies, have a look at the docs here:
http://www.hibernate.org/hib_docs/nhibe ... e-fetching

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 01, 2007 7:43 pm 
Newbie

Joined: Wed Jun 27, 2007 9:34 am
Posts: 8
I have been having issues trying to get NHibernate 1.2.0 to ever use the inner select to populate my many-to-one properties.

If I have the following query:

Code:
FROM AppUser app
LEFT JOIN FETCH app.Type


The AppUser query contains the inner join to Type, but it still queries the Type table once for each AppUser row.

Also, why does NHibernate query for every row, shouldn't it check the cache first since most rows in AppUser are of the same type?

Any ideas would be appreciated.

Thanks,

Sean


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 01, 2007 11:26 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
Please start a new thread instead of resurrecting old topics so you can give proper credit to whoever end up helping you.

By "cache", I assume you meant the second-level cache. Queries made by IQuery and ICriteria are not automatically cached. Have a read of this thread and see if it answers your question:
http://forum.hibernate.org/viewtopic.php?t=981567

_________________
Karl Chu


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