Hibernate - EhCache - Which region to Cache associations/sets/collections ??

Posted by lifeisnotfair on Stack Overflow See other posts from Stack Overflow or by lifeisnotfair
Published on 2010-03-14T07:26:28Z Indexed on 2010/03/14 7:35 UTC
Read the original article Hit count: 192

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.

© Stack Overflow or respective owner

Related posts about hibernate

Related posts about ehcache