-->
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.  [ 5 posts ] 
Author Message
 Post subject: many-to-one loads new instance for each row
PostPosted: Wed Nov 16, 2005 2:29 pm 
Newbie

Joined: Wed Nov 16, 2005 1:39 pm
Posts: 3
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 2.1.7c

Mapping documents:

I've stripped the example down to make it easier to understand:

Code:
<hibernate-mapping package="my.datamodel">
  <class name="HibFamilyData" table="FAMILY">
    <id name="id">
      <generator class="native"/>
    </id>
  </class>
</hibernate-mapping>

<hibernate-mapping package="my.datamodel">
  <class name="HibChildData" table="CHILD">
    <id name="id">
      <generator class="native"/>
    </id>
    <many-to-one
          name="family" class="HibFamilyData" cascade="none">
      <column name="FAMILY_ID"/>
    </many-to-one>
    </class>
</hibernate-mapping>


Question
Say I have three child rows that have the same family_id, and I load each child [session.load(HibChildData.class, id)] row. When I do this I get a new instance of HibFamilyData for each row. I thought the HibFamilyData would be in the first level cache and therefore each HibChildData would have the same instance of HibFamilyData.

Can someone point out my error?

Thanks,
-John K[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 16, 2005 9:06 pm 
Beginner
Beginner

Joined: Fri Oct 28, 2005 10:46 am
Posts: 37
You're certain that you're using the same Session to load all of them?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 4:07 am 
Regular
Regular

Joined: Sat Nov 05, 2005 5:33 am
Posts: 70
Location: Linz, Austria
I'm not sure, but you might want to check your implementation of equals (and hashCode) in the class.
I'm sure that you need those for composite ids, but it may be that you've got the same problem here.

And the problem with equals and composite ids is that Hibernate just can't compare two ids as equal unless you implement equals correctly.

_________________
hth,
Heinz
Don't forget to rate if this helped


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 8:19 pm 
Newbie

Joined: Wed Nov 16, 2005 1:39 pm
Posts: 3
zzantozz, you're spot on.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 8:20 pm 
Newbie

Joined: Wed Nov 16, 2005 1:39 pm
Posts: 3
zzantozz, you're correct. didn't spot that it was called createSession for each one!


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