我参与了需要在单个事务中持久化嵌套对象的项目。
class PurchaseOrder{
@Id
@GeneratedValue(strategy=strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = PurchaseOrderMaterial.class)
@JoinColumn(name = "purchaseOrder_id")
private List<PurchaseOrderMaterial> purchaseOrderMaterials;
......
......
}
和purchaseordermaterial.java
class PurchaseOrderMaterial{
@Id
@GeneratedValue(strategy=strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = PurchaseOrderMaterialDutiesAndTax.class)
@JoinColumn(name = "purchaseOrderMaterial_id", nullable = true)
private List<PurchaseOrderMaterialDutiesAndTax> purchaseOrderMaterialDutiesAndTaxes;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = PurchaseOrderOtherExpenditureItem.class)
@JoinColumn(name = "purchaseOrderMaterial_id")
private List<PurchaseOrderOtherExpenditureItem> purchaseOrderOtherExpenditureItems;
.....
.....
}
其他两个实体(purchaseorderotherexpenditureitem.java和purchaseorderotherexpenditureitem.java)将没有子实体/关系。我从前端装框。在我的服务实现中,我只调用了
repo.save(purchaseOrder);
我的应用程序花了5秒钟来存储数据。以上代码是示例,实际代码可能有更多的子实体。
1条答案
按热度按时间nwwlzxa71#
你的Map有一些严重的问题。这就是为什么hibernate也运行许多更新查询。我已经用以下代码进行了测试。对于seven对象,它正在运行seven insert into语句和seven update查询。
您应该更改一对多Map所有者端。请看下面的博客https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate/