无法添加或更新子行

无法添加或更新子行

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束失败(ravermeisterartist_recordlabel,CONSTRAINT FK_9dgdyft45droyopxsqijwb1dx外键(artist_id)参考artistid))


艺术家班

@Entity
@Repository
@Data
@NoArgsConstructor(force = true)
public class Artist {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Column(name = "artist_firstname")
    private String artist_firstname;

    @Column(name = "artist_secondname")
    private String artist_secondname;

    @Column(name = "artist_nickname")
    private String artist_nickname;


    @ManyToMany (fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinTable(name = "artist_recordlabel", joinColumns = @JoinColumn(name = "artist_id"),
            inverseJoinColumns = @JoinColumn(name = "label_id"))
    private Set<RecordLabel> recordLabels;

    @ManyToMany (fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinTable (name = "artist_musicrelease", joinColumns = @JoinColumn (name = "artist_id"),
    inverseJoinColumns = @JoinColumn (name = "musicrelease_id"))
    private Set <MusicRelease> musicReleaseSet;


和RecordLabel类

@Entity
@Repository
@Data
@NoArgsConstructor(force = true)
public class RecordLabel {

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

    @Column(name = "label_name")
    private String label_name;

    @Column(name = "label_country")
    private String label_country;

    @ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinTable(name = "artist_recordlabel", joinColumns = @JoinColumn(name = "label_id"),
            inverseJoinColumns = @JoinColumn(name = "artist_id"))
    private Set<Artist> artistsList;

最佳答案

请按照本指南进行多对多关系

http://www.baeldung.com/hibernate-many-to-many

09-30 15:54