[英]Spring Boot - java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails
I am getting this error and I cannot solve this problem.我收到此错误,无法解决此问题。
There were plenty of StackOverflow answers based on this topic, but neither one fits me.有很多基于这个主题的 StackOverflow 答案,但没有一个适合我。
The goal is to save Todo
and afterwards save TaskTodo
(connecting table of Task
and Todo
).目标是保存
Todo
,然后保存TaskTodo
(连接Task
和Todo
表)。
I tried to save and flush Todo
and afterwards save TaskTodo
, but it failed.我尝试保存并刷新
Todo
,然后保存TaskTodo
,但失败了。
Can someone tell me where is the problem?有人能告诉我问题出在哪里吗?
ERROR:错误:
java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (
todo
.task_todo
, CONSTRAINTFK8dn0kderp1dame65xsokdaavj
FOREIGN KEY (todo_id
) REFERENCEStodo
(id
))java.sql.SQLIntegrityConstraintViolationException:不能添加或更新子行,外键约束失败(
todo
。task_todo
,约束FK8dn0kderp1dame65xsokdaavj
外键(todo_id
)参考todo
(id
))
Assignement - Parent abstract class:分配 - 父抽象类:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Assignment {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
}
Todo - Domain class: Todo - 域类:
@Entity
public class Todo extends Assignment {
private boolean isChecked;
@OneToMany(mappedBy = "todo")
Set<TaskTodo> taskTodoSet;
}
Task - Domain class:任务 - 域类:
@Entity
public class Task extends Assignment {
private String description;
@OneToMany(mappedBy = "task")
public
Set<TaskTodo> taskTodoSet;
public Task() {}
public Task(String name) {
this.setName(name);
}
}
TaskTodo - domain class: TaskTodo - 域类:
@Entity
public class TaskTodo {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
@ManyToOne
@JoinColumn(name = "task_id")
Task task;
@ManyToOne
@JoinColumn(name = "todo_id")
Todo todo;
public TaskTodo(Task task, Todo todo) {
this.task = task;
this.todo = todo;
}
}
Service function for saving Todo and TaskTodo:保存 Todo 和 TaskTodo 的服务功能:
@Override
public void saveTodo(Todo todo, Long taskId) {
Task task = getTaskById(taskId);
todoRepository.saveAndFlush(todo);
TaskTodo taskTodo = new TaskTodo(task, todo);
taskTodoRepository.save(taskTodo);
}
I managed to figure out what is the problem我设法弄清楚是什么问题
Service function should look like this:服务功能应该是这样的:
@Override
public void saveTodo(Todo todo, Long taskId) {
Task task = getTaskById(taskId);
todo = todoRepository.saveAndFlush(todo); // This line is changed
TaskTodo taskTodo = new TaskTodo(task, todo);
taskTodoRepository.save(taskTodo);
}
Reason is because Todo
which was sent as parameter did not have id in itself and because of that there was an error.原因是因为作为参数发送的
Todo
本身没有 id,因此出现了错误。
When I put todo = todoRepository.saveAndFlush(todo);
当我把
todo = todoRepository.saveAndFlush(todo);
, saveAndFlush
returned object which had id in itself. ,
saveAndFlush
返回本身具有 id 的对象。 Because of that TaskTodo
could be saved.因为可以保存
TaskTodo
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.