2017-04-13 30 views
0

我能夠在購物車,代表,產品,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; 
    } 
} 

也有另一種表I E CARTS_SKU

@JoinTable(
      name = "CARTS_SKU", 
      joinColumns = { @JoinColumn(name = CARTS_ID) }, 
      inverseJoinColumns = { @JoinColumn(name = SKU_ID) } 
    ) 

當I M通過cart_id刪除車當時我想只能從車臺和CARTS_SKU表中刪除記錄,但它刪除,cart_id相關的所有記錄。

回答

0

我得到了上述問題的答案。我從所有子表中刪除cascade=cascadeType.all,通過使用這個我可以避免在該表上執行刪除操作。

0

CascadeType.ALL的含義是持久性將傳播(級聯)所有EntityManager操作(PERSIST,REMOVE,REFRESH,MERGE,DETACH)到相關實體。