[英]getting java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails
[英]SpringBoot App throws ava.sql.SQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails
我正在使用 Spring Boot 做一個小應用程序,當我嘗試刪除一個類別並設置 onDeleteCascade 時拋出此錯誤,我正在搜索信息,但我發現的所有內容都不能解決我的問題,如果有人可以幫助我,我會不勝感激。
這是錯誤的一部分:
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]
這是一些代碼:
實體板
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;
}
}
實體類別
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;
}
}
嘗試這個:
@OneToMany(mappedBy = "categoria")
private List<Categoria> categoria;
@ManyToOne(optional = false, fetch = FetchType.EAGER)
private Categoria categoria;
在我的代碼中,我沒有指定級聯,所有操作都在工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.