Java Spring + Hibernate"无法添加或更新子行"

时间:2017-11-27 01:11:36

标签: java mysql spring hibernate jpa

  

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束失败(ravermeisterartist_recordlabel,CONSTRAINT FK_9dgdyft45droyopxsqijwb1dx FOREIGN KEY( 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;

1 个答案:

答案 0 :(得分:0)

请按照本指南获取多对多关系

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