users@glassfish.java.net

Re: Can not remove a EJB 3 entity and the error is: violation of foreign key constraint

From: Stijn de Witt <StijnDeWitt_at_chello.nl>
Date: Mon, 3 May 2010 20:48:05 +0200

Hi Sarah,

You should probably ask on a JPA or EclipseLink mailing list, but here is my stab in the dark.

The error you are getting is a foreign key constraint violation. It means that the record you want to delete is still being referred to by another record through a foreign key.

I think that CascadeType is not actually what you think it is/want it to be. I think that it's actually related to how your changes to the items in the collection are persisted. You probably need to set a 'ON DELETE CASCADE' on the foreign key constraint on the database level.

That or just delete the child object yourself first, before attempting to delete the parent.

I think this thread is related to your problem:
http://stackoverflow.com/questions/306144/jpa-cascadetype-all-does-not-delete-orphans

It mentions an annotation for EclipseLink, @PrivateOwned, which could be just what you are looking for.

Hope this helps,

-Stijn

  ----- Original Message -----
  From: Sarah kho
  To: users_at_glassfish.dev.java.net
  Sent: Monday, May 03, 2010 8:07 PM
  Subject: Can not remove a EJB 3 entity and the error is: violation of foreign key constraint


  Hi,


  I am trying to write a code to remove a entity which has collection of other entitiies as one to many relation. My source code for the one to many and many to one relations are like:




  In Contract class which which has a collection of Branches.


    @OneToMany(mappedBy = "contract", cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}, fetch = FetchType.EAGER)
      Collection<Branch> branches;






  In Branch class which has a collection of BranchMemberItem




   @OneToMany(mappedBy = "branch", cascade = {CascadeType.REMOVE,CascadeType.ALL}, fetch = FetchType.EAGER)
      protected Collection<BranchMemberItem> members;
   @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}, fetch = FetchType.EAGER)
      @JoinColumn(name = "contract")
      Contract contract;






  In BranchMemberItem class:


   @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE},fetch=FetchType.EAGER)
      @JoinColumn(name = "branch")
      protected Branch branch;


  I think I have all correct codes in place and sped long time finding what is wrong but I am still getting this error. If you know what is wrong wit my code, please let me know.




  javax.persistence.RollbackException: Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))): oracle.toplink.essentials.exceptions.DatabaseException
  Internal Exception: java.sql.SQLIntegrityConstraintViolationException: DELETE on table 'BRANCH' caused a violation of foreign key constraint 'BRNCHMMBRITEMBRNCH' for key (4440). The statement has been rolled back.
  Error Code: 20000
  Call: DELETE FROM BRANCH WHERE (ID = ?)
          bind => [4440]
  Query: DeleteObjectQuery(com.domain.Branch_at_57ee026e)
          at oracle.toplink.essentials.internal.ejb.cmp3.transaction.base.EntityTransactionImpl.commit(EntityTransactionImpl.java:120)
          at oracle.toplink.essentials.internal.ejb.cmp3.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:60)
          at com.utils.DAO.deleteContract(DAO.java:42)
          at com.dataentry.ContractorsTopComponent.jButton3ActionPerformed(ContractorsTopComponent.java:319)
          at com.dataentry.ContractorsTopComponent.access$000(ContractorsTopComponent.java:40)
          at com.dataentry.ContractorsTopComponent$1.actionPerformed(ContractorsTopComponent.java:119)
          at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
          at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
          at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
          at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
          at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
          at java.awt.Component.processMouseEvent(Component.java:6263)
          at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
          at java.awt.Component.processEvent(Component.java:6028)
          at java.awt.Container.processEvent(Container.java:2041)
          at java.awt.Component.dispatchEventImpl(Component.java:4630)
          at java.awt.Container.dispatchEventImpl(Container.java:2099)
          at java.awt.Component.dispatchEvent(Component.java:4460)
          at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
          at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
          at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
          at java.awt.Container.dispatchEventImpl(Container.java:2085)
          at java.awt.Window.dispatchEventImpl(Window.java:2478)
          at java.awt.Component.dispatchEvent(Component.java:4460)
          at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
          at org.netbeans.core.TimableEventQueue.dispatchEvent(TimableEventQueue.java:104)
          at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
          at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
          at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
          at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
          at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
          at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)