Hibernate throws Cannot delete or update a parent row: a foreign key constraint fails

前端 未结 2 553
臣服心动
臣服心动 2020-12-10 06:36

I am working on a basic example to test cascade delete operation but I am getting exception.

I have below entities:

Employee.java

相关标签:
2条回答
  • 2020-12-10 07:13

    When you delete Employee on session try to add this, I had the same issue:

    session.delete(session.get(Employee.class, employee_Id));
    

    On my issue I had Movie and TimeTable relation was OneToOne:

    On Movie model:

    public class Movie implements Serializable
    {
       @Id
       @Column(name = "fid")
       @GeneratedValue(strategy = GenerationType.IDENTITY)
       private int fid;
       ....
        @OneToOne(mappedBy = "movie", cascade = CascadeType.ALL, orphanRemoval = true)
        private TimeTable timetable;
    }
    

    On TimeTable model:

    public class TimeTable implements Serializable 
    {
       ...
        @OneToOne
        @JoinColumn(name = "fid")
        private Movie movie;
    }
    
    0 讨论(0)
  • 2020-12-10 07:19

    The REMOVE cascade type is for the standard JPA remove() operation. For the native Hibernate delete() operation, you need to use a Hibernate-proprietary annotation:

    @Cascade(CascadeType.DELETE)
    
    0 讨论(0)
提交回复
热议问题