外键约束@​​ManyToMany关系防止删除

时间:2019-11-09 09:37:39

标签: spring spring-boot jpa

我有三个关联的记录(ConferenceSubmissionRecordSubmissionAuthorRecord)。每个SubmissionRecord都有一个Conference对象和一个List<SubmissionAuthorRecord>

如果SubmissionRecord与该会议相关联,则删除会议记录时,该记录也应级联并删除。但是,我不断收到一条java.sql.SQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`viz`.`submission_record_author_set`, CONSTRAINT `FKgnqq52l26bitkmojk1oiuaki1` FOREIGN KEY (`submission_record_s_id`) REFERENCES `submission_record` (`s_id`))错误消息。

submission_record_author_set是自动创建的,我没有映射到它的实体。

我了解问题在于,submission_record_author_set行阻止SubmissionRecord被删除,并尝试了此处描述的@PreRemove方法(How to remove entity with ManyToMany relationship in JPA (and corresponding join table rows)?),但无济于事。也许ManyToMany注释存在问题?原因我在SubmissionAuthorRecord中也看不到等效的批注。

@Entity
public class SubmissionRecord {

    @Id
    @GenericGenerator(name = "UseExistingIdOtherwiseGenerateUsingIdentity", strategy = "xyz")
    @GeneratedValue(generator = "UseExistingIdOtherwiseGenerateUsingIdentity")
    @JsonSerialize(using = ToStringSerializer.class)
    @Column(name = "s_id")
    private Long id;

    @Exportable(name = "Submission Id", nameInDB = "s_submission_id")
    @Column(name = "s_submission_id")
    private String submissionId;

    // internal set of authors of the associated
    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JsonIgnore
    private List<SubmissionAuthorRecord> authorSet;

    @JoinColumn(name="conference_id")
    @ManyToOne(fetch = FetchType.EAGER)
    @OnDelete(action = OnDeleteAction.CASCADE)
    private Conference conference;

    //...
}

@Entity
public class Conference {

    @Id
    @GenericGenerator(name = "UseExistingIdOtherwiseGenerateUsingIdentity", strategy = "xyz")
    @GeneratedValue(generator = "UseExistingIdOtherwiseGenerateUsingIdentity")
    @JsonSerialize(using = ToStringSerializer.class)
    private Long id;

    private String creatorIdentifier;

    private String conferenceName;

    private String conferenceYear;
}

@Entity
public class SubmissionAuthorRecord {

    @Id
    @GenericGenerator(name = "UseExistingIdOtherwiseGenerateUsingIdentity", strategy = "xyz")
    @GeneratedValue(generator = "UseExistingIdOtherwiseGenerateUsingIdentity")
    @JsonSerialize(using = ToStringSerializer.class)
    @Column(name = "s_author_id")
    private Long id;

    private String dataSet;

    @Column(name = "s_author_name")
    private String name;
}

submission_author_record_set表如下所示: enter image description here

0 个答案:

没有答案