1 Reply Latest reply on Mar 19, 2007 5:40 AM by colineberhardt

    Deleting parent entity should clear out its foreign key in c

    thexfed

      I'm having an issue where deleting the parent entity isn't clearing out the Foreign Keys in the child entities (since I don't have cascade REMOVE from the parent to the child entities).

      In my application, I will assign a category (parent) to an entry (child) and save the entry which will update the category FK. However, when I delete a category, I'd like all FKs to that category in the entries collection to be cleared out.

      Currently when deleting a Category (that I just loaded, which has a couple of Entries in its collection) I only see a "delete from category where ..." SQL statement being executed. I'd expect to also see a couple "update entry set category_id=null where ..." so the FKs get cleared out.

      I tried to use the JoinColumn annotation on the Entry side to see if that would help, but that didn't seem to change any behavior:
      @JoinColumn(name="category_id", insertable=true, updatable=true, nullable=true)

      Any help would be much appreciated.


      // ************* CATEGORY ENTITY (parent) ******************
      
      @Entity
      @Table(name="category")
      public class Category extends BaseEntity {
      ...
       @Id
       @GeneratedValue(strategy=GenerationType.TABLE, generator="hilo")
       @TableGenerator(name="hilo", table="HIBBY_KEY", initialValue=100,
       pkColumnValue="category", pkColumnName="table_name", valueColumnName="next_hi", allocationSize=1)
       private Long id;
       @OneToMany(mappedBy="category")
       public Collection<Entry> entries;
      
      ...
      }
      
      
      // ************* ENTRY ENTITY (child) ******************
      // (Subclasses of this entity don't have anything significant in them)
      @Entity
      @Table(name="entry")
      @DiscriminatorColumn(name="entry_type", discriminatorType=DiscriminatorType.STRING, length=1)
      public abstract class Entry extends BaseEntity {
      ...
       @Id
       @GeneratedValue(strategy=GenerationType.TABLE, generator="hilo")
       @TableGenerator(name="hilo", table="HIBBY_KEY", initialValue=100,
       pkColumnValue="entry", pkColumnName="table_name", valueColumnName="next_hi", allocationSize=1)
       private Long id;
       @ManyToOne(optional=true, fetch=FetchType.EAGER)
       public Category category;
      
      ...
      }




        • 1. Re: Deleting parent entity should clear out its foreign key
          colineberhardt

          Hi,

          I would not expect the container to behave as you described. From what I understand, it is the responsibility of the EntityManager and the EJB container to save the state of your managed state Entity beans at the end of the transaction. If you wish your child.category property to be null, then you must manually apply this change in state to all your child entities of the parent category.

          Calling em.remove(parent) will remove 'parent' from the database but it will not delete parent the object, your application still holds a reference to it. It's state will be changed from managed to new. You child entities will still maintain a reference to this new state 'parent'

          Regards,
          Colin E.