-->
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.  [ 14 posts ] 
Author Message
 Post subject: Modelling a one-to-one, parent-child relationship
PostPosted: Fri Jul 30, 2004 11:29 am 
Newbie

Joined: Fri May 07, 2004 5:33 am
Posts: 18
Folks,

I'm having conceptual problems with trying to model a particular relationship. What I need is a one-to-one, parent-child relationship. The Parent and Child tables have a common primary key, with a constraint from the Child table to the Parent.

Currently, I'm modelling them as a primary-key-association one-to-one mapping as described in section 5.1.11 of the Hibernate docs, but this gives dreadful performance - querying for Parent objects triggers a torrent of select statements for the Child objects.

I've been back and forth on this problem using various alternatives, but none seem to model what I'm after. The example in Chapter 16, for instance, deals with a collection of child objects, rather than a one-to-one.

One solution is to give the Child table it's own primary key sequence, and model the relationshop as a many-to-one, but I'd rather keep the shared-primary-key shcema if possible.

Any thoughts?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 30, 2004 11:30 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Set the classes to lazy="true".

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 30, 2004 11:51 am 
Newbie

Joined: Fri May 07, 2004 5:33 am
Posts: 18
I'm not sure how that helps. Firstly, I want the initial select on parent to grab all the information for Parent and Child in one go using an outer-join. Secondly, iI tried adding lazy="true" to Parent and Child, but I'm still getting multiple selects issued when I query from parent;


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 30, 2004 11:53 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
What is "when I query from parent"?

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 30, 2004 11:57 am 
Newbie

Joined: Fri May 07, 2004 5:33 am
Posts: 18
Sorry, that was a bit amniguous :)

I mean when I do something like session.find("from Parent")


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 30, 2004 11:59 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Thats because HQL ignores any mapping metadata fetching strategies, for a good reason: You can define a runtime fetching strategy by using the "fetch" keyword, or with Criteria, a FetchMode.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 30, 2004 12:08 pm 
Newbie

Joined: Fri May 07, 2004 5:33 am
Posts: 18
Ah, OK, that makes sense.

So how about this lazy attribute? Even after I added lazy="true" to the <class> tags of Parent and Child, I'm still getting the mutltiple select when I run the query. Shouldn't this be a property on the <one-to-one_ mapping, rather than of the class itself?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 30, 2004 12:10 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Ahem, read again what I said. You will of course get immediate additional selects, since HQL ignores whatever you specify as a fetching strategy in your mappings. And yes, it would be nice if the lazy="true" was on the association mappings, not the class, but this is not how it is implemented.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 30, 2004 12:15 pm 
Newbie

Joined: Fri May 07, 2004 5:33 am
Posts: 18
But lazy="true" is a fetching strategy, is it not? When I add this to a collection mapping, it prevents additional selects to fetch the collection, but this is not the case for <one-to-one> associations.

So while I can use the FETCH clause to speed things up, how can I make the one-to-one association a lazy one?

Thanks for your patience :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 30, 2004 12:17 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
I'm at a loss here. You have to think about "default strategies" and "runtime strategies". What you define in mapping metadata is the default, you can override/ignore this with HQL or Criteria queries at runtime. The default fetching strategies are used whenever you a) load an object by id with get() or load() or b) navigate an object graph.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 30, 2004 12:22 pm 
Newbie

Joined: Fri May 07, 2004 5:33 am
Posts: 18
Yeah, we seem to be tying ourselves in knots here. Allow me to return to my original problem.

I have a better idea of what's going on now, but I still need a way to alter the mapping so that "select from Parent" does not issue the additional select for Child, but rather fetches Child lazily. This is what I see with a <set> mapping, and I'd like something similar for <one-to-one>.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 30, 2004 12:25 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
My point is that you can't alter the mapping in any way to get this with a "from Foo" query. HQL ignores any fetching strategy in your mapping. You know, why don't you try a simple "from Foo join fetch foo.bar" and look at the generated SQL. It will not change, whatever mapping fetching strategy you try. Then try loading Foo by id with get() and load() and try the different mapping attributes.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 30, 2004 12:44 pm 
Newbie

Joined: Fri May 07, 2004 5:33 am
Posts: 18
Erk, I see what you mean.

So how come I can influence the behaviour of "select from Foo" when Foo contains a Set (by adding lazy), but I can't influence it when I have a <one-to-one>? Why is the default fetch behaviour different?

For example, <set name="bar" lazy="true"> will prevent a multiple select, but there seems to be no equivalent for <one-to-one>

I understand the behaviour of the query now, I just don't understand this apparent inconsistency. Are collections just a special case?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 30, 2004 12:58 pm 
Newbie

Joined: Fri May 07, 2004 5:33 am
Posts: 18
I just read this:

http://www.hibernate.org/162.html

Bugger.

Ah well, not to worry. At least I understand this a bit better now.

Thanks for your help.


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