aspose file tools*
The moose likes Object Relational Mapping and the fly likes Where to Cache associations/sets/collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Where to Cache associations/sets/collections" Watch "Where to Cache associations/sets/collections" New topic
Author

Where to Cache associations/sets/collections

Monika Dullo
Greenhorn

Joined: Mar 14, 2010
Posts: 2
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.

Thanks all.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Yes, they are two seperate cache regions. However, I don't think you need to have different region names.

My suggestions, try both out and check the HibernateStatistics object for cache hit and miss ratios.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Monika Dullo
Greenhorn

Joined: Mar 14, 2010
Posts: 2
Thanks for the reply Mark.

I was more concerned about the consistency. In the above case we have different cache regions for children namely as org.demo.parent.children and org.demo.children then if one of them is updated the other one would not reflect that change. So will this not affect the consistency or shall the changes be propagated everywhere?? Kindly clarify this.

Doesn't the above configuration kind of mean that we shall have 2 cache regions for the same table/class??
Also my understanding of the above scenario is like this

org.demo.parent cache region will be something like this in dehydrated form:
id - name - {children ids} ::: These {children ids would point towards the org.demo.parent.children cache region and NOT to org.demo.children }

org.demo.parent.children will be smthng like this
id - name - parent_id

there would also be org.demo.children cache region which would be like org.demo.parent.children cache region but will be entirely different from it:
id - name - parent_id

Doesn't this kind of lead to redundancy and thereby inconsistency if one the regions is updated? Should then the associations then be mapped to the same table (example: to org.demo.children in the above case) to which they belong when caching them?

Kindly provide your inputs and comments on this.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Where to Cache associations/sets/collections