Manytomany JPA - Product Category

Asked

Viewed 338 times

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. inserir a descrição da imagem aqui

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;
    }
}
  • 1

    Enter the code of the classes Category, Product, and Categoricacao.

  • Jose, could you paste the code so I can see how the mapping of the entity?

  • Hello, follow the class codes.

1 answer

0


I managed to resolve, I had implemented the part that inserts the categories in the products, but had not implemented the opposite, insert the products in the category, somehow was generating error in the relationship by treatingif from Manytomany, after correction I was able to exclude normally using Cascadetype.ALL.

Browser other questions tagged

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