-->
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.  [ 8 posts ] 
Author Message
 Post subject: Outer fetching, and
PostPosted: Fri May 07, 2004 5:19 am 
Newbie

Joined: Fri May 07, 2004 4:34 am
Posts: 5
Hi,

I have a little problem with Hibernate 2.1.3.key-many-to-one.
Here is a part of my configuration, speaking for itself :

Code:
<class name="Application" table="APPLICATIONS">
    <id name="id" column="APP_ID" type="java.lang.Integer" >
        <generator class="native" />
    </id>
    ...
</class>

<class name="Selection" table="SELECTIONS">
    <composite-id>
        <key-property
            name="user"
            column="SEL_USER"
            type="java.lang.String" />
        <key-many-to-one
            name="application"
            column="APP_ID"
            class="Application" />
        </key-many-to-one>
    </composite-id>
    ...
</class>


Then, I want to retrieve all the 'selection' entities for a specific user,
and fetch all the selection.application entities :

Code:
Query q = hS.createQuery("from Selection as selection " +
                         "left join fetch selection.application  " +
                         "where selection.user = :user " +
                         "order by selection.column, selection.row")
            .setString("user", username);


In the logs, I always see :
- One first SQL query to retrieve the selections
- n more SQL queries, retrieving the application for each selection.
I would like to retrieve all the selections and selection.applications in one single query - I'm sure it's possible.
So I tried to debug, and found these lines in net.sf.hibernate.loader.Loader (line 341) :

Code:
// if we know there is exactly 1 row, we can skip.
// it would be great if we could _always_ skip this;
// it is a problem for <key-many-to-one>

if ( isSingleRowLoader() && id!=null ) {
   resultId = id;
}
else {
   Type idType = persister.getIdentifierType();
   resultId = (Serializable) idType.nullSafeGet(rs, suffixedKeyColumns[i], session, null); //problematic for <key-many-to-one>!
   if ( id!=null && resultId!=null && id.equals(resultId) ) resultId = id; //use the id passed in
}


I'm not quite sure that this is about the problem I have,
but if anybody has any hint, or any piece of advice,
it would be much appreciated.
Thanks !


Top
 Profile  
 
 Post subject: To make it shorter
PostPosted: Fri May 07, 2004 11:07 am 
Newbie

Joined: Fri May 07, 2004 4:34 am
Posts: 5
In a few words : Is the "left join fetch" supposed to be compatible with a "key-many-to-one" relationship ?

Should it possible to populate an element *and* its child element in one single SQL query,
if the child element is mapped as a "key-many-to-one" key of a composite Id ?

I think/hope yes, but I'm still investigating on how to make it work...


Top
 Profile  
 
 Post subject: Same thing....
PostPosted: Fri May 07, 2004 2:49 pm 
Newbie

Joined: Fri May 07, 2004 2:46 pm
Posts: 5
I am experiencing the same thing. Join fetching seems to work (ie, retrieve joined records in one query) for a normal many-to-one mapping, but not for key-many-to-one mapping. Anyone have experience with this?

-eric


Top
 Profile  
 
 Post subject: Did anyone solve this?
PostPosted: Thu Apr 21, 2005 8:22 pm 
Newbie

Joined: Thu Aug 05, 2004 6:01 pm
Posts: 18
Location: New Zealand
Hi

This is exactly the issue I am having -- left join fetch a composite key-many-to-one relationship generating only one sql statement.

Any progress?

Thanks
Shane


Top
 Profile  
 
 Post subject: potential work-around
PostPosted: Fri Apr 29, 2005 1:46 am 
Newbie

Joined: Thu Apr 28, 2005 8:16 pm
Posts: 9
I experienced this issue myself (unable to specify a fetch="join" for key-many-to-one association). I replicated the key-many-to-one association as a many-to-one association with fetch="join", and was amazed to see that it worked. My testing was not rigorous and I am sure I have not considered all of the consequences, but here it is:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
   
<hibernate-mapping package="socnav.services.data">

    <class name="TemplCourseOneWayXfer" table="soc_templ_course_one_way_xfer">

        <composite-id>
            <key-many-to-one name="templateCourse" class="TemplateCourse" column="course_id"/>
            <key-many-to-one name="courseCatCode" class="CourseCatCode" column="course_cat_code_id"/>
        </composite-id>

        <!-- duplicate of courseCatCode in key; I am able to specify the
             fetch="join" here, which I can't do in the composite-id;
             I had to change the case of the column name to avoid duplicate
             column name exceptions when the configuration is loaded -->
        <many-to-one name="courseCatCode" class="CourseCatCode" column="Course_cat_code_id" not-null="true" fetch="join"/>

    </class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 29, 2005 10:43 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Look at hibernate-3.x/test/org/hibernate/test/cid for a better way to handle identifying relationships.

Actually the same example is here:

http://www.hibernate.org/hib_docs/v3/re ... posite-key


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 04, 2005 6:55 pm 
Newbie

Joined: Sat Oct 30, 2004 3:48 pm
Posts: 14
gavin wrote:
Look at hibernate-3.x/test/org/hibernate/test/cid for a better way to handle identifying relationships.

Actually the same example is here:

http://www.hibernate.org/hib_docs/v3/re ... posite-key


So, what you are saying is key-many-to-one can not be outer join fetched in h2/h3?

I posted the same problem before http://forum.hibernate.org/viewtopic.php?t=940398

It makes no since that this should not work, is it a bug or just everyones misunderstanding?


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 09, 2005 6:06 am 
Beginner
Beginner

Joined: Fri Apr 09, 2004 12:47 pm
Posts: 36
I agree, mapping things like this (specifiyng twice the same column, and so on) is involved. Is there any specific reason for Hibernate inability to do the right thing without all this hassle?


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