[hibernate-issues] [Hibernate-JIRA] Created: (HHH-4007) Hibernate may generate duplicate/gap list index when cascade save

LC (JIRA) noreply at atlassian.com
Thu Jul 2 02:45:15 EDT 2009


Hibernate may generate duplicate/gap list index when cascade save
-----------------------------------------------------------------

                 Key: HHH-4007
                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-4007
             Project: Hibernate Core
          Issue Type: Bug
    Affects Versions: 3.3.2
         Environment: Windows. Java 1.5.14. Spring 2.5.6, Junit 3.8.1. Sybase
            Reporter: LC


Say we have ParentTable(p_id, etc) ChildTable(c_id, p_id, listindex)
Parent{
    id
    Arraylist children;
    ... properties
}
Child{
   id
   properties
}

mapping:
Parent.hbm.xml
<class name="Parent" table="ParentTable">
         <id name="id" type="java.lang.Long">
            <column name="p_id" />
            <generator class="native" />
        </id>
        <list name="children" cascade="all-delete-orphan">
        	<key column="p_id" not-null="true" update="false"/>
	<list-index>
        		<column name="list_index"/>
        	</list-index>
        	<one-to-many class="Child"/>
        </list>
</class>
Child.hbm.xml
<class name="Child" table="ChildTable">
        <id name="id" type="java.lang.Long">
            <column name="c_id"/>
            <generator class="native"/>
        </id>
</class>

DAO
PDAO extends HibernateDaoSupport{
    ......
    public void saveAndUpdate(Parent instance){
        getHibernateTemplate().saveOrUpdate(instance);
    }
    ......
}

Test case:

Parent p = new Parent();
p.setChildren(new Arraylist());

Child c1 = new Child();
Child c2 = new Child();
Child c3 = new Child();
Child c4 = new Child();
p.getChildren().add(c1);
p.getChildren().add(c2);
p.getChildren().add(c3);
p.getChildren().add(c4);

pDAO.saveAndUpdate(p);// now database have c1(list_index 0),c2(list_index1),c3(list_index2),c4(list_index3) all get saved and looks fine.

p.getChildren.remove(c3);
pDAO.saveAndUpdate(p);// now c3 get removed. database have 1(list_index 0),c2(list_index1), c4(list_index3). Here we get a gap.

Child c5 = new Child();
p.getChildren().add(c5);
pDAO.saveAndUpdate(p);// now database have 1(list_index 0),c2(list_index1), c4(list_index3), c5(list_index3). Here we get a gap and a duplicate.

>From this point. the dao can't do anything with the parent anymore and the data get cruptted.

I think this is a quite common case that something hibernate should be able to handle and it shouldn't leave gap and generate duplicate list_index.

Is there anyway to fix it in Hibernate?

Thank you very much.





-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        



More information about the hibernate-issues mailing list