开发者

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

开发者 https://www.devze.com 2022-12-23 08:06 出处:网络
I am a newcomer to hibernate. It would be great if someone could comment over the following query that i have:

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>
&开发者_JS百科lt;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.


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.

By default, the optional region is set to the class or collection role name. So for the set, the default region name would be "org.demo.Parent.children" (i.e. different from the default region name for the Child which would be "org.demo.Child"). This makes sense IMO since you want to be able to invalidate a particular collection or all the collections in a region without invalidating all the entities from the type of a collection.

But actually, the big question here is: why the hell don't you use the defaults? You are introducing extra work, maintenance and potential sources of problem and I fail at understanding the benefits. As a fan of conventions over configuration, I use the default (i.e. I don't set region).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号