I'm trying to set up a second level cache for my application, which uses Hibernate 3.6.10. I have been reading Hibernate documentation and it seems quite straight forward to configure a second level cache over it, just adding a third party provider (in my case it's EhCache).
However, I'm currently using some DB views to avoid having to step through Hibernate entities looking for some properties. As an example, I've got my Person
entity, which belongs to an Organization
:
<class name="com.mycompany.model.Person" table="tperson">
<id name="id" column="id" type="integer">
<generator class="native" />
</id>
<property name="name" column="name" />
<property name="surname1" column="surname1" />
<property name="emailAddress" column="email" />
<many-to-one name="organization"
class="com.mycompany.model.Organization">
<column name="id_organization" />
</many-to-one>
</class>
In order to avoid having to load the Organization
property for each Person
, specially while I'm loading lists of people, I use a DB view which makes the SQL join and loads the most common fields related to a person. That's how I map it:
<class name="com.mycompany.model.VPerson" table="vperson" mutable="false">
<id name="id" column="id" type="integer" />
<property name="name" column="name" />
<property name="surname1" column="surname1" />
<property name="emailAddress" column="email" />
<property name="organizationName" column="organization_name" />
<property name="clientName" column="client_name" />
</class>
So in most of the cases when I only want to display data I load the view entity, which also avoids having to load collections, and so on. However, when I want to edit a concrete person, I load the Person
entity, because I need it to be mutable.
So, if I use a second level cache related to the view, how to notify Hibernate to invalidate it when I update a concrete Person
? Cause a second level cache data is related to a concrete entity, AFAIK.