Reputation: 1421
In the second level cache explanation on
It says that: NHibernate will evict associated entities automatically if the association is mapped with cascade="all" or cascade="all-delete-orphan".
Why would I want nhibernate to evict cached associations when cascade is there?
Does this mean I have to change the associaton mappings to make nhibernate second level actually work?
Upvotes: 0
Views: 267
Reputation: 3466
It's a good thing that it works this way. Caching is great but it must err on the safe side to prevent invalid cached data when possible.
Think of the concept of an invoice with a header and line items. The header is set to cascade all insert/updates/deletes to the line items. So if you delete the header, the line items are also deleted. This makes sense because this constitutes one "Aggregate Entity", with the header being the Aggregate Root. These line items don't have a true identity on their own. If you save a new header, you want any added line items to be saved as well. Cascading is great in that it handles this for you.
Likewise from a caching perspective, if the header is changed and therefore flushed from the cache, all the line items should be flushed as well because a cascade may have impacted one or more of them. If you are reloading the aggregate root from the DB, you should reload it's children as well.
Basically, if the aggregate root has changed, you cannot guarantee the state of the child items in the cache, so you have to expect that they may have changed as well.
I should also add that you should think carefully about what you want to cache. If you are caching entities that undergo constant change, it may not benefit you. Typically you're going to cache things that are relatively static so the cascade is not a big deal.
Upvotes: 1