Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
672 views
in Technique[技术] by (71.8m points)

java - Cannot delete or update a parent row a foreign key constraint fails JPA Spring Boot

I have a request table related to 2 tables where I save the request number according to the type of request that can be a request for water analysis and request for soil analysis, but when I try to delete or update the request table I get the error

Cannot delete or update a parent row: a foreign key constraint fails

My code that implements the relationships is the following is the following

//class for request

@Entity
@Table(name = "solicitud")
public class Solicitud implements Serializable{

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;
    
    //@Column(unique=true)
    //private String codigo;
    
    @ManyToOne(fetch = FetchType.LAZY)
    private Estado estado;
    
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @Temporal(TemporalType.DATE)
    @NotNull
    @Column(name="fecha")
    private Date fecha;
    
    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    private Usuario usuario;
    
    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    private Usuario teclab;
    
    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    private TipoMuestra tmuestra;
    
    
    
    //@Min(value = 0L, message = "Debe ingresar un valor positivo")
    //@Pattern(regexp = "[\s]*[0-9]*[1-9]+",message="msg")
    @NotNull
    private Integer numMuestras;
    
    int year = 0;
    
    
    
    public Long getId() {
        return id;
    }



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



    public Estado getEstado() {
        return estado;
    }



    public void setEstado(Estado estado) {
        this.estado = estado;
    }



    public Date getFecha() {
        return fecha;
    }



    public void setFecha(Date fecha) {
        this.fecha = fecha;
    }
    
    


    public Usuario getUsuario() {
        return usuario;
    }



    public void setUsuario(Usuario usuario) {
        this.usuario = usuario;
    }




    public TipoMuestra getTmuestra() {
        return tmuestra;
    }



    public void setTmuestra(TipoMuestra tmuestra) {
        this.tmuestra = tmuestra;
    }
    
    public Integer getNumMuestras() {
        return numMuestras;
    }



    public void setNumMuestras(Integer numMuestras) {
        this.numMuestras = numMuestras;
    }




    public Usuario getTeclab() {
        return teclab;
    }



    public void setTeclab(Usuario teclab) {
        this.teclab = teclab;
    }
    
    





    /*@PostPersist
    public void generateCode() {
        CodigoAgua agua=new CodigoAgua();
        agua.setSolicitud(this);
        agua.generateCode();
    }*/






    /**
     * 
     */
    private static final long serialVersionUID = 1L;

}

//class for save number for type request water analysis

@Entity
@Table(name = "cagua")
public class CodigoAgua implements Serializable{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(unique=true)
    private String codigo;
    
    @OneToOne(fetch=FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name="solicitud_id")
    private Solicitud solicitud;
    
    int year = 0;
    
    
    
    public Long getId() {
        return id;
    }

    

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



    public String getCodigo() {
        return codigo;
    }



    public void setCodigo(String codigo) {
        this.codigo = codigo;
    }



    public Solicitud getSolicitud() {
        return solicitud;
    }



    public void setSolicitud(Solicitud solicitud) {
        this.solicitud = solicitud;
    }
    @PostPersist
    public void generateCode() {
        Date date = new Date();
        LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
          year = localDate.getYear();
          
        this.codigo=year +" - "+id+" - A";
        System.out.println("Codigo solicitud creado"+id+this.getSolicitud().getId());
    }

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

}

//class for save number for type request soil analysis

@Entity
@Table(name = "csuelo")
public class CodigoSuelo implements Serializable{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(unique=true)
    private String codigo;
    
    @OneToOne(fetch=FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name="solicitud_id")
    private Solicitud solicitud;
    
    int year = 0;
    
    
    
    
    public Long getId() {
        return id;
    }




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




    public String getCodigo() {
        return codigo;
    }




    public void setCodigo(String codigo) {
        this.codigo = codigo;
    }




    public Solicitud getSolicitud() {
        return solicitud;
    }




    public void setSolicitud(Solicitud solicitud) {
        this.solicitud = solicitud;
    }

    @PostPersist
    public void generateCode() {
        Date date = new Date();
        LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
          year = localDate.getYear();
          
        this.codigo=year +" - "+id+" - S";
        System.out.println("Codigo solicitud creado"+id+this.getSolicitud().getId());
    }


    /**
     * 
     */
    private static final long serialVersionUID = 1L;

}
question from:https://stackoverflow.com/questions/65886565/cannot-delete-or-update-a-parent-row-a-foreign-key-constraint-fails-jpa-spring-b

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This message indicates that you want to delete a row from table1, while its primary key is present as a foreign key in table2. To delete a record from table1, you must delete all the lines that refer to it in the other tables in order to be able to delete this record. I hope I've helped you


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...