Entity relationship error 1:N using orphanRemoval

Asked

Viewed 136 times

0

I am trying to update a record in the database through a REST service with the help of Postman, however is returning me an error:

"A collection with cascade=\"all-delete-orphan\" was no longer referenced by the owning entity instance: spring.restful.model.Pessoa.telefones; nested exception is org.hibernate.HibernateException: A collection with cascade=\"all-delete-orphan\" was no longer referenced by the owning entity instance: spring.restful.model.Pessoa.telefones",

Follow the code below ENDPOINT:

@PutMapping("/{cpf}")
public ResponseEntity<Pessoa> atualizar(@PathVariable String cpf, @RequestBody Pessoa pessoa){
    Pessoa existente = pessoas.findById(cpf).get();
    if(existente == null) {
        return ResponseEntity.notFound().build();
    }
    BeanUtils.copyProperties(pessoa , existente, "cpf");
    pessoas.save(existente);
    return ResponseEntity.ok(existente);
}

Class Person:

@Entity
public class Pessoa implements Serializable {

@Id
private String cpf;

private String nome;

@OneToMany(mappedBy="pessoa", orphanRemoval = true, cascade = CascadeType.ALL)
private List<Telefone> telefones = new ArrayList<Telefone>();

public String getNome() {
    return nome;
}

public void setNome(String nome) {
    this.nome = nome;
}

public String getCpf() {
    return cpf;
}

public void setCpf(String cpf) {
    this.cpf = cpf;
}

public List<Telefone> getTelefones() {
    return telefones;
}

public void setTelefones(List<Telefone> telefones) {
    this.telefones = telefones;
}

Class Telephone:

@Entity
public class Telefone implements Serializable {

@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Long id;

private String numero;

@JsonIgnore
@ForeignKey(name="pessoa_cpf")
@ManyToOne(optional = false, cascade = javax.persistence.CascadeType.ALL)
private Pessoa pessoa;

public Long getId() {
    return id;
}

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

public String getNumero() {
    return numero;
}

public void setNumero(String numero) {
    this.numero = numero;
}

public Pessoa getPessoa() {
    return pessoa;
}

public void setPessoa(Pessoa pessoa) {
    this.pessoa = pessoa;
}   

NOTE: Without using the attribute of type List<> the method BeanUtils.copyProperties works properly without any error.

How can I solve this problem?

  • Your problem is in the DAO layer, well where the JPA (Hibernate) annotations were made, the error must be describing which entity the problem is

  • what is the relationship of the tables? 1:n, 1:1 or n:n, this influences the use of collection (List) or not.

  • Please edit your question and enter the information

  • Which version of the spring-boot framework are you using? I posted an answer, if it doesn’t work answers here that I tidy or delete.

  • <modelVersion>4.0.0</modelVersion>&#xA; <parent>&#xA; <groupId>org.springframework.boot</groupId>&#xA; <artifactId>spring-boot-starter-parent</artifactId>&#xA; <version>2.2.2.RELEASE</version>&#xA; <relativePath/> <!-- lookup parent from repository -->&#xA; </parent>

  • I downloaded the latest version of https://spring.io/tools for windows

Show 1 more comment

1 answer

1

Try changing the annotation @ForeignKey(name="pessoa_cpf") for @JoinColumn(name="pessoa_cpf"), and remove the recursive cascade configuration , cascade = javax.persistence.CascadeType.ALL telephone class.

@Entity
public class Telefone implements Serializable {

@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Long id;

private String numero;

@JsonIgnore
@JoinColumn(name="pessoa_cpf")
@ManyToOne(optional = false)
private Pessoa pessoa;

public Long getId() {
    return id;
}

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

public String getNumero() {
    return numero;
}

public void setNumero(String numero) {
    this.numero = numero;
}

public Pessoa getPessoa() {
    return pessoa;
}

public void setPessoa(Pessoa pessoa) {
    this.pessoa = pessoa;
}   
  • the error persists!

  • I made a change in the reply

  • still remains the same mistake

Browser other questions tagged

You are not signed in. Login or sign up in order to post.