views:

67

answers:

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.

Thanks all.

A: 

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).

Pascal Thivent
Thanks for the reply. "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. "Do u mean to say in the above cache configuration hving two different cache regions for children is advantageous. I did not understand how?? kindly elaborate. What about the consistency. If one the regions (org.demo.parent.children) got updated, the other cache region for children (org.demo.children) wouldn't reflect tht change. Doesnt this cause inconsistencyKindly clarify.
@lifeisnotfair I'm not sure to get you. First, do you agree that you can modify a collection by adding/removing elements without modifying the elements themselves? I don't see any source of inconsistency here. Second, Hibernate handles cache/regions for you so you don't have to worry about it. And this is one more reason to use the defaults (especially if you're new to Hibernate) instead of trying to optimize prematurely (and maybe wrongly) something that isn't a problem. If you want to fine tune, do it later.
Pascal Thivent