From b2815fd417771f446662c7f7a83cc9dc81094093 Mon Sep 17 00:00:00 2001 From: Nikita Sergeev Date: Fri, 17 Mar 2023 19:34:38 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ip/labwork/shop/model/Component.java | 6 ++++-- src/main/java/ip/labwork/shop/model/Order.java | 2 ++ .../java/ip/labwork/shop/model/OrderProducts.java | 2 ++ src/main/java/ip/labwork/shop/model/Product.java | 5 ++++- .../java/ip/labwork/shop/model/ProductComponents.java | 2 ++ .../ip/labwork/shop/service/ComponentService.java | 11 +++++++++++ .../java/ip/labwork/shop/service/ProductService.java | 9 +++++++++ 7 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/main/java/ip/labwork/shop/model/Component.java b/src/main/java/ip/labwork/shop/model/Component.java index 2107ebd..002eefc 100644 --- a/src/main/java/ip/labwork/shop/model/Component.java +++ b/src/main/java/ip/labwork/shop/model/Component.java @@ -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 products; diff --git a/src/main/java/ip/labwork/shop/model/Order.java b/src/main/java/ip/labwork/shop/model/Order.java index 2d5f8bc..fdaf109 100644 --- a/src/main/java/ip/labwork/shop/model/Order.java +++ b/src/main/java/ip/labwork/shop/model/Order.java @@ -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 products; diff --git a/src/main/java/ip/labwork/shop/model/OrderProducts.java b/src/main/java/ip/labwork/shop/model/OrderProducts.java index 37daa79..ef18682 100644 --- a/src/main/java/ip/labwork/shop/model/OrderProducts.java +++ b/src/main/java/ip/labwork/shop/model/OrderProducts.java @@ -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() { diff --git a/src/main/java/ip/labwork/shop/model/Product.java b/src/main/java/ip/labwork/shop/model/Product.java index 803f45b..c545398 100644 --- a/src/main/java/ip/labwork/shop/model/Product.java +++ b/src/main/java/ip/labwork/shop/model/Product.java @@ -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 components; - @OneToMany(mappedBy = "product", cascade = CascadeType.ALL) + @OneToMany(mappedBy = "product", cascade = CascadeType.ALL, fetch = FetchType.EAGER) @JsonIgnore private List orders; diff --git a/src/main/java/ip/labwork/shop/model/ProductComponents.java b/src/main/java/ip/labwork/shop/model/ProductComponents.java index 55f8f8b..715824b 100644 --- a/src/main/java/ip/labwork/shop/model/ProductComponents.java +++ b/src/main/java/ip/labwork/shop/model/ProductComponents.java @@ -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() { diff --git a/src/main/java/ip/labwork/shop/service/ComponentService.java b/src/main/java/ip/labwork/shop/service/ComponentService.java index f218099..3a3e305 100644 --- a/src/main/java/ip/labwork/shop/service/ComponentService.java +++ b/src/main/java/ip/labwork/shop/service/ComponentService.java @@ -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(); } } diff --git a/src/main/java/ip/labwork/shop/service/ProductService.java b/src/main/java/ip/labwork/shop/service/ProductService.java index 046c742..15e4a52 100644 --- a/src/main/java/ip/labwork/shop/service/ProductService.java +++ b/src/main/java/ip/labwork/shop/service/ProductService.java @@ -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(); }