-->
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.  [ 2 posts ] 
Author Message
 Post subject: one-to-one and second level cache
PostPosted: Tue Nov 28, 2006 11:16 am 
Newbie

Joined: Thu May 12, 2005 3:51 pm
Posts: 7
Hibernate version:
3.1.3 (but I checked the source and it's the same for 3.2.1)

In our code, we have many one-to-one relationships like the one below between Person and User. We found that whenever a Person object was loaded Hibernate always loaded the corresponding User object from the database (expected behavior) and did not load the User object from the second level cache (unexpected behavior).

In looking at the way that the one-to-one relationships are cached in the OneToOneType.java, it did not put the ID of the other side of the relationship in the disassembled object value array and thus the object was considered null and it was always loaded from the database.

We did find a class called SpecialOneToOneType.java that implemented what we believe was a better one-to-one caching mechanism. We copied over the assemble and disassemble methods to return the proper identifier and the ID was then put into the disassembled object array.

Thus when the object was re-constructed, Hibernate was able to check the cache for the object using the ID of the one-to-one and if that ID was in the cache it was returned. Doing this eliminated the database hits for the one-to-one relationships where the object was cached.

In our testing we have not yet run into any problems, but I would like to know if doing this will cause us other problems down the road that we have not yet seen. Is there any particular reason why Hibernate by default does not cache one-to-one relationships?

Mapping documents:
Code:
<class name="com.domain.entity.Person" table="PERSON">
   ... id and more here ...
   <property name="gender" type="java.lang.String" column="GENDER" length="50" not-null="true"/>
   <one-to-one name="user" class="com.authoria.domain.entity.User" property-ref="person" />
   ... more ...
</class>

<class name="com.domain.entity.User" table="DOMAIN_USER">
   <property name="login" type="java.lang.String" column="LOGIN" length="50" not-null="true" unique="true"/>
   <many-to-one name="person" class="com.domain.entity.Person" column="PERSON_ID" not-null="true" />
</class>


Top
 Profile  
 
 Post subject: one-to-one and second level cache
PostPosted: Thu Nov 30, 2006 12:34 am 
Newbie

Joined: Thu May 12, 2005 3:51 pm
Posts: 7
In doing a bit more debugging I found that Hibernate will issue additional SQL to retrieve objects by foreign keys even when if all of the objects are retrieved in a single query.

Same example as above, but there is another one-to-one relationship between Person and User that is specified with fetch=join.

The first SQL that is issued is:
Code:
from PERSON_NAME personname0_, PERSON person1_, DOMAIN_USER user2_
where personname0_.ID=person1_.PERSON_NAME_ID(+) and person1_.ID=user2_.PERSON_ID(+) and
personname0_.ID=?


This populates the Person and PersonName objects, then since User points back to Person in a one-to-one relationship it issues the following SQL to get the Person object by foreign key for the User.
Code:
from PERSON person0_, DOMAIN_USER user1_
where person0_.ID=user1_.PERSON_ID(+) and person0_.PERSON_NAME_ID=?


Can Hibernate process the entire result set for a single object before trying to hydrate the back pointer associations?


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