Removing an item from the one-to-many collection (Java + HIbernate + Struts)

I cannot delete a child from the database. From the method, org.apache.struts.action.Action.execute()I remove the child from the parent List, and also call session.delete(child). I simplified the code below and included only what I consider to be relavent.


Hibernation display

<class 
    name="xxx.xxx.hibernate.Parent" 
    table="parent">

    ...

    <list
        name="children"
        cascade="all,delete-orphan"
        lazy="true"
        inverse="true">

        <key column="parent_id"/>
        <index column="list_index"/>
        <one-to-many class="xxx.xxx.hibernate.Child"/>
    </list>
</class>

<class 
    name="xxx.xxx.hibernate.Child" 
    table="child">

    ...

    <many-to-one
        name="parent"
        class="xxx.xxx.hibernate.Parent"
        not-null="true"
        column="parent_id" />

</class>


Excerpt from execute () method

Transaction tx = session.beginTransaction();  //session is of type org.hibernate.Session

try {
    Parent parent = (Parent) session.get(Parent.class, getParentId());

    Iterator i = form.getDeleteItems().iterator();  //form is of type org.apache.struts.action.ActionForm
    while(i.hasNext()){
        Child child = (Child) i.next();
        session.delete(child);
        parent.getChildren().remove(child); //getChildren() returns type java.util.List
    }

    session.saveOrUpdate(parent);
    tx.commit();
} ...


session.delete(child);, parent.getChildren().remove(child); , . - . , ( System.out.println(); , ), . , - , , , !

Hibernate this SO . .

? ! .


:

  • Java 1.4.2
  • SQL Server 2005
  • Hibernate 3.0.5
  • Struts 1.2.7
  • Apache Tomcat 5.0.28
+5
3

equals(), , , , . remove . , delete , re-cascacde, . :

  • (t) > equals() ( hashCode()), id (), - - ( ) ( stackoverflow ), getChildren().remove(child)
  • , :

    Iterator<Child> i = form.getDeleteItems().iterator();
    while(i.hasNext()){
        Child child = i.next();
        for (Iterator<Child> it = parent.getChildren().iterator();) {
             if (child.getId().equals(it.next().getId()) {
                 it.remove(); // this removes the child from the underlying collection
             }
        }
    }
    
+6

, , Child. Child . :

Transaction tx = session.beginTransaction();  //session is of type org.hibernate.Session

try {
    Parent parent = (Parent) session.get(Parent.class, getParentId());

    Iterator i = form.getDeleteItems().iterator();  //form is of type org.apache.struts.action.ActionForm
    while(i.hasNext()){
        Child child = (Child) session.get(Chile.class, ((Child) i.next()).getChildId());
        parent.getChildren().remove(child); //getChildren() returns type java.util.List
    }

    session.saveOrUpdate(parent);
    tx.commit();
} ...

SQL, Hibernate

<property name="show_sql">true</property>
<property name="format_sql">true</property>

Edit:

10.

+1

Child , Hibernate , , . null, , .

parent.getChildren().remove(child);
child.parent = null; 
session.delete(child);

not-null = "true" .

It is best to work with reverse associations - update both sides in the Java code, so you can continue to work with objects in memory, and you do not need to worry about which side owns this relationship.

A similar situation is discussed here: http://simoes.org/docs/hibernate-2.1/155.html

-2
source

All Articles