Удаление работает
This commit is contained in:
parent
8c516e69f1
commit
b2815fd417
@ -8,15 +8,17 @@ import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
@Table(name = "components")
|
||||
public class Component {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
@Column()
|
||||
@Column(name = "name")
|
||||
private String componentName;
|
||||
@Column(name = "price")
|
||||
private Integer price;
|
||||
|
||||
@OneToMany(mappedBy = "component", cascade = CascadeType.ALL)
|
||||
@OneToMany(mappedBy = "component", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
||||
@JsonIgnore
|
||||
private List<ProductComponents> products;
|
||||
|
||||
|
@ -13,7 +13,9 @@ public class Order {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
@Column(name = "date")
|
||||
private Date date;
|
||||
@Column(name = "price")
|
||||
private Integer price;
|
||||
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
||||
private List<OrderProducts> products;
|
||||
|
@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "order_product")
|
||||
public class OrderProducts {
|
||||
@EmbeddedId
|
||||
private OrderProductsKey id;
|
||||
@ -16,6 +17,7 @@ public class OrderProducts {
|
||||
@JoinColumn(name = "order_id")
|
||||
@JsonIgnore
|
||||
private Order order;
|
||||
@Column(name = "count")
|
||||
private Integer count;
|
||||
|
||||
public OrderProducts() {
|
||||
|
@ -8,17 +8,20 @@ import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
@Table(name = "products")
|
||||
public class Product {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
@Column(name = "name")
|
||||
private String productName;
|
||||
@Column(name = "price")
|
||||
private Integer price;
|
||||
|
||||
@OneToMany(mappedBy = "product", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
||||
private List<ProductComponents> components;
|
||||
|
||||
@OneToMany(mappedBy = "product", cascade = CascadeType.ALL)
|
||||
@OneToMany(mappedBy = "product", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
||||
@JsonIgnore
|
||||
private List<OrderProducts> orders;
|
||||
|
||||
|
@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "product_component")
|
||||
public class ProductComponents {
|
||||
@EmbeddedId
|
||||
private ProductComponentsKey id;
|
||||
@ -16,6 +17,7 @@ public class ProductComponents {
|
||||
@JoinColumn(name = "product_id")
|
||||
@JsonIgnore
|
||||
private Product product;
|
||||
@Column(name = "count")
|
||||
private Integer count;
|
||||
|
||||
public ProductComponents() {
|
||||
|
@ -1,6 +1,9 @@
|
||||
package ip.labwork.shop.service;
|
||||
|
||||
import ip.labwork.shop.model.Component;
|
||||
import ip.labwork.shop.model.OrderProducts;
|
||||
import ip.labwork.shop.model.Product;
|
||||
import ip.labwork.shop.model.ProductComponents;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
@ -67,11 +70,19 @@ public class ComponentService {
|
||||
@Transactional
|
||||
public Component deleteComponent(Long id) {
|
||||
final Component currentComponent = findComponent(id);
|
||||
int size = currentComponent.getProducts().size();
|
||||
for (int i = 0; i < size; i++) {
|
||||
ProductComponents temp = currentComponent.getProducts().get(0);
|
||||
temp.getComponent().removeProduct(temp);
|
||||
temp.getProduct().removeComponent(temp);
|
||||
em.remove(temp);
|
||||
}
|
||||
em.remove(currentComponent);
|
||||
return currentComponent;
|
||||
}
|
||||
@Transactional
|
||||
public void deleteAllComponent() {
|
||||
em.createQuery("delete from ProductComponents").executeUpdate();
|
||||
em.createQuery("delete from Component").executeUpdate();
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package ip.labwork.shop.service;
|
||||
|
||||
import ip.labwork.shop.model.Component;
|
||||
import ip.labwork.shop.model.OrderProducts;
|
||||
import ip.labwork.shop.model.Product;
|
||||
import ip.labwork.shop.model.ProductComponents;
|
||||
import jakarta.persistence.EntityManager;
|
||||
@ -96,6 +97,13 @@ public class ProductService {
|
||||
temp.getProduct().removeComponent(temp);
|
||||
em.remove(temp);
|
||||
}
|
||||
int ordSize = currentProduct.getOrders().size();
|
||||
for (int i = 0; i < ordSize; i++){
|
||||
OrderProducts temp = currentProduct.getOrders().get(0);
|
||||
temp.getProduct().removeOrder(temp);
|
||||
temp.getOrder().removeProducts(temp);
|
||||
em.remove(temp);
|
||||
}
|
||||
em.remove(currentProduct);
|
||||
return currentProduct;
|
||||
}
|
||||
@ -103,6 +111,7 @@ public class ProductService {
|
||||
@Transactional
|
||||
public void deleteAllProduct() {
|
||||
em.createQuery("delete from ProductComponents").executeUpdate();
|
||||
em.createQuery("delete from OrderProducts ").executeUpdate();
|
||||
em.createQuery("delete from Product").executeUpdate();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user