-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Many-to-many mapping not cascading save to children
PostPosted: Thu May 19, 2011 7:06 am 
Newbie

Joined: Thu May 19, 2011 6:42 am
Posts: 2
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 Scenario

Code:
<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 Gif

Code:
<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 Class

Code:
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 Class

Code:
public class GifUpload extends DomainObject{

    private String name;
   
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}


Failing Test

Code:
@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();
    }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.