users@glassfish.java.net

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

From: Sarah kho <sarah.kho_at_gmail.com>
Date: Mon, 3 May 2010 22:37:37 +0430

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)