Не удается удалить встроенное свойство - PullRequest
0 голосов
/ 12 апреля 2020

Spring boot 2.5

здесь содержимое корзины

{
code = '0'
message = 'Success'
body = '{
  "id": 30,
  "username": "admin@admin.com",
  "created": "Apr 12, 2020, 6:16:18 PM",
  "updated": "Apr 12, 2020, 6:17:12 PM",
  "productEntities": [
    {
      "quantity": 1,
      "product": {
        "id": 32,
        "name": "product name_1",
        "description": "product description_1",
        "created": "Mar 10, 2020, 10:34:15 PM",
        "price": 1.0,
        "currency": "USD",
        "images": [
          "some_url1"
        ]
      }
    }
  ]
}'}

Теперь я хочу удалить товар с id. Я использую CascadeType.ALL аннотацию

репо:

public interface ProductEntityReposistory extends CrudRepository<ProductEntity, Integer> {

}

здесь модели:

@Entity
public class Cart {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    @NotNull
    private String username;
    @NotNull
    @DateTimeFormat(pattern = "dd.MM.yyyy HH:mm:ss")
    private Date created;
    @DateTimeFormat(pattern = "dd.MM.yyyy HH:mm:ss")
    private Date updated;
    @OneToMany(mappedBy = "cart", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Set<ProductEntity> productEntities = new HashSet<>();

@Entity
public class ProductEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Exclude
    private int id;
    @NotNull
    @DateTimeFormat(pattern = "dd.MM.yyyy HH:mm:ss")
    @Exclude
    private Date created;
    @DateTimeFormat(pattern = "dd.MM.yyyy HH:mm:ss")
    @Exclude
    private Date updated;
    private int quantity;
    @Exclude
    @JoinColumn(name = "order_id")
    @ManyToOne(fetch = FetchType.LAZY)
    private Orders orders;
    @OneToOne(cascade = CascadeType.ALL)
    private Product product;
    @Exclude
    @ManyToOne(fetch = FetchType.EAGER, optional = false)
private Cart cart;
@Entity
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    @NotNull
    private String name;
    private String description;
    @NotNull
    @DateTimeFormat(pattern = "dd.MM.yyyy HH:mm:ss")
    private Date created;
    @DateTimeFormat(pattern = "dd.MM.yyyy HH:mm:ss")
    private Date updated;
    @NotNull
    private double price;
    @NotNull
    private String currency;
    @ElementCollection
    private Set<String> images;
    @Exclude
    @OneToOne(mappedBy = "product", fetch = FetchType.EAGER)
private ProductEntity productEntity;

и здесь метод в контроллере, который удаляет продукт

 @DeleteMapping("/cart/product")
    public Response deleteProduct(@RequestParam(name = "cart_id") int cartId, @RequestParam(name = "product_id") int productId) {
        try {
            logger.info("deleteProduct: cartId = " + cartId + ", productId = " + productId);
            Optional<Cart> findCart = cartRepository.findById(cartId);
            if (findCart.isPresent()) {
                Cart cart = findCart.get();
                ProductEntity findProductEntity = cart.getProductEntity(productId);
                logger.info("deleteProduct: findProductEntity " + findProductEntity);
                productEntityReposistory.delete(findProductEntity);
                logger.info("deleteProduct: success_delete_product_with_id: " + productId);
                return ResponseService.getSuccessResponse();
            } else {
                String error = "Not found cart by id = " + cartId;
                logger.warn("deleteProduct: " + error);
                return ResponseService.getErrorResponse(error);
            }
        } catch (Throwable ex) {
            return ResponseService.getErrorResponse(ex.getMessage());
        }
}

Журнал:

deleteProduct: success_delete_product_with_id: 32

Удаление прошло успешно.

Но когда я снова получаю содержимое корзины, тогда корзина снова загружает продукт с id = 32

Не хотите ли удалить товар?

...