-->
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.  [ 11 posts ] 
Author Message
 Post subject: Many-to-one mapping (inner join)
PostPosted: Fri Jan 07, 2005 3:19 pm 
Beginner
Beginner

Joined: Sun Nov 07, 2004 4:19 pm
Posts: 38
Version 2.6.1

I can't figure out a way to use inner joins for my many-to-one mappings. Hibernate keeps generating outer joins. How can I make it do an inner join when using the session.load.

dino


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 07, 2005 7:05 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Outer join is correct in this situation.
What are you trying to achieve?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 08, 2005 1:07 pm 
Beginner
Beginner

Joined: Sun Nov 07, 2004 4:19 pm
Posts: 38
It maybe correct but is inefficient since I KNOW my many-to-one always exists. A more efficient way is to use an inner join. Actually, if any of my many-to-one relationship is not null, one should always use an inner join for efficiency.

Dino


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 08, 2005 1:11 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Nobody really understands what you are trying to do, please read the rules of this forum and fill out the question form.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 08, 2005 4:44 pm 
Beginner
Beginner

Joined: Sun Nov 07, 2004 4:19 pm
Posts: 38
See http://forum.hibernate.org/viewtopic.ph ... +manytoone

There is an example but no adequate response from the hibernate team.

Let me rephase the question: why can't I specify in the mapping file to use an inner join rather than an outer join?

Dino


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 08, 2005 5:05 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
[blockquote]When outer-join is set to true:
load(IncoiceItem.class,pk); produces 1 SQL Select with outer joins.

When outer-join is set to false:
load(IncoiceItem.class,pk); produces 3 SQL Selects

Thats the behaviour described in "Hibernate in action" Chapter 4 Page 14[/blockquote]

And if you set the associated entity to lazy="true" a single select statement will be executed. Those are perfectly valid options and I don't see what an inner join could improve.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 08, 2005 6:49 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Assuming that inner joins are universally more performent than an outer join when the appropriate relationship constraints and not null attributes are set then this could be a special optimisation. Four years completing a PhD leads me to always look for the proof-of-fact to support the assertion. It may turn out to be correct but I would think that the difference would be minimal thus not worth the added complexity in the sql generation phase. I might be wrong as a study might show its reasonably significant. Do you have a link to such a study or compelling discussion?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 08, 2005 7:56 pm 
Beginner
Beginner

Joined: Sun Nov 07, 2004 4:19 pm
Posts: 38
I don't have any links or documents or phd theories. I never use outer joins when it should be expressed as an inner join. However, they are different and I'm sure outer joins cannot be optimized as inner joins can be. But I don't time to find links/research to validate that so I have to be satisfied with your response. At least you recognise that you don't do it because it makes your life harder in the sql generation.

I lead a team of 12 developers and is one of the common questions that gets asked because no one writes outer joins when you don't need to.

Thanks for your responses.

Dino


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 09, 2005 8:29 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Actually, it is straightforward to write

Code:
from Foo foo inner join fetch foo.bars


or even just

Code:
from Foo foo join fetch foo.bars



It doesn't make much sense to specify an inner join at the mapping file level, because it is possible that the mapping file fetch strategy could be used recursively, so you could get an inner join following an outer join (which is always wrong).

But, if you are following the best practices (make all associations lazy in the mapping file, and override in the query), then it is easy to get an inner join as above.


I think that fully answers your problem/question ;)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 10, 2005 3:56 pm 
Beginner
Beginner

Joined: Sun Nov 07, 2004 4:19 pm
Posts: 38
This is what I am doing in the end. I was hoping to avoid this since we were initially trying to avoid HSQL.


Dino


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 08, 2005 5:46 am 
Newbie

Joined: Wed Dec 22, 2004 5:56 am
Posts: 9
I think that your suggestion is not available in all situations. Imagine following mappings:

<class name="Card">
...
</class>

<class name="CardApplication">
...
<many-to-one name="card"/>
</class>

<class name="Device">
...
</class>

<class name="Transaction">
...
<many-to-one name="device"/>
<many-to-one name="cardApplication"/>
</class>

And I want to select from Transaction. I can write query:

from Transaction as t inner joinn fetch t.device inner join fetch t.cardApplication as ca inner join fetch ca.card

But when I have e.g. 10000 transactions and only 500 cardApplications, therefore it is not as efficient as could be, because there is huge redundancy in the retrieved data.

When I write query:

from Transaction as t inner joinn fetch t.device

The card transactions are retrieved with separate selects and the card is retrieved with outer join and I can't affect it to be used inner join, that is more efficent (nearly 5x in DB2 environment), because the query is automatically generated by Hibernate.

So how to solve this problem??

Thanks


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