父数据正在保存,但子数据列表未保存在表中。 来自邮递员的数据
{"billno":"nur-1001", "grandTotal": 5000,"billcart":[{"itemcode":"SU10027", "soldPrice":0},{"itemcode":"SU10027","soldPrice":1100}]}
Bill 是父实体 & billcart 是子实体
@NoArgsConstructor @AllArgsConstructor @Data @Entity
public class Bill {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String billno;
private Long grandTotal;
@OneToMany(mappedBy = "bill", cascade = CascadeType.ALL)
@JsonIgnore
private List<Billcart> billcart = new ArrayList<>();
public Bill( String billno, Long grandTotal, List<Billcart> billcart) {
this.billno = billno;
this.grandTotal = grandTotal;
this.billcart = billcart;
this.billcart.forEach(e -> e.setBill(this));
}
}
子实体
@NoArgsConstructor @AllArgsConstructor @Data @Entity
public class Billcart {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String itemcode;
private Integer soldPrice;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "bill_id")
private Bill bill;
控制器
public void saveBill(@RequestBody Bill request) {
List<Billcart> billscart = request.getBillcart().stream()
.map(e -> new Billcart(e))
.collect(Collectors.toList());
Bill bill = new Bill(request.getBillno(), request.getGrandTotal() , billscart);
billRepository.save(bill);