简体   繁体   English

无法添加或更新子行:尝试保存实体及其关系实体时出现外键约束失败错误

[英]Cannot add or update a child row: a foreign key constraint fails error when trying to save an entity and its relational entity

Well, i have the following tables in my database:好吧,我的数据库中有以下表格:

CREATE TABLE movie (
movie_id INTEGER NOT NULL AUTO_INCREMENT,
translated_title VARCHAR(150),
original_title VARCHAR(150),
plot MEDIUMTEXT,
genre VARCHAR(150),
country VARCHAR(250),
language VARCHAR(100),
upvotes INTEGER,
premiere_year INTEGER(4),
duration_minutes Integer,
CONSTRAINT movie_pk PRIMARY KEY (movie_id));

CREATE TABLE actor (
actor_id INTEGER NOT NULL AUTO_INCREMENT,
name VARCHAR(150),
nationality VARCHAR(150),
born_date DATE,
CONSTRAINT actor_pk PRIMARY KEY (actor_id));

CREATE TABLE actor_movie (
actor_id INTEGER,
movie_id INTEGER,
FOREIGN KEY (actor_id) REFERENCES actor (actor_id) ON DELETE CASCADE,
FOREIGN KEY (movie_id) REFERENCES movie (movie_id) ON DELETE CASCADE);

and the following entities:以及以下实体:

@Entity(name = "movie")
public class Movie implements Serializable {    

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "actor_movie",
        inverseJoinColumns  = { @JoinColumn(name = "actor_id", insertable = true, updatable = true)},
        joinColumns = { @JoinColumn(name = "movie_id", insertable = true, updatable = true) })
private Set<Actor> actors;

and:和:

@Entity(name = "actor")
public class Actor implements Serializable {

@ManyToMany(mappedBy="actors")
private Set<Movie> movies;

When i create a Movie object and set all properties, including an Actor and call the save method, i get: "Cannot add or update a child row: a foreign key constraint fails ( movieow . actor_movie , CONSTRAINT FKsc6u9gs0762qyrnyfwp9d5q2b FOREIGN KEY ( movie_id ) REFERENCES actor ( actor_id ))"当我创建一个 Movie 对象并设置所有属性,包括一个 Actor 并调用 save 方法时,我得到:“无法添加或更新子行:外键约束失败( movieow actor_movie ,CONSTRAINT FKsc6u9gs0762qyrnyfwp9d5q2b FOREIGN KEY ( movie_id ) REFERENCES actoractor_id ))”

I want to when i save a Movie object, it saves in cascade the actor and actor_movie tables.我想当我保存一个 Movie 对象时,它会级联保存 actor 和 actor_movie 表。

What am i doing wrong?我究竟做错了什么?

This sound like the problem that the different entities are not synchronised (I did not test it for your case).这听起来像是不同实体不同步的问题(我没有针对您的情况进行测试)。 The idea is to create add and delete methods for your set which add or delete the entity in the respective associated object.这个想法是为您的集合创建添加和删除方法,用于添加或删除相应关联对象中的实体。 For a clear example of @ManyToMany relationships, you could have a look at Vlad Mihalcea's article about these relationships .有关@ManyToMany关系的清晰示例,您可以查看Vlad Mihalcea 关于这些关系的文章 About synchronisation and cascading, he also wrote an article .关于同步和级联,他也写过一篇文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试在Hibernate中添加主复合键时,“无法添加或更新子行:外键约束失败” - 'Cannot add or update a child row: a foreign key constraint fails' when trying to add a primary composite key in Hibernate 无法添加或更新子行:外键约束失败Hibernate - Cannot add or update a child row: a foreign key constraint fails Hibernate 无法添加或更新子行:外键约束在休眠状态下失败 - Cannot add or update a child row: a foreign key constraint fails in hibernate 休眠:无法添加或更新子行:外键约束失败 - Hibernate:Cannot add or update a child row: a foreign key constraint fails 无法添加或更新子行:外键约束失败 - Cannot add or update a child row: a foreign key constraint fails 无法添加或更新子行:外键约束失败 - Hibernate - Cannot add or update a child row: a foreign key constraint fails - Hibernate 无法添加或更新子行:外键约束失败 - Cannot add or update a child row: a foreign key constraint fails Mysql无法添加或更新子行:外键约束失败 - Mysql Cannot add or update a child row: a foreign key constraint fails Hibernate:无法添加或更新子行:外键约束失败 - Hibernate :Cannot add or update a child row: a foreign key constraint fails MySQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束失败 - MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM