0
Hello, I’m having difficulties in the process of deleting a record that has relationship @ManyToMany. When I delete the record referring to the Product entity with the annotation CascadeType.ALL, it excludes all records from the intermediate table (Categorization), and not only the records referring to the deleted product.

What I want is that when deleting a product it also deletes its reference records in the table Categorization.
Follow the classes:
Class Category:
package com.eclodir.voucomprei.domain;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import com.fasterxml.jackson.annotation.JsonIgnore;
@Entity
public class Categoria implements Serializable{
@Id
@GeneratedValue (strategy=GenerationType.IDENTITY)
private Long id;
private String descricao;
private String foto;
@ManyToMany
@JoinTable (name="Categorizacao", joinColumns=@JoinColumn(name="categoria_id"),inverseJoinColumns=@JoinColumn(name="produto_id"))
private List<Produto> produtos = new ArrayList<>();
public Categoria() {}
public Categoria(Long id, String descricao, String foto) {
super();
this.id = id;
this.descricao = descricao;
this.foto = foto;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Categoria other = (Categoria) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getDescricao() {
return descricao;
}
public void setDescricao(String descricao) {
this.descricao = descricao;
}
public List<Produto> getProdutos() {
return produtos;
}
public void setProdutos(List<Produto> produtos) {
this.produtos = produtos;
}
public String getFoto() {
return foto;
}
public void setFoto(String foto) {
this.foto = foto;
}
}
Product class:
package com.eclodir.voucomprei.domain;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import com.eclodir.voucomprei.domain.enums.Unidade;
import com.fasterxml.jackson.annotation.JsonIgnore;
@Entity
public class Produto implements Serializable {
@Id
@GeneratedValue (strategy=GenerationType.IDENTITY)
private Long id;
private String descricao;
private String foto;
private String fabricante;
@Enumerated
private Unidade und;
@JsonIgnore
@ManyToMany (mappedBy="produtos", cascade=CascadeType.ALL)
private List<Categoria> categorias = new ArrayList<>();
public Produto() {}
public Produto(Long id, String descricao, String foto, String fabricante, Unidade und) {
super();
this.id = id;
this.descricao = descricao;
this.foto = foto;
this.fabricante = fabricante;
this.und = und;
this.categorias = categorias;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Produto other = (Produto) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getDescricao() {
return descricao;
}
public void setDescricao(String descricao) {
this.descricao = descricao;
}
public Unidade getUnd() {
return und;
}
public void setUnd(Unidade und) {
this.und = und;
}
public String getFabricante() {
return fabricante;
}
public void setFabricante(String fabricante) {
this.fabricante = fabricante;
}
public String getFoto() {
return foto;
}
public void setFoto(String foto) {
this.foto = foto;
}
public List<Categoria> getCategorias() {
return categorias;
}
public void setCategorias(List<Categoria> categorias) {
this.categorias = categorias;
}
}
Enter the code of the classes Category, Product, and Categoricacao.
– Giuliana Bezerra
Jose, could you paste the code so I can see how the mapping of the entity?
– Bruno Spy
Hello, follow the class codes.
– Gonzaga Neto