简体   繁体   English

Spring Boot - java.sql.SQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束失败

[英]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 (连接TaskTodo表)。

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 , CONSTRAINT FK8dn0kderp1dame65xsokdaavj FOREIGN KEY ( todo_id ) REFERENCES todo ( id )) java.sql.SQLIntegrityConstraintViolationException:不能添加或更新子行,外键约束失败( todotask_todo ,约束FK8dn0kderp1dame65xsokdaavj外键( todo_id )参考todoid ))

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.

相关问题 获取 java.sql.SQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束失败 - getting java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails Hibernate:java.sql.SQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束失败 - Hibernate: java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails java.sql.SQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束失败 - java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails java.sql.SQLIntegrityConstraintViolationException春季启动 - java.sql.SQLIntegrityConstraintViolationException Spring boot SpringBoot App 抛出 ava.sql.SQLIntegrityConstraintViolationException:无法删除或更新父行:外键约束失败 - SpringBoot App throws ava.sql.SQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails java.sql.BatchUpdateException:无法添加或更新子行:外键约束失败ERROR - java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key constraint fails ERROR 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 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 Hibernate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM