Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 不保存实体的内部列表_Spring Boot_Spring Data Jpa - Fatal编程技术网

Spring boot 不保存实体的内部列表

Spring boot 不保存实体的内部列表,spring-boot,spring-data-jpa,Spring Boot,Spring Data Jpa,弹簧靴2.2 型号: @Entity public class Cart { @Id @GeneratedValue(strategy = GenerationType.AUTO) @JsonIgnore private int id; @NotNull private String cartId; @NotNull private String username; @NotNull @DateTimeFormat(

弹簧靴2.2

型号:

@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;
如您所见,
productEntities
大小为1

但在我调用此方法(按用户名获取汽车)后:

答复结果:

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}

为什么“productEntities”现在是空的?

我认为问题在于
productEntities
在最初的保存调用中没有保存

这是由于您的
productEntities
字段中缺少
CascadeType.PERSIST

将映射更改为:

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

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