In one of my previous posts we saw how we could configure the NHibernate second level cache. The second level cache could also be used for entities that are not read only.
For example lets say we want to add the User entity to the second level cache. User entity is by no means a read only entity. Hence, we will have to configure the User entity mapping to indicate that it should be added to a read-write cache. In the previous post we had added a read only entity Country to the cache. Building on the same example lets add User entity to a read write cache. The mapping for the User entity could look like
As you can see, the mappings for User and Country entities are pretty straightforward. The only thing to notice is that we have marked User as ReadWrite and Country as ReadOnly. We have also specified the cache region to use for each entity. For now we are using the cache region "ReadWriteRegion" for both entities.
Lets write a unit test where we update the user's country and save it. Simple enough right?
So far so good. Lets run the test to see what is the result.
Hmm, obviously something wrong. The problem is that we have included the Country entity and the User entity in the same second level cache region i.e. "ReadWriteRegion".
The solution is pretty simple. We just have to make sure that the readonly entities are cached in a different cache region from read-write entities.
How do we do that? Simple, specify a different cache region name in the mapping files. The updated mappings look like
If we now run the test you will see all is well and the test case passes.