我可以在购物车,代表,产品,sku和CARTS_SKU(即@JoinTable创建的第三个表)中一次正确插入记录,但在删除购物车表中的记录时,它会删除所有记录一次相关的表格。
Carts.java
package com.xptraining.model;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
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.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "carts")
public class Carts {
public static final String CARTS_ID= "carts_id";
public static final String PRODUCT_ID= "product_id";
public static final String SKU_ID= "sku_id";
public static final String REPRESENTATIVE_ID = "representative_id";
public static final String QUANTITY= "quantity";
public static final String PRIZE = "prize";
@Id
@GeneratedValue (strategy=GenerationType.AUTO)
@Column(name = CARTS_ID)
private long cartId;
@Column(name = PRIZE)
private int prize;
@ElementCollection(targetClass=Product.class)
@OneToMany(cascade = CascadeType.ALL , fetch=FetchType.EAGER ,orphanRemoval=true)
@JoinTable(
name = "CARTS_SKU",
joinColumns = { @JoinColumn(name = CARTS_ID) },
inverseJoinColumns = { @JoinColumn(name = SKU_ID) }
)
private List<Sku> listSku;
public List<Sku> getListSku() {
return listSku;
}
public void setListSku(List<Sku> listSku) {
this.listSku = listSku;
}
@OneToOne(cascade=CascadeType.ALL ,fetch=FetchType.EAGER)
@JoinColumn(name=REPRESENTATIVE_ID)
private Representative representative;
public Representative getRepresentative() {
return representative;
}
public void setRepresentative(Representative representative) {
this.representative = representative;
}
public long getCartId() {
return cartId;
}
public void setCartId(long cartId) {
this.cartId = cartId;
}
public int getPrize() {
return prize;
}
public void setPrize(int prize) {
this.prize = prize;
}
}
Representative.java
package com.xptraining.model;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIgnore;
/**
* The Representative Entity Class
*
* @author Harshad Kenjale
*
*/
@Entity
@Table(name = Representative.TABLE_NAME)
public class Representative implements java.io.Serializable {
private static final long serialVersionUID = 1L;
public static final String TABLE_NAME = "representative";
public static final String REPRESENTATIVE_ID = "representative_id";
public static final String FIRST_NAME = "representative_first_name";
public static final String LAST_NAME = "representative_last_name";
public static final String EMAIL = "representative_email";
public static final String PASSWORD = "representative_password";
public static final String EMPID = "representative_emp_id";
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = REPRESENTATIVE_ID)
private long representativeId;
@Column(name = FIRST_NAME)
private String representativeFirstName;
@Column(name = LAST_NAME)
private String representativeLastName;
@Column(name = EMAIL)
private String representativeEmail;
@Column(name = PASSWORD)
private String representativePassword;
@Column(name = EMPID)
private int representativeEmpId;
@JsonIgnore
@OneToOne(mappedBy="representative", cascade=CascadeType.ALL , fetch=FetchType.EAGER)
private Orders orders;
public Orders getOrders() {
return orders;
}
public void setOrders(Orders orders) {
this.orders = orders;
}
@JsonIgnore
@OneToOne(mappedBy="representative", cascade=CascadeType.ALL , fetch=FetchType.EAGER)
private Carts carts;
public Carts getCarts() {
return carts;
}
public void setCarts(Carts carts) {
this.carts = carts;
}
public long getRepresentativeId() {
return representativeId;
}
public void setRepresentativeId(long representativeId) {
this.representativeId = representativeId;
}
public String getRepresentativeFirstName() {
return representativeFirstName;
}
public void setRepresentativeFirstName(String representativeFirstName) {
this.representativeFirstName = representativeFirstName;
}
public String getRepresentativeLastName() {
return representativeLastName;
}
public void setRepresentativeLastName(String representativeLastName) {
this.representativeLastName = representativeLastName;
}
public String getRepresentativeEmail() {
return representativeEmail;
}
public void setRepresentativeEmail(String representativeEmail) {
this.representativeEmail = representativeEmail;
}
public String getRepresentativePassword() {
return representativePassword;
}
public void setRepresentativePassword(String representativePassword) {
this.representativePassword = representativePassword;
}
public int getRepresentativeEmpId() {
return representativeEmpId;
}
public void setRepresentativeEmpId(int representativeEmpId) {
this.representativeEmpId = representativeEmpId;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
Product.java
package com.xptraining.model;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
/**
* The Product Entity Class
*
* @author Harshad Kenjale
*
*/
@Entity
@Table(name = Product.TABLE_NAME)
public class Product implements java.io.Serializable {
private static final long serialVersionUID = 1L;
public static final String TABLE_NAME = "product";
public static final String PRODUCT_ID= "product_id";
public static final String PRODUCT_NAME = "product_name";
public static final String SPECIALITY_ID = "spc_id";
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = PRODUCT_ID)
private long productId;
@Column(name = PRODUCT_NAME )
private String productName;
@Column(name = SPECIALITY_ID)
private int specialtyId;
@LazyCollection(LazyCollectionOption.FALSE)
@ElementCollection(targetClass=Product.class)
@OneToMany(mappedBy="product" , cascade=CascadeType.ALL)
private List<Sku> listSkuOrders = new ArrayList<Sku>();
public long getProductid() {
return productId;
}
public void setProductid(long productid) {
this.productId = productid;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public int getSpecialtyId() {
return specialtyId;
}
public void setSpecialtyId(int specialtyId) {
this.specialtyId = specialtyId;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
Sku.java
package com.xptraining.model;
import javax.persistence.CascadeType;
import javax.persistence.Column;
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.ManyToOne;
import javax.persistence.Table;
/**
* The SKU Entity Class
*
* @author Harshad Kenjale
*
*/
@Entity
@Table(name = Sku.TABLE_NAME)
public class Sku implements java.io.Serializable {
private static final long serialVersionUID = 1L;
public static final String TABLE_NAME = "sku";
public static final String SKU_ID= "sku_id";
public static final String SKU_NAME = "sku_name";
public static final String UNIT_NAME = "unit_name";
public static final String SIZE = "size";
public static final String PRIZE = "prize";
public static final String PRODUCT_ID= "product_id";
public static final String ORDER_ID= "orders_id";
public static final String QUANTITY= "quantity";
@Id
@GeneratedValue (strategy=GenerationType.AUTO)
@Column(name = SKU_ID)
private long skuId;
@Column(name = SKU_NAME)
private String skuName;
@Column(name = UNIT_NAME)
private String unitName;
@Column(name = SIZE)
private String size;
@Column(name = PRIZE)
private int prize;
@Column(name = QUANTITY)
private int quanity;
@ManyToOne(fetch = FetchType.EAGER , cascade=CascadeType.ALL)
@JoinColumn(name = PRODUCT_ID , nullable = false)
private Product product;
public long getSkuId() {
return skuId;
}
public void setSkuId(long skuId) {
this.skuId = skuId;
}
public String getSkuName() {
return skuName;
}
public void setSkuName(String skuName) {
this.skuName = skuName;
}
public String getUnitName() {
return unitName;
}
public void setUnitName(String unitName) {
this.unitName = unitName;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public int getPrize() {
return prize;
}
public void setPrize(int prize) {
this.prize = prize;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
public int getQuanity() {
return quanity;
}
public void setQuanity(int quanity) {
this.quanity = quanity;
}
}
另外还有另一张表CARTS_SKU
@JoinTable(
name = "CARTS_SKU",
joinColumns = { @JoinColumn(name = CARTS_ID) },
inverseJoinColumns = { @JoinColumn(name = SKU_ID) }
)
当我通过cart_id删除购物车时,我想仅从购物车表和CARTS_SKU表中删除记录,但它删除了与cart_id相关的所有记录。
答案 0 :(得分:0)
我得到了上述问题的答案。我从所有子表中删除cascade=cascadeType.all
,使用此表可以避免对该表执行删除操作。
答案 1 :(得分:0)
CascadeType.ALL的含义是持久性将所有EntityManager操作(PERSIST,REMOVE,REFRESH,MERGE,DETACH)传播(级联)到相关实体。