-->
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: Persisting and retrieving a collection property
PostPosted: Tue Jul 20, 2010 11:17 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
Hello,

I have a Form object containing several FormItem objects in a property.

Here is the model:
Code:
public class Form implements java.io.Serializable {

   private Integer id;
   private int version;
   private String name;
   private Set<FormItem> formItems = new HashSet<FormItem>(0);
...
}

public class FormItem implements java.io.Serializable {

   private Integer id;
   private int version;
   private String type;
   private String name;
   private Form form;
...
}


The test that fails:
Code:
   @Test
   public void testCollection() {
      formItem0 = new FormItem();
      formItem0.setName("name");
      formItem0.setListOrder(2);
      formItem0.setType("itemtype");
      formItem0.setForm(form0);
      formItem1 = new FormItem();
      formItem1.setName("name");
      formItem1.setListOrder(1);
      formItem1.setType("itemtype");
      formItem1.setForm(form0);
      form0 = formDao.makePersistent(form0);
      formItem0 = formItemDao.makePersistent(formItem0);
      formItem1 = formItemDao.makePersistent(formItem1);
      Form retrievedForm = formDao.findById(form0.getId(), false);
      retrievedForm = formDao.findById(form0.getId(), false);
      assertEquals(2, retrievedForm.getFormItems().size());
   }


with the exception:
Quote:
FormDaoTest@80669d, testMethod = testCollection@FormDaoTest, testException = java.lang.NullPointerException]]


The test class constructor:
Code:
   public FormDaoTest() {
      form0 = new Form();
      form0.setName(name0);
      form1 = new Form();
      form1.setName(name1);
      form1.setImage(image);
   }

Note that the other following test works fine:
Code:
   @Test
   public void testSaveAndRetrieve() {
      form0 = formDao.makePersistent(form0);
      assertNotNull(form0.getId());
      assertNotSame(form0.hashCode(), 0L);
      assertFalse(form0.toString().equals(""));
      Form retrievedForm = formDao.findById(form0.getId(), false);
      assertEquals(form0.hashCode(), retrievedForm.hashCode());
      assertEquals(form0.getName(), retrievedForm.getName());
      assertNotNull(retrievedForm.getId());
   }


The hibernate mapping:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 18, 2010 1:03:51 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
    <class name="core.domain.Form" table="form" dynamic-insert="true" dynamic-update="true">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <version name="version" type="int">
            <column name="version" not-null="true" />
        </version>
        <property name="name" type="string">
            <column name="name" length="50" not-null="true" />
        </property>
        <property name="description" type="string">
            <column name="description" not-null="false" />
        </property>
        <set name="formItems" inverse="true">
            <key>
                <column name="form_id" not-null="false" />
            </key>
            <one-to-many class="core.domain.FormItem" />
        </set>
    </class>
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 18, 2010 1:03:51 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
    <class name="core.domain.FormItem" table="form_item" dynamic-insert="true" dynamic-update="true">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <version name="version" type="int">
            <column name="version" not-null="true" />
        </version>
        <many-to-one name="form" class="core.domain.Form" cascade="all" fetch="select">
            <column name="form_id" not-null="true" />
        </many-to-one>
        <property name="type" type="string">
            <column name="type" length="50" not-null="true" />
        </property>
        <property name="name" type="string">
            <column name="name" length="50" not-null="false" />
        </property>
        <property name="listOrder" type="int">
            <column name="list_order" not-null="true" />
        </property>
    </class>
</hibernate-mapping>


The issue I have is with the test that tries to persist and retrieve the formItems proprety of the form object. This property is a collection, of objects of type FormItem.

I'm a bit lost when it comes to persisting and retrieving this collection...

Is my mapping okay ? Do I need to call these makePersistent dao methods ?

If anyone has a better understanding on how to persist a collection property, I'm all ears :-)

Thanks for any tips..

Stephane


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.