-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Collection cache and bi-directional relationships
PostPosted: Fri Jun 10, 2005 2:39 pm 
Newbie

Joined: Thu Mar 17, 2005 1:55 pm
Posts: 15
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.