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: Where to Cache associations/sets/collections ??
PostPosted: Sat Mar 13, 2010 11:58 pm 
Newbie

Joined: Sat Mar 13, 2010 11:13 pm
Posts: 1
Hi all,

I am a newcomer to hibernate. It would be great if someone could comment over the following query that i have:

Say i have a parent class and each parent has multiple children. So the mapping file of parent class would be something like:

parent.hbm.xml

<hibernate-mapping >
<class name="org.demo.parent" table="parent" lazy="true">
<cache usage="read-write" region="org.demo.parent"/>
<id name="id" column="id" type="integer" length="10">
<generator class="native">
</generator>
</id>
<property name="name" column="name" type="string" length="50"/>

<set name="children" lazy="true">
<cache usage="read-write"
region="org.demo.parent.children" />
<key column="parent_id"/>
<one-to-many class="org.demo.children"/>
</set>


</class>
</hibernate-mapping>


*************
children.hbm.xml

<hibernate-mapping >
<class name="org.demo.children" table="children" lazy="true">
<cache usage="read-write" region="org.demo.children"/>
<id name="id" column="id" type="integer" length="10">
<generator class="native">
</generator>
</id>

<property name="name" column="name" type="string" length="50"/>

<many-to-one name="parent_id" column="parent_id" type="integer" length="10" not-null="true"/>

</class>
</hibernate-mapping>


So for the set children, should we specify the region org.demo.parent.children where it should cache the association or should we use the cache region of org.demo.children where the children would be getting cached.

I am using EHCache as the 2nd level cache provider. I tried to search for the answer to this question but couldnt find any answer in this direction. It makes more sense to use org.demo.children but I dont know in which scenarios one should use a separate cache region for associations/sets/collections as in the above case. Kindly provide your inputs also let me know if I am not clear in my question.

Also on another note, wouldnt having 2 cache regions for children cause inconsistency. If one of the regions gets updated then I guess that change wont be reflected in the other cache region? Is my understanding correct?? So what should be the correct way to configure the caches as in the above scenario?

Thanks all.


Top
 Profile  
 
 Post subject: Re: Where to Cache associations/sets/collections ??
PostPosted: Mon Mar 15, 2010 3:46 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
If one of the regions gets updated then I guess that change wont be reflected in the other cache region? Is my understanding correct??


Yes.

In practice it is indifferent in which regions you store entities or collections,
but there's one particular think to pay attention:
you should configure the regions in a way that entities do never expire before the collections which refer to this entities.
Example:
Scenario 1:
- you call a.getAllB();
- the association collection and the target B entities are all present in 2L cache
This is the perfect scenario, no database hit.
Scenario 2:
- you call a.getAllB();
- the association collection is present in 2L cache but some of the B entities which are referred by the collection
are no more present in 2l cache (already expired).
In this case Hibernate wlll issue single sql-statements in order to load the missing entities again into the caches.
Now Instead to have 0 or 1 database-hit loading the association, you can have up to <n> database hits! (where n is the size of the collection)


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.