I have multiple classes that are mapped to the same table and I suspect that this will cause problems for the 2nd level cache.
As I understand it (please correct me if I am wrong) there is one cache per rootclass and flushing an object will only invalidate the entry in it's own cache regardless of to which table it is mapped.
The problem for us is that even after updating an object of type "BusinessObject", reading an "DTO" (se mapping below) will return stale data from the cache because
flushing an "BusinessObject" will never invalidate an "DTO" even if they are mapped to the same table and have the same primary key.
One possible solution I have thought of is to keep track of which entities are mapped to the same table and use a preFlush-callback in an Interceptor to call SessionFactory.evict() on
all entities mapped to the same table. Of course this would probably not implement correct transactional behaviour (we use READ_COMITTED) but we might be able to live with that.
Do you see any problem with this approach?
Does it matter if I set the "mutable" attribute to true on the "DTO" class?
Backround: only one of the classes mapped to each table is mutable, the rest are DTOs that only contain parts of the properties. For performance reasons we don't want to read the BOs from the database and then copy data to DTO but rather read the DTOs directly from database, or in this case cache.
Hibernate version:
2.1.4
Mapping documents:
Code:
<class name="BusinessObject" table="T">
<id name="Id" column="ID" type="int">
<generator class="assigned" />
</id>
<property name="P1" .../>
<property name="P2" .../>
<property name="P3" .../>
<property name="P4" .../>
<property name="P5" .../>
</class>
<class name="DTO" table="T">
<id name="Id" column="ID" type="int">
<generator class="assigned" />
</id>
<property name="P1" .../>
<property name="P4" .../>
</class>