I'm having some trouble whith Hibernate 2.0.3
Here is the case.
I have a mono-directional one-to-one relationship with a foreign generator.
I save the non mapped side object in one session, close it
Then I save the mapped side object and hibernate recreate a new non-mapped side object. I can't understand why.
Here are some materials
Code:
session = sf.openSession();
Transaction tx = session.beginTransaction();
Dumb dumb = new Dumb();
session.save(dumb);
tx.commit();
session.close();
Long oldDumbId = dumb.getId(); //copy id
//new session
session = sf.openSession();
tx = session.beginTransaction();
Dumber dumber = new Dumber();
dumber.setDumb(dumb);
session.save(dumber);
tx.commit();
session.close();
assertEquals(oldDumbId, dumb.getId()); //this fail, actually a new dumb is inserted in DB with a new id
the previous code with session.saveOrUpdate(dumb); before session.save(dumber); works.
Here is the mapping
Code:
<hibernate-mapping>
<class name="sample.Dumb" table="dumb">
<id name="id" column="id" type="long" unsaved-value="null">
<generator class="sequence">
<param name="sequence">SEQ_DEFAULT</param>
</generator>
</id>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="sample.Dumber" table="dumber">
<id name="id" column="id" type="long" unsaved-value="null">
<generator class="foreign">
<param name="property">dumb</param>
</generator>
</id>
<one-to-one class="sample.Dumb" constrained="true" name="dumb"/>
</class>
</hibernate-mapping>
Is this the expected behavior ? Why hibernate does not execute a saveOrUpdate() on dumb instead of save() when saving dumber ?