Я хочу сценарий, в котором, если я удаляю Продукт, удаляются только продукты И если я удаляю Order, то только Order должен быть удален. Я могу добиться этого сценария с помощью следующего кода. Но сейчас, когда я размещаю заказ на несколько продуктов. Это показывает мне ошибку
** org.h2.jdb c .JdbcSQLIntegrityConstraintViolationException: уникальный индекс или нарушение первичного ключа: "PUBLI C .PRIMARY_KEY_7 ON PUBLI C .ORDER_PRODUCTS (ORDER_ID, PID" VAL) VAL ; SQL оператор: **
//Order Class
package com.mainApp.model;
@Entity
public class Orders {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
int order_id;
int total_Amount;
boolean adminconfirmation;
@Temporal(TemporalType.TIMESTAMP)
Date order_date;
boolean order_status;
public boolean isAdminconfirmation() {
return adminconfirmation;
}
public void setAdminconfirmation(boolean adminconfirmation) {
this.adminconfirmation = adminconfirmation;
}
public int getTotal_Amount() {
return total_Amount;
}
public void setTotal_Amount(int total_Amount) {
this.total_Amount = total_Amount;
}
public Orders() {
super();
//Setting status :rejected from beginning
order_status=false;
adminconfirmation=false;
//System.out.println("Order_Status and Admin_confirmation set to False");
}
@ManyToOne(fetch=FetchType.LAZY, cascade= {CascadeType.DETACH,CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH})
@JoinColumn(name="customer_id")
Customer customer;
//An order can contain multiple products.
@ManyToMany(cascade= {CascadeType.DETACH,CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH})
@JoinTable(name="order_products",
joinColumns= {@JoinColumn(name="order_id")}, inverseJoinColumns= {@JoinColumn(name="pid")})
Set<Product> products=new HashSet<>();
public Set<Product> getProducts() {
return products;
}
public void setProducts(Set<Product> products) {
this.products = products;
}
public int getOrder_id() {
return order_id;
}
public void setOrder_id(int order_id) {
this.order_id = order_id;
}
public Date getOrder_date() {
return order_date;
}
public void setOrder_date(Date order_date) {
this.order_date = order_date;
}
public boolean isOrder_status() {
return order_status;
}
public void setOrder_status(boolean order_status) {
this.order_status = order_status;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
@Override
public String toString() {
return "Orders [order_id=" + order_id + ", total_Amount=" + total_Amount + ", adminconfirmation="
+ adminconfirmation + ", order_date=" + order_date + ", order_status=" + order_status + ", customer="
+ customer + ", products=" + products + "]";
}
public void addProduct(Product p) {
this.products.add(p);
p.getOrders().add(this);
}
public void removeProduct(Product p) {
this.products.remove(p);
p.getOrders().remove(this);
}
}
//Product Class
@Entity
public class Product {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
int pid;
@NotBlank(message="Required")
String pname;
int pprice;
int pqty;
String pcategory;
//@ManyToMany(mappedBy="products",cascade= {CascadeType.DETACH,CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH})
@ManyToMany(cascade= {CascadeType.DETACH,CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH})
@JoinTable(name="order_products",
joinColumns= {@JoinColumn(name="pid")}, inverseJoinColumns= {@JoinColumn(name="order_id")})
Set<Orders> orders=new HashSet<>();
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public int getPprice() {
return pprice;
}
public void setPprice(int pprice) {
this.pprice = pprice;
}
public int getPqty() {
return pqty;
}
public void setPqty(int pqty) {
this.pqty = pqty;
}
public String getPcategory() {
return pcategory;
}
public Set<Orders> getOrders() {
return orders;
}
public void setOrders(Set<Orders> orders) {
this.orders = orders;
}
public void setPcategory(String pcategory) {
this.pcategory = pcategory;
}
@Override
public String toString() {
return "Product [pid=" + pid + ", pname=" + pname + ", pprice=" + pprice + ", pqty=" + pqty + ", pcategory="
+ pcategory + "]";
}
}