Search code examples
hibernatejpamany-to-many

How could remove only the product from the client's cart?


I would like to remove a supplier, however when I do so, the clients who have this product in their cart are removed as well, how could I change this and remove only the product from the client's cart?

Here is the BBDD design: enter image description here

I've tried with orphansRemoval but in @ManyToMany associations is not possible.

Supplier.java

package es.upm.dit.apsv.gatashop.model;

import java.io.Serializable;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

//AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
@Entity
@Table(name="SUPPLIERS")
public class Supplier implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id //@GeneratedValue(strategy= GenerationType.AUTO)
    private Long id;
    private String name;
    private String contactName;
    private String email;
    private String password;
    private String phone;
    private String country;
    private String city;
    private String address;
    private String postalCode;
    
    @OneToMany(mappedBy = "supplier", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval=true)
    private List<Product> suppliedProducts;

    //Constructor//
    public Supplier() {
        super();
    }

Product.java

package es.upm.dit.apsv.gatashop.model;

import java.io.Serializable;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
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 javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="PRODUCTS")
public class Product implements Serializable{
    
    private static final long serialVersionUID = 1L;

    @Id //@GeneratedValue(strategy= GenerationType.AUTO)
    private Long id;
    private String name;
    private String unit;
    private double price;
    private boolean available;
        
    @ManyToOne (cascade = CascadeType.ALL)
    @JoinColumn(name = "suppliedProducts", insertable=true, updatable=false)
    private Supplier supplier;

    @OneToMany(mappedBy = "product", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private List<OrderDetail> orderDetails;
    
    @ManyToMany
    @JoinColumn(name = "productsAssociated", insertable=true, updatable=false)
    private List<Category> categories;
    
    @ManyToMany(mappedBy = "cart", fetch = FetchType.EAGER, cascade = {     
            CascadeType.DETACH,
            CascadeType.MERGE,
            CascadeType.PERSIST,
            CascadeType.REFRESH
    })
    private List<Client> customers;
    
    //Constructor//
    public Product() {
        super();
    }

Client

package es.upm.dit.apsv.gatashop.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="CLIENTS")
public class Client implements Serializable{
    
    private static final long serialVersionUID = 1L;
    
    @Id //@GeneratedValue(strategy= GenerationType.AUTO)
    private Long id;
    private String email;
    private String user;
    private String password;
    private String country;
    private String city;
    private String address;
    private String postalCode;
    
    @OneToMany(mappedBy = "client", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private List<Order> orders;
    
    @ManyToMany (fetch = FetchType.EAGER, cascade = {       
                        CascadeType.DETACH,
                        CascadeType.MERGE,
                        CascadeType.PERSIST,
                        CascadeType.REFRESH
                })
    private List<Product> cart;
    
    
    //Constructor//
    public Client() {
        super();
    }

Solution

  • I've solved doing manually, following the advices of https://thorben-janssen.com/hibernate-tips-the-best-way-to-remove-entities-from-a-many-to-many-association/#1_Use_a_Set_instead_of_a_List