简体   繁体   English

SpringBoot App 抛出 ava.sql.SQLIntegrityConstraintViolationException:无法删除或更新父行:外键约束失败

[英]SpringBoot App throws ava.sql.SQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails

I'm doing a little app with Spring Boot and this error is thrown when I try to delete a category and I putted onDeleteCascade, I'm searching information but everything I find doesn't fix my problem, if somebody could help me I would be very grateful.我正在使用 Spring Boot 做一个小应用程序,当我尝试删除一个类别并设置 onDeleteCascade 时抛出此错误,我正在搜索信息,但我发现的所有内容都不能解决我的问题,如果有人可以帮助我,我会不胜感激。

Here is some part of the error:这是错误的一部分:

java.sql.SQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`platos`.`plato`, CONSTRAINT `FKd7wg8wh3ov5moo4er9jj4vd8b` FOREIGN KEY (`id_cat`) REFERENCES `categoria` (`id`))
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:117) ~[mysql-connector-java-8.0.22.jar:8.0.22]
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.22.jar:8.0.22]

Here is some code:这是一些代码:

Entity Plate实体板

package cf.victorlopez.platosrestaurantespring.models.entity;
import javax.persistence.*;
import javax.validation.constraints.NotNull;

@Entity
@Table(name = "plato")
public class Plato {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) /** Para auto_increment **/
    @Column(name = "id", updatable = false, nullable = false)
    private Integer id;
    @Column(unique=true)
    private String nombre;
    @Column
    private String descripcion;
    @Column
    private String foto;
    @NotNull
    @JoinColumn(name = "id_cat",nullable = false)
    @ManyToOne(optional = false, fetch = FetchType.EAGER,cascade = CascadeType.REFRESH)
    private Categoria categoria;

    public Plato() {
        this.id = -1;
        this.foto = "";
        this.nombre="";
        this.descripcion="";
        this.categoria = new Categoria();
    }

    public Plato(String nombre, String descripcion, String foto, Categoria cat) {
        this.id = -1;
        this.nombre = nombre;
        this.descripcion = descripcion;
        this.foto = foto;
        this.categoria = cat;
    }

    public Integer getId() {
        return id;
    }

    /** No permitimos modificar el id desde fuera ya que es de tipo autoincrement */
    public void setId(int id) {
        this.id = id;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;

        Plato plato = (Plato) o;

        return id == plato.id;
    }

    @Override
    public int hashCode() {
        return id;
    }

    public String getDescripcion() {
        return descripcion;
    }

    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }

    public String getFoto() {
        return foto;
    }

    public void setFoto(String foto) {
        this.foto = foto;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Categoria getCategoria() {
        return categoria;
    }

    public void setCategoria(Categoria categoria) {
        this.categoria = categoria;
    }
}

Entity Category实体类别

package cf.victorlopez.platosrestaurantespring.models.entity;

import javax.persistence.*;
import java.util.List;

@Entity
@Table(name = "categoria")
public class Categoria {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) /** Para auto_increment **/
    @Column(name = "id", updatable = false, nullable = false)
    private Integer id;
    @Column(unique=true)
    private String nombre;
    @Column
    private String descripcion;
    @Column
    private String foto;
    @OneToMany(mappedBy = "categoria",cascade = CascadeType.REMOVE)
    private List<Categoria> categoria;

    public Categoria() {
        this.id = -1;
        this.foto = "";
        this.nombre="";
        this.descripcion="";
    }

    public Categoria(String nombre, String descripcion, String foto) {
        this.id = -1;
        this.nombre = nombre;
        this.descripcion = descripcion;
        this.foto = foto;
    }

    public Integer getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;

        Categoria plato = (Categoria) o;

        return id == plato.id;
    }

    @Override
    public int hashCode() {
        return id;
    }

    public String getDescripcion() {
        return descripcion;
    }

    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }

    public String getFoto() {
        return foto;
    }

    public void setFoto(String foto) {
        this.foto = foto;
    }

    public void setId(Integer id) {
        this.id = id;
    }
}

Try this:尝试这个:

@OneToMany(mappedBy = "categoria")
    private List<Categoria> categoria;
@ManyToOne(optional = false, fetch = FetchType.EAGER)
    private Categoria categoria;

In my code, I haven't specified cascade, all operation are working.在我的代码中,我没有指定级联,所有操作都在工作。

暂无
暂无

声明:本站的技术帖子网页,遵循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 在 SpringBoot 中从数据库中删除项目时出错:无法删除或更新父行:外键约束失败 - Error when deleting item from database in SpringBoot: Cannot delete or update a parent row: a foreign key constraint fails java.sql.SQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束失败 - java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails Spring Boot - java.sql.SQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束失败 - Spring Boot - java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails Hibernate 抛出无法删除或更新父行:外键约束失败 - Hibernate throws Cannot delete or update a parent row: a foreign key constraint fails Hibernate引发-无法删除或更新父行:外键约束失败 - Hibernate throws - Cannot delete or update a parent row: a foreign key constraint fails 休眠:无法删除或更新父行:外键约束失败 - Hibernate :Cannot delete or update a parent row: a foreign key constraint fails Hibernate集合映射问题。无法删除或更新父行:外键约束失败 - Hibernate collections mapping questions.Cannot delete or update a parent row: a foreign key constraint fails 无法删除或更新父行 外键约束失败 JPA Spring 引导 - Cannot delete or update a parent row a foreign key constraint fails JPA Spring Boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM