10 Replies Latest reply on Aug 21, 2005 7:51 PM by Emmanuel Bernard

    Problem with CascadeType.DELETE_ORPHAN in EJB 3 beta 1

    Chris Richardson Newbie

      My class has a undirectional one-to-many relationship.
      I want the children to be automatically deleted when removed from the collection and so I'm using DELETE_ORPHAN:

      @Entity(access = AccessType.FIELD)
      public class PendingOrder implements Serializable {
      ...
       @OneToMany(cascade = { CascadeType.ALL })
       @OrderBy("index")
       @JoinColumn(name = "PENDING_ORDER_ID")
       @org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
       private List<PendingOrderLineItem> lineItems = new ArrayList<PendingOrderLineItem>();
      
      ..


      This doesn't seem to be working.
      Rather than deleting the children EJB3/Hibernate is setting the FK column to null.

      I also noticed this in the log:

      2005-07-14 13:42:49,921 DEBUG [org.hibernate.cfg.annotations.CollectionBinder] Binding a OneToMany: net.chrisrichardson.foodToGo.ejb3.domain.PendingOrder.lineItems through a foreign key
      2005-07-14 13:42:49,921 DEBUG [org.hibernate.cfg.annotations.CollectionBinder] Collection role: net.chrisrichardson.foodToGo.ejb3.domain.PendingOrder.lineItems
      2005-07-14 13:42:49,921 DEBUG [org.hibernate.cfg.annotations.PropertyBinder] Building property lineItems
      2005-07-14 13:42:49,921 DEBUG [org.hibernate.cfg.annotations.PropertyBinder] Cascading lineItems with all,delete-orphan

      2005-07-14 13:42:52,312 DEBUG [org.hibernate.persister.collection.AbstractCollectionPersister] Static SQL for collection: net.chrisrichardson.foodToGo.ejb3.domain.PendingOrder.lineItems
      2005-07-14 13:42:52,312 DEBUG [org.hibernate.persister.collection.AbstractCollectionPersister] Row insert: update FTGO_PENDING_ORDER_LINE_ITEM set PENDING_ORDER_ID=? where id=?
      2005-07-14 13:42:52,312 DEBUG [org.hibernate.persister.collection.AbstractCollectionPersister] Row delete: update FTGO_PENDING_ORDER_LINE_ITEM set PENDING_ORDER_ID=null where PENDING_ORDER_ID=? and id=?
      2005-07-14 13:42:52,312 DEBUG [org.hibernate.persister.collection.AbstractCollectionPersister] One-shot delete: update FTGO_PENDING_ORDER_LINE_ITEM set PENDING_ORDER_ID=null where PENDING_ORDER_ID=?

      Any suggestions?

      Thanks.

      Chris