Not save inner list of entites

只愿长相守 提交于 2020-08-10 19:12:07

问题


Spring Boot 2.2

Models:

@Entity
public class Cart {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @JsonIgnore
    private int id;
    @NotNull
    private String cartId;
    @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.MERGE, orphanRemoval = true)
    private Set<ProductEntry> productEntities = new HashSet<>();

  @Override
    public String toString() {
        return "Cart{" +
                "id = " + id +
                ", cartId = " + cartId +
                ", username = " + username +
                ", productEntities(" + productEntities.size() + ")\n" + productEntities +
                ", created = " + created +
                ", updated = " + updated +
                '}';
    }


@Entity
public class ProductEntry {
    @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;
    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private Product product;
    @Exclude
    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    private Orders orders;
    @Exclude
    @JsonIgnore
    @ManyToOne(fetch = FetchType.EAGER)
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
    @JsonIgnore
    @OneToOne(mappedBy = "product", fetch = FetchType.EAGER)
private ProductEntry productEntry;

Controller:

Add product to cart by this:

 @PostMapping("/cart/product")
    public Cart addProductToCart(@RequestBody Map<String, Object> payloadMap) {
        logger.info("addProductToCart: payloadMap: " + payloadMap);
        String userName = payloadMap.get("user_name").toString();
        final String productString = payloadMap.get("product").toString();
        Product product;
        Object fromValue = payloadMap.get("product");
        if (fromValue instanceof LinkedHashMap) {
            product = new ObjectMapper().convertValue(fromValue, Product.class);
        } else {
            product = GsonUtil.gson.fromJson(productString, Product.class);
        }
        int quantity = (int) payloadMap.get("quantity");
        Cart findCart = cartRepository.findByUsername(userName);
        logger.info("addProductToCart: user_name = " + userName + " -> findCart:\n" + findCart);
        if (findCart == null) {
            Cart cart = new Cart();
            cart.setCartId(UUID.randomUUID().toString());
            cart.setCreated(new Date());
            cart.setUsername(userName);
            cart.addProduct(product, quantity);
            cart = cartRepository.save(cart);
            logger.info("addProductToCart: success_add_product_to_new_cart:\n" + cart);
            return cart;
        } else {
            findCart.addProduct(product, quantity);
            findCart = cartRepository.save(findCart);
            logger.info("addProductToCart: success_add_product_to_exist_cart:\n" + findCart);
            return findCart;
        }
}

The result is:

: addProductToCart: user_name = admin@admin.com -> findCart:
null
2020-04-26 21:24:41.359  INFO 6485 --- [nio-8092-exec-1] r.o.s.e.controller.CartController        : addProductToCart: success_add_product_to_new_cart:
Cart{id = 1, cartId = 69714d80-6724-403e-a8c5-17505cd8f4fb, username = admin@admin.com, productEntities(1)
[
ProductEntity{id = 0, quantity = 0, product = 
Product{id = 0, name = 'My product 1', description='', created=Sun Apr 26 21:24:41 EEST 2020, updated=null, price=1.0, currency='USD', images=[url_1, url_2]}}], created = Sun Apr 26 21:24:41 EEST 2020, updated = null}

As you can see the productEntities size is 1.

But after I call this method (get car by user_name):

    @GetMapping("/cart")
    public Cart getCart(@RequestParam(name = "user_name") String user_name) {
        Cart findCart = cartRepository.findByUsername(user_name);
        logger.info("getCart: user_name = " + user_name + " -> findCart:\n" + findCart);
        return findCart;
}

response result:

2020-04-26 21:29:44.136  INFO 6485 --- [nio-8092-exec-6] r.o.s.e.controller.CartController        : getCart: user_name = admin@admin.com -> findCart:
Cart{id = 1, cartId = 69714d80-6724-403e-a8c5-17505cd8f4fb, username = admin@admin.com, productEntities(0)
[], created = 2020-04-26 21:24:41.331, updated = null}

Why "productEntities" now is empty?


回答1:


I think the issue is productEntities was not saved at the initial save call.

This is due to absence of CascadeType.PERSIST in your productEntities field.

Change your mapping to this:

@OneToMany(mappedBy = "cart", fetch = FetchType.EAGER, cascade = { CascadeType.MERGE, CascadeType.PERSIST } orphanRemoval = true) 
private Set<ProductEntry> productEntities = new HashSet<>(); 

This will work.



来源:https://stackoverflow.com/questions/61446078/not-save-inner-list-of-entites

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!