JPA:奇怪的Cascade删除行为,OneToMany。使用两个单独的DAO时发生FK违规

时间:2016-06-25 12:34:58

标签: java mysql jpa eclipselink cascade

父级:StaffGroup

@Entity
@Table( name = "STAFF_GROUP")
public class StaffGroup implements Serializable{
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private long id;

    @OneToMany(mappedBy="staffGroup", cascade = CascadeType.ALL)
    private List<Staff> staffs = new ArrayList<>();

孩子:员工

@Entity
@Table( name = "STAFF")
public class Staff implements java.io.Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    // Relationships
    @ManyToOne
    @JoinColumn(name = "STAFF_GROUP_ID")
    private StaffGroup staffGroup;

这是一个使用JPA和MySQL作为数据库的JavaFX应用程序。

我为每个实体(员工和员工组)都有一个DAO。一个逻辑类称为StaffGate,如下所示:

public class StaffGate {
    // GRP
    private List<StaffGroup> staffGrps = new ArrayList<>();
    private StaffGroupDAO groupDAO = new StaffGroupDAO();

    // Staff
    private static List<Staff> staffList = new ArrayList<>();
    private StaffDAO staffDao = new StaffDAO();

    public void removeFromDBAndMemory(StaffGroup element) {
        groupDAO.remove(element);
        staffGrps.remove(element);
    }
    //...

现在,对于View我有一个名为StaffView的类,这个类是创建View的地方,它有两个StaffGate实例,一个用于GroupStaff操作,另一个用于Staff。

public class StaffView {
    private StaffGate staffGate = new StaffGate();// Grp operations
    private StaffGate staffGatestf = new StaffGate();// Staff operations

现在,我可以创建一个组和工作人员,并且它们会被持久化,我也可以删除一个Staff实体。问题是当我想删除一个StaffGroup实体时,它会抛出一个FK违例异常

[EL Warning]: 2016-06-25 10:35:20.567--UnitOfWork(1053875258)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.2.v20151217-774c696): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`school-finma`.`staff`, CONSTRAINT `FK_STAFF_STAFF_GROUP_ID` FOREIGN KEY (`STAFF_GROUP_ID`) REFERENCES `staff_group` (`ID`))
Error Code: 1451
Call: DELETE FROM STAFF_GROUP WHERE (ID = ?)
    bind => [1 parameter bound]
Query: DeleteObjectQuery(StaffGroup [id=4, name=Drivers, imageByteInfoEntity=null, staffs={[]}, school=School [id=1, name=Banafsaj, schoolType=USA, address=null, creationDate=2016-06-13, image=ImageByteInfoEntity [id=1, byteImage.length=2793472, width=1024, height=682], active=true], active=true])
Exception in thread "JavaFX Application Thread" javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.2.v20151217-774c696): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`school-finma`.`staff`, CONSTRAINT `FK_STAFF_STAFF_GROUP_ID` FOREIGN KEY (`STAFF_GROUP_ID`) REFERENCES `staff_group` (`ID`))
Error Code: 1451
Call: DELETE FROM STAFF_GROUP WHERE (ID = ?)
    bind => [1 parameter bound]
Query: DeleteObjectQuery(StaffGroup [id=4, name=Drivers, imageByteInfoEntity=null, staffs={[]}, school=School [id=1, name=Banafsaj, schoolType=USA, address=null, creationDate=2016-06-13, image=ImageByteInfoEntity [id=1, byteImage.length=2793472, width=1024, height=682], active=true], active=true])
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.flush(EntityManagerImpl.java:879)
    at derbyware.school.dao.AbstractFacade.remove(AbstractFacade.java:49)
    at derbyware.school.logic.StaffGate.removeFromDBAndMemory(StaffGate.java:49)

我确实有cascade.all。

当我使用仅一个StaffGate 实例时,问题会出现 NOT

为什么会这样?我该如何解决?

我在调用StaffGate中的删除方法之前添加了这一行,但问题仍然存在:staffGatestf = new StaffGate();

更新

我的意思

  

当我只使用一个StaffGate实例

时,问题不会发生

是Class:StaffView我删除:staffGatestf实例,并且只与staffGate一起工作。在这种情况下,不会发生异常。

1 个答案:

答案 0 :(得分:0)

我通过只删除一个StaffGate实例来修复它。但如果有人知道如何解决我会很感激