hibernate - 怎么能只从客户的购物车中删除产品?
问题描述
我想删除一个供应商,但是当我这样做时,在他们的购物车中有这个产品的客户也被删除了,我怎么能改变这个并且只从客户的购物车中删除产品?
我已经尝试过 orphansRemoval 但在 @ManyToMany 关联中是不可能的。
供应商.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();
}
产品.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();
}
客户
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();
}
解决方案
推荐阅读
- postgresql - 用于 BETWEEN 和 ORDER BY 的 Postgresql 多列索引
- php - 使 SQL 查询仅选择 WHERE 条件之一
- e-commerce - 在 Kentico 中以编程方式注入翻译产品时出错,我做得对吗?
- css - 我在将 Css 文件链接到 Django 模板时遇到问题
- python - 自定义词形还原词并附加到 WordNetLemmatizer
- sql - 尝试使用 dplyr 在 R 中的 SQL 数据库中选择/过滤数据
- mysql - 如何在 mysql 中使用一个选择查询在两列之间切换值?
- symfony - Symfony Doctrine SortBy ToMany 关系对象
- html - 如何使用 XPATH 在跨度类下创建所有文本的连接字符串
- java - 升级 Android Firebase 库会导致崩溃