spring数据jpa、hibernate和mysql

xwbd5t1u  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(319)

我参与了需要在单个事务中持久化嵌套对象的项目。

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秒钟来存储数据。以上代码是示例,实际代码可能有更多的子实体。

nwwlzxa7

nwwlzxa71#

你的Map有一些严重的问题。这就是为什么hibernate也运行许多更新查询。我已经用以下代码进行了测试。对于seven对象,它正在运行seven insert into语句和seven update查询。

PurchaseOrderMaterialDutiesAndTax purchaseOrderMaterialDutiesAndTaxes = new PurchaseOrderMaterialDutiesAndTax();
    PurchaseOrderMaterialDutiesAndTax purchaseOrderMaterialDutiesAndTaxes1 = new PurchaseOrderMaterialDutiesAndTax();

    PurchaseOrderOtherExpenditureItem purchaseOrderOtherExpenditureItem = new PurchaseOrderOtherExpenditureItem();
    PurchaseOrderOtherExpenditureItem purchaseOrderOtherExpenditureItem1 = new PurchaseOrderOtherExpenditureItem();

    PurchaseOrderMaterial purchaseOrderMaterials = new PurchaseOrderMaterial();
    purchaseOrderMaterials.setPurchaseOrderMaterialDutiesAndTaxes(Arrays.asList(purchaseOrderMaterialDutiesAndTaxes, purchaseOrderMaterialDutiesAndTaxes1));
    purchaseOrderMaterials.setPurchaseOrderOtherExpenditureItems(Arrays.asList(purchaseOrderOtherExpenditureItem, purchaseOrderOtherExpenditureItem1));

    PurchaseOrderMaterial purchaseOrderMaterials1 = new PurchaseOrderMaterial();
    purchaseOrderMaterials.setPurchaseOrderMaterialDutiesAndTaxes(Arrays.asList(purchaseOrderMaterialDutiesAndTaxes, purchaseOrderMaterialDutiesAndTaxes1));
    purchaseOrderMaterials.setPurchaseOrderOtherExpenditureItems(Arrays.asList(purchaseOrderOtherExpenditureItem, purchaseOrderOtherExpenditureItem1));

    PurchaseOrder purchaseOrder = new PurchaseOrder();
    purchaseOrder.setPurchaseOrderMaterials(Arrays.asList(purchaseOrderMaterials, purchaseOrderMaterials1));

您应该更改一对多Map所有者端。请看下面的博客https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate/

相关问题