Hi to everybody!
We use hibernate in a mid-sized project.
We use Hibernate 2.1.2 with ehcache as Second-level Cache.
I have now two classes mapped by one-to-one: (see attached mapping files)
MainObject and Object2, each of them using the second-level cache ("usage=read-write")
..within the application there is a sequence of three transactions (TR):
Each of the transactions starts with
Code:
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
and ends with
Code:
tx.commit();
session.close();
TR1:
create and save an instance of MainObject
TR2:
load an existing MainObject
create an instance of Object2
set references from MainObject to Object2 and vice-versa
(Object2 is saved because of cascade-all-mapping)
TR3:
load the MainObject
THE PROBLEM:
The Object2 is correctly saved to the database (a postgresql 7.4) but MainObject.getObject2() will return null in TR3.
Only some minutes later MainObject "knows" its Object2 and returns it correctly by MainObject.getObject2()
If I use one-to-many mapping in MainObject instead of one-to-one, this problem does not appear.
Since our transactions are strongly capsulated to abstract from hibernate, I cannot call sessionFactory.evict(MainObject.class), which would be -in this small testcase- another solution to the problem.
My Mapping-Files:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class
name="cachetest.MainObject"
table="mainobject"
dynamic-update="false"
dynamic-insert="false"
>
<cache usage="read-write" />
<id
name="id"
column="id"
type="java.lang.Long"
>
<generator class="native">
<param name="sequence">seq_mainobj</param>
</generator>
</id>
<one-to-one
name="obj2"
class="cachetest.Object2"
cascade="all"
outer-join="auto"
constrained="false"
/>
<property
name="description"
type="java.lang.String"
update="true"
insert="true"
column="description"
/>
</class>
</hibernate-mapping>
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class
name="cachetest.Object2"
table="object2"
dynamic-update="false"
dynamic-insert="false"
>
<cache usage="read-write" />
<id
name="id"
column="id"
type="java.lang.Long"
>
<generator class="foreign">
<param name="property">belongsToMainObj</param>
</generator>
</id>
<property
name="dummy"
type="java.lang.String"
update="true"
insert="true"
column="dummy"
/>
<one-to-one
name="belongsToMainObj"
class="cachetest.MainObject"
cascade="none"
outer-join="auto"
constrained="true"
/>
</class>
</hibernate-mapping>