ManyToMany JPA - Product Category

0

Hello, I'm having trouble deleting a record that has @ManyToMany relationship. When I delete the record for the Product entity with the CascadeType.ALL annotation, it deletes all the records in the middle table (Categorization), not just the records for the deleted product.

WhatIwantisthatwhendeletingaproduct,italsodeletesitsreferencerecordsintheCategorizationtable.

Herearetheclasses:

ClassCategory:

packagecom.eclodir.voucomprei.domain;importjava.io.Serializable;importjava.util.ArrayList;importjava.util.List;importjavax.persistence.Entity;importjavax.persistence.GeneratedValue;importjavax.persistence.GenerationType;importjavax.persistence.Id;importjavax.persistence.JoinColumn;importjavax.persistence.JoinTable;importjavax.persistence.ManyToMany;importcom.fasterxml.jackson.annotation.JsonIgnore;@EntitypublicclassCategoriaimplementsSerializable{@Id@GeneratedValue(strategy=GenerationType.IDENTITY)privateLongid;privateStringdescricao;privateStringfoto;@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;
    }
}
    
asked by anonymous 22.03.2018 / 19:05

1 answer

0

I was able to solve it, I had implemented the part that inserts the categories in the products, but I had not implemented the opposite, inserting the products in the category, somehow I was generating error in the relationship because it was ManyToMany, after the correction I got delete normally using CascadeType.ALL.

    
24.03.2018 / 05:42