I am using a many-to-many relation ship between to classes. When I create the parent class Scenario and add some Gif's to it then save only the Scenario is saved. If I manually save the gifs and create the association in the link table it loads the associated Gifs so it appears only the cascade on save option is not working. I have tried substituting cascade="all" with "save-update". No difference in behaviour.
This all works as expected from NHibernate in .net against the same db schema so I am surprised it is not working in my Java project.
Mapping ScenarioCode:
<hibernate-mapping package="com.eet.mpm.portopt.domainmodel.entities">
<class lazy="false" name="ModelScenario" table="MODEL_SCENARIO">
<id column="ID" name="id" type="long">
<generator class="seqhilo">
<param name="sequence">MODEL_SCENARIO_PKS</param>
</generator>
</id>
<bag access="field" name="gifUploads" table="MODEL_SCENARIO_GIF_UPLOAD" fetch="join" cascade="all">
<key column="MODEL_SCENARIO_ID"/>
<many-to-many column="GIF_UPLOAD_ID" class="GifUpload"/>
</bag>
....
</class>
</hibernate-mapping>
Mapping GifCode:
<hibernate-mapping package="com.eet.mpm.portopt.domainmodel.entities">
<class lazy="false" name="GifUpload" table="GIF_UPLOAD">
<id column="ID" name="id" type="long">
<generator class="seqhilo">
<param name="sequence">GIF_UPLOAD_PKS</param>
</generator>
</id>
<property name="name" type="string">
<column name="name" sql-type="nvarchar2"/>
</property>
...
</class>
</hibernate-mapping>
Scenario ClassCode:
public class ModelScenario extends DomainObject {
private List<GifUpload> gifUploads;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ModelScenario() {
gifUploads = new ArrayList<GifUpload>(0);
}
public List<GifUpload> getGifUploads()
{
return gifUploads;
}
public void setGifUploads(List<GifUpload> gifUploads) {
this.gifUploads = gifUploads;
}
}
Gif ClassCode:
public class GifUpload extends DomainObject{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Failing TestCode:
@Test
public void Test_ModelScenario_With_Multiple_Gif()
{
Session hibernateSession = hibernateSessionFactory.getCurrentSession();
hibernateSession.beginTransaction();
List<GifUpload> uploads = new ArrayList<GifUpload>();
GifUpload gifUpload1 = CreateGif("Fred");
GifUpload gifUpload2 = CreateGif("Bill");
uploads.add(gifUpload1);
uploads.add(gifUpload2);
ModelScenario modelScenario = new ModelScenario();
modelScenario.setName("Joe");
modelScenario.setGifUploads(uploads);
assertThat(modelScenario.getGifUploads().size(),Is.is(2));
Long id = (Long) hibernateSession.save(modelScenario);
hibernateSession.getTransaction().commit(); // checking the database here reveals no Gifs saved
assertNotNull(id);
assertThat(modelScenario.getGifUploads().size(),Is.is(2));
hibernateSession = hibernateSessionFactory.getCurrentSession();
hibernateSession.beginTransaction();
ModelScenario load = (ModelScenario) hibernateSession.load(ModelScenario.class, id);
// No entries are getting created in the Model_Scenario_Gif_Upload table
// fails on this line
assertThat(load.getGifUploads().size(),Is.is(2));
hibernateSession.close();
}