Удаление работает

This commit is contained in:
Nikita Sergeev 2023-03-17 19:34:38 +04:00
parent 8c516e69f1
commit b2815fd417
7 changed files with 34 additions and 3 deletions

View File

@ -8,15 +8,17 @@ import java.util.List;
import java.util.Objects; import java.util.Objects;
@Entity @Entity
@Table(name = "components")
public class Component { public class Component {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
private Long id; private Long id;
@Column() @Column(name = "name")
private String componentName; private String componentName;
@Column(name = "price")
private Integer price; private Integer price;
@OneToMany(mappedBy = "component", cascade = CascadeType.ALL) @OneToMany(mappedBy = "component", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JsonIgnore @JsonIgnore
private List<ProductComponents> products; private List<ProductComponents> products;

View File

@ -13,7 +13,9 @@ public class Order {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
private Long id; private Long id;
@Column(name = "date")
private Date date; private Date date;
@Column(name = "price")
private Integer price; private Integer price;
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL, fetch = FetchType.EAGER) @OneToMany(mappedBy = "order", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<OrderProducts> products; private List<OrderProducts> products;

View File

@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*; import jakarta.persistence.*;
@Entity @Entity
@Table(name = "order_product")
public class OrderProducts { public class OrderProducts {
@EmbeddedId @EmbeddedId
private OrderProductsKey id; private OrderProductsKey id;
@ -16,6 +17,7 @@ public class OrderProducts {
@JoinColumn(name = "order_id") @JoinColumn(name = "order_id")
@JsonIgnore @JsonIgnore
private Order order; private Order order;
@Column(name = "count")
private Integer count; private Integer count;
public OrderProducts() { public OrderProducts() {

View File

@ -8,17 +8,20 @@ import java.util.List;
import java.util.Objects; import java.util.Objects;
@Entity @Entity
@Table(name = "products")
public class Product { public class Product {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
private Long id; private Long id;
@Column(name = "name")
private String productName; private String productName;
@Column(name = "price")
private Integer price; private Integer price;
@OneToMany(mappedBy = "product", cascade = CascadeType.ALL, fetch = FetchType.EAGER) @OneToMany(mappedBy = "product", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<ProductComponents> components; private List<ProductComponents> components;
@OneToMany(mappedBy = "product", cascade = CascadeType.ALL) @OneToMany(mappedBy = "product", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JsonIgnore @JsonIgnore
private List<OrderProducts> orders; private List<OrderProducts> orders;

View File

@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*; import jakarta.persistence.*;
@Entity @Entity
@Table(name = "product_component")
public class ProductComponents { public class ProductComponents {
@EmbeddedId @EmbeddedId
private ProductComponentsKey id; private ProductComponentsKey id;
@ -16,6 +17,7 @@ public class ProductComponents {
@JoinColumn(name = "product_id") @JoinColumn(name = "product_id")
@JsonIgnore @JsonIgnore
private Product product; private Product product;
@Column(name = "count")
private Integer count; private Integer count;
public ProductComponents() { public ProductComponents() {

View File

@ -1,6 +1,9 @@
package ip.labwork.shop.service; package ip.labwork.shop.service;
import ip.labwork.shop.model.Component; 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.EntityManager;
import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContext;
@ -67,11 +70,19 @@ public class ComponentService {
@Transactional @Transactional
public Component deleteComponent(Long id) { public Component deleteComponent(Long id) {
final Component currentComponent = findComponent(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); em.remove(currentComponent);
return currentComponent; return currentComponent;
} }
@Transactional @Transactional
public void deleteAllComponent() { public void deleteAllComponent() {
em.createQuery("delete from ProductComponents").executeUpdate();
em.createQuery("delete from Component").executeUpdate(); em.createQuery("delete from Component").executeUpdate();
} }
} }

View File

@ -1,6 +1,7 @@
package ip.labwork.shop.service; package ip.labwork.shop.service;
import ip.labwork.shop.model.Component; import ip.labwork.shop.model.Component;
import ip.labwork.shop.model.OrderProducts;
import ip.labwork.shop.model.Product; import ip.labwork.shop.model.Product;
import ip.labwork.shop.model.ProductComponents; import ip.labwork.shop.model.ProductComponents;
import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManager;
@ -96,6 +97,13 @@ public class ProductService {
temp.getProduct().removeComponent(temp); temp.getProduct().removeComponent(temp);
em.remove(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); em.remove(currentProduct);
return currentProduct; return currentProduct;
} }
@ -103,6 +111,7 @@ public class ProductService {
@Transactional @Transactional
public void deleteAllProduct() { public void deleteAllProduct() {
em.createQuery("delete from ProductComponents").executeUpdate(); em.createQuery("delete from ProductComponents").executeUpdate();
em.createQuery("delete from OrderProducts ").executeUpdate();
em.createQuery("delete from Product").executeUpdate(); em.createQuery("delete from Product").executeUpdate();
} }