我有两个实体: -
Sales.Java
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int salesId;
private BigDecimal discount;
private BigDecimal totalTransaction;
@OneToMany(mappedBy = "sales", cascade = CascadeType.ALL)
private List<ProductSalesDetails> productSalesDetails = new ArrayList<>();
public List<ProductSalesDetails> getProductSalesDetails() {
return productSalesDetails;
}
public void addProductSalesDetails(ProductSalesDetails productSalesDetails) {
this.productSalesDetails.add(productSalesDetails);
}
另一个实体是: -
ProductSalesDetails.java
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String category,name,vendor;
private int quantity;
private BigDecimal price;
private BigDecimal totalSalesAmount;
@Temporal(TemporalType.DATE)
private Date createdDate;
private String salesBy;
@ManyToOne
private Sales sales;
现在我的前端表单数据是JSON,如下所示: -
[{category:"spa"
discount:""
grandTotal:3800
name:"toe cleaner"
price:"1200"
quantity:2
totalPrice:2400
vendor:"spa product"},
{category:"Hair"
discount:""
grandTotal:3800
name:"hair wash shampoo"
price:"700"
quantity:2
totalPrice:1400
vendor:"hair vendor"}]
我的DAO课程: -
public void createBill(List<ProductSales> salesList) {
Sales s = new Sales();
ProductSalesDetails ps = new ProductSalesDetails();
for (ProductSales p : salesList){
RgProduct rgProduct = productDao
.getSingleProductByProductName(p.getName());
s.setDiscount(p.getDiscount());
s.setTotalTransaction(p.getGrandTotal());
ps.setCategory(p.getCategory());
ps.setName(p.getName());
ps.setQuantity(p.getQuantity());
ps.setVendor(p.getVendor());
ps.setPrice(rgProduct.getCostPrice());
ps.setTotalSalesAmount(p.getTotalPrice());
ps.setCreatedDate(new Date());
ps.setSalesBy("sagar");
s.addProductSalesDetails(ps);
ps.setSales(s);
}
em.persist(s);
for(ProductSalesDetails pds : s.getProductSalesDetails()){
em.persist(pds);
}
}
所以现在我想在销售表中保存grandtotal和折扣,而其他细节应保存在ProductSalesDetails表中。我试图把销售和ProductSalesDetails之间的关系。
ProductSalesDetails将有一个外键列,它将映射到sales表的主键。
我的理想数据库行如下所示: -
目前,sales表正在按需保存,但在产品详细信息表中,无论列表中存在多少行,都只保存一行。