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
– Erick Luz
what is the relationship of the tables? 1:n, 1:1 or n:n, this influences the use of collection (List) or not.
– Erick Luz
Please edit your question and enter the information
– Erick Luz
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.
– Erick Luz
<modelVersion>4.0.0</modelVersion>
 <parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>2.2.2.RELEASE</version>
 <relativePath/> <!-- lookup parent from repository -->
 </parent>
– cazzruan
I downloaded the latest version of https://spring.io/tools for windows
– cazzruan