-->
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.  [ 7 posts ] 
Author Message
 Post subject: Eagerly fetching many-to-one composite-id using HQL
PostPosted: Tue Apr 14, 2009 11:48 am 
Newbie

Joined: Wed Dec 05, 2007 11:23 am
Posts: 3
Hello everyone.
I'm using NHibernate 2.0. I am having some problems when eagerly fetching some data from an "association entity" with a composite-id.
The mapping file looks like this:
Code:
<class name="AssociationEntity">
   <composite-id>
      <key-many-to-one name="Entity1" column="Entity1Id" />
      <key-many-to-one name="Entity2" column="Entity2Id" />
   </composite-id>

   <!-- other properties -->

</class>


I'd like to fetch a list of those entities using HQL, while eagerly fetching one of the two keys in the composite-id (via join). The HQL query I'm using is the following:

Code:
FROM AssociationEntity AS ae
LEFT JOIN FETCH ae.Entity1
ORDER BY ...


The resulting SQL query is correct, but NHibernat will ignore the data that has already been fetched and query for all the referenced entities again.
After the first query is executed (with the join), the log contains lines like:

Code:
initializing proxy: [Entity1...]
attempting to resolve: [Entity1...]
object not resolved in any cache: [Entity1...]
Fetching entity [Entitiy1...]
...


But, if two instances of AssociationEntity contain a reference to the same Entitiy1 instance, the value will correctly be found in the session cache. But this should already be the case for every single AssociationEntity instance, since all needed data was already loaded during the first HQL query... Am I doing something wrong somewhere?


Top
 Profile  
 
 Post subject: Re: Eagerly fetching many-to-one composite-id using HQL
PostPosted: Wed Mar 03, 2010 7:02 pm 
Newbie

Joined: Wed Jan 04, 2006 12:52 pm
Posts: 4
Were you ever able to solve this problem? I'm having the same issue with (Java) Hibernate 3.3.2 GA. All referenced entities in the composite key were already fetched via "left join fetch", and I would think these should be in the session cache. However, Hibernate is issuing a select for each entity in the composite key anyway.


Top
 Profile  
 
 Post subject: Re: Eagerly fetching many-to-one composite-id using HQL
PostPosted: Tue Aug 10, 2010 10:10 pm 
Newbie

Joined: Tue Aug 10, 2010 10:05 pm
Posts: 2
Same request, curious to know if you were able to resolve this issue, and how? I am also experiencing this problem, exactly the same symptoms. Changing the cache of the AssociationEntity doesn't seem to help.


Top
 Profile  
 
 Post subject: Re: Eagerly fetching many-to-one composite-id using HQL
PostPosted: Wed Aug 11, 2010 10:59 am 
Newbie

Joined: Wed Jan 04, 2006 12:52 pm
Posts: 4
I never found a real fix to this problem, but I did come up with a workaround. If I execute an additional query to select the entities in the composite key outside the context of the composite key, those entities end up getting put in the session cache. I don't actually save the results of this query; I just do a #list() and discard the results. Then when I execute the real query with the composite key, Hibernate consults the session cache, sees that the entities are already in the cache, and connects them to the objects containing the composite key. Using the example above:


Execute query to return the Entity1's of interest, but don't bother saving the results to a variable. Just executing the #list() causes the objects to go in the session cache.


Execute the real query with the composite key of Entity1's and Entity2's, saving the AssociationEntity objects to a variable. The Entity1's should be pulled from the session cache.


So this results in 2 queries rather than 1, but that's at least as good as n + 1 for n >= 1.

As far as I understand, there does seem to be a real bug here with the entities in the composite key not automatically going into the session cache. This is just a workaround and not a fix.


Top
 Profile  
 
 Post subject: Re: Eagerly fetching many-to-one composite-id using HQL
PostPosted: Wed Aug 11, 2010 4:03 pm 
Newbie

Joined: Tue Aug 10, 2010 10:05 pm
Posts: 2
I was able yesterday after posting my query to come up with a solution that (almost) works.

I changed the key definition to use key-property only and the natural keys of the table (four columns in all, two for each Entity using your first example).
I added two many-to-one definitions to the class to define the Entities that were key-many-to-one, now simply as many-to-one.
At that point, NHib recognized the Entities from within the query and resolved the Entity population from the fetched data. Excellent

However, that's only for retrieving data, and we need to write data as well using the same class definition. At this point, I'm still struggling to make it work with my new definition - our concern is that we won't be able to use the same column in two properties of the object. If I get it working, I'll post what I find.

You're right, a workaround is to pre-load the Entities before running the main query - definitely better to have two queries than n+1 - however, the number of Entities involved in my case may not make this an expedient workaround - too time consuming.


Top
 Profile  
 
 Post subject: Re: Eagerly fetching many-to-one composite-id using HQL
PostPosted: Tue Apr 05, 2011 12:45 pm 
Newbie

Joined: Tue Apr 05, 2011 12:38 pm
Posts: 1
Its a very weird hibernate hibernate. Hibernate collection is not retrieving all results if the last property of a composite key is string. It does retrieve all results if the last property of the composite key is long. So basically re-ordering the composite key properties fixed the issue. I can't explain this behavior, I am hoping someone from hibernate team would answer this. The queries generated before and after were exactly the same expect the column reordering.

Class A has a set collection of Class B.

Class B key:
Before: Retrieved only 9 records.
<composite-id>
<key-property name="requestId" column="ANALYSIS_ID" type="long"/>
<key-property name="locations" column="LOCATION" type="string"/>
<key-property name="version" column="VERSION" type="long"/>
<key-property name="field" column="FIELD_NAME" type="string"/>
</composite-id>

After: Retrieved all 10 records.

<composite-id>
<key-property name="requestId" column="ANALYSIS_ID" type="long"/>
<key-property name="locations" column="LOCATION" type="string"/>
<key-property name="field" column="FIELD_NAME" type="string"/>
<key-property name="version" column="VERSION" type="long"/>
</composite-id>


Top
 Profile  
 
 Post subject: Re: Eagerly fetching many-to-one composite-id using HQL
PostPosted: Wed Oct 12, 2011 9:36 am 
Newbie

Joined: Wed Sep 21, 2011 9:34 am
Posts: 6
Thanks for this! I’ve been trying to make time to sort work out what the issue was but had never gotten
around to it!! Been pulling my hair out for the last 2 hours :)


Last edited by Celestejonas on Mon Jun 18, 2012 2:57 am, edited 1 time in total.

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