Hi,
I'm using ehcache as second level cache.
My problem appears when loading object defined with <joined-subclass mapping
for instance:
Code:
<hibernate-mapping>
<class name="org.xx.dossierformation.model.DossierFormation" table="DOSSIER_FORMATION" lazy="true">
<cache usage="read-write"/>
<id name="id" column="DOSSIER_FORMATION_ID" type="java.lang.Long">
<generator class="identity"/>
</id>
<property name="dossierFormationType" column="DOSSIER_FORMATION_TYPE" type="java.lang.String" not-null="true"/>
<joined-subclass name="org.xx.dossierformation.model.DossierFormationStd" table="DOSSIER_FORMATION_STD" lazy="true">
<key column="DOSSIER_FORMATION_STD_ID_FK"/>
<property name="dossierFormationStdTypeAccordEngagement" column="DOSSIER_FORMATION_STD_TYPE_ACCORD_ENGAGEMENT" type="java.lang.String" not-null="true"/>
</joined-subclass>
<joined-subclass name="org.xx.dossierformation.model.DossierFormationMasse" table="DOSSIER_FORMATION_MASSE" lazy="true">
<key column="DOSSIER_FORMATION_MASSE_ID_FK"/>
<property name="dossierFormationMasseDureeBaremeJours" column="DOSSIER_FORMATION_MASSE_DUREE_BAREME_JOURS" type="java.lang.Float"/>
</joined-subclass>
</class>
</hibernate-mapping>
In my design this DossierFormation object doesn't exist on its own. It's either a DossierFormationStd or a DossierFormationMasse.
And it works as expected.
Unless you load a DossierFormation, don't update it, and reload it. Then the cache gives you a DossierFormation object that is not an instance of any of the joined-subclass.
Concretly it happens if a user loads an object for modification, then changes his mind, do something else, and finally wants to modify this same object.
If the update is not commited, then the second level cache doesn't know it has to refresh the object and loads the main object and not the joined-subclass one. As a consequence you can't test it with "instanceof" and have to reload the desired object and specify its real class, not subclass.
Is it an expected behavior or is there any trick to avoid that?
nodje