父母: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应用程序。

我为每个实体(staff和staffGroup)都有一个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);
    }
    //...


现在,对于“视图”,我有一个名为StaffView的类,该类是创建视图的地方,它有两个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)


我确实有层叠。

当我仅使用一个StaffGate实例时,不会发生此问题。

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

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

更新资料

我的意思是


  当我仅使用一个StaffGate实例时,不会发生此问题


是在类:StaffView中删除了:staffGatestf实例,并且仅与staffGate一起使用。在这种情况下,不会发生任何异常。

最佳答案

我通过仅删除一个StaffGate实例来解决此问题。但是,如果有人知道如何解决,我将不胜感激

08-27 12:51