2

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.

Aritz
  • 30,971
  • 16
  • 136
  • 217

1 Answers1

1

You could use a mutable view and update VPerson entity fields as well. If no field contained in the view entity is changed, you could introduce an 'artificial' one (like updateDateTime) and change it just to mark the entity instance as dirty and force the cache invalidation/refresh.

The other way could be using a transactional cache and evicting the stale entries manually.

Dragan Bozanovic
  • 23,102
  • 5
  • 43
  • 110
  • That's a good idea. I didn't even know that MariaDB had updatable views. Having tried it, seems quite interesting. – Aritz Sep 18 '15 at 11:10
  • How about loading the corresponding `View` entity for each of my entities in their update methods and evicting it? Just how it's done here: http://stackoverflow.com/a/1604261/1199132 – Aritz Sep 18 '15 at 11:20
  • Regarding the eviction, it has to be transactional, as I noted in the second alternative (I am not sure whether you can use transactional EHCache with Hibernate 3.6; you might have to look at other cache providers). Otherwise, you risk inconsistency (concurrent transaction will just reload stale data into the cache after you evict them in the first transaction before the changes are committed). – Dragan Bozanovic Sep 18 '15 at 14:34