I'm avoiding using flush. Correct me if I'm wrong - if the a query is flush, I cannot roll it back. (?)
I was able to get it to commit if I allow null for the foreign key field that points to the Parent entity. But when I tested rollback(), the Parent was not inserted - the child was dangling with (NULL) reference back to the parent.
(I have cascade="all-delete-orphan", but I guess it didn't make a difference, since the child was inserted without reference back to its parent).
Here is the detail:
Code:
//s.setFlushMode(FlushMode.COMMIT);
Transaction t = s.beginTransaction();
ESR esr = new ESR(new Integer(52341), "ED");
s.save(esr);
//s.flush();
PatchedFile f1 = new PatchedFile("aa.jar", esr);
PatchedFile f2 = new PatchedFile("bb.jar", esr);
esr.addFile(f2);
esr.addFile(f1);
//esr.addFile("bb.jar");
s.save(f1);
s.save(f2);
//s.flush();
t.rollback();
s.close();
The queries sent:
Code:
Hibernate: insert into patchedfile (file, patchedInESR) values (?, ?)
Hibernate: SELECT LAST_INSERT_ID()
Hibernate: insert into patchedfile (file, patchedInESR) values (?, ?)
Hibernate: SELECT LAST_INSERT_ID()
The parent:
Code:
<class name="com.stc.cm.esr.hibernate.codegen.ESR" table="esr">
<id column="esrNumber" name="esrNumber" type="int">
<generator class="assigned"/>
</id>
<property column="createDate" length="19" name="createDate" not-null="true" type="timestamp"/>
<property column="Component" length="11" name="component" not-null="true" type="string"/>
<set name="patchedFiles" cascade="all-delete-orphan">
<key column="patchedInESR"/>
<one-to-many class="com.stc.cm.esr.hibernate.codegen.PatchedFile"/>
</set>
</class>
The child:
Code:
<class name="com.stc.cm.esr.hibernate.codegen.PatchedFile" table="patchedfile">
<id column="fileMapID" name="fileMapID" type="long">
<generator class="identity"/>
</id>
<property column="file" length="128" name="file" not-null="true" type="string"/>
<!-- <property column="patchedInESR" length="7" name="patchedInESR" not-null="true" type="integer"/> -->
<many-to-one name="esr" class="com.stc.cm.esr.hibernate.codegen.ESR" column="patchedInESR" outer-join="true" not-null="false"/>
</class>