Hibernate 3.0.5.
Suppose I have an entity, with a one-to-many relationship to another entity -- the usual parent-child relationship.
Parent has a Set<Child>, mapped like this:
Code:
<class name="Parent">
<cache usage="read-write"/>
...
<set name="children" inverse="true">
<cache usage="read-write"/>
<one-to-many class="Child"/>
</set>
</class>
And the child has a Parent property, mapped like this:
Code:
<class name="Child">
<cache usage="read-write"/>
...
<many-to-one name="parent" class="Parent">
<column name="id_parent"/>
</many-to-one>
</class>
Pay special attention to the <cache/> settings. The children set in Parent has a cache, and the Child class has a cache of it's own. This works most of the time, except when a Child entity is created/removed without being explicitly added to the Parent set. When that happens, the cache for the parent's children collection will *not* be updated, resulting in 1) created children missing from the set or 2) deleted children causing an exception.
If I remove the <cache/> configuration from the Parent's children <set/>, things work again, but then hibernate will issue a SELECT to find a parent's children everytime a parent is loaded.
Shouldn't hibernate be smart enough to cache the set, and invalidate it when a children is created/deleted? Do I
really need to remember to add/remove children to/from the parent's collection (and save the parent) to take full advantage of caching? Sounds a little too cumbersome for me.