gpt4 book ai didi

java - JPA合并错误: "org.hibernate.TransientPropertyValueException: object references an unsaved transient instance -"

转载 作者:行者123 更新时间:2023-12-01 17:59:41 27 4
gpt4 key购买 nike

更新父实体时出现以下错误:请注意,持久化新父子实体时不会出现错误,仅在合并操作时才会出现错误。

org.hibernate.TransientPropertyValueException:对象引用未保存的 transient 实例 - 在刷新之前保存 transient 实例:

这是我的实体结构:

public class Vendor implements Serializable {

private static final long serialVersionUID = 4681697981214145859L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "vendor_id")
private Long vendorId;

@Column(name = "biz_type")
private String bizType;

...

@OneToMany(mappedBy = "vendor", fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
private Set<VendorAddress> vendorAddresses;
}

> public class VendorAddress implements Serializable {

private static final long serialVersionUID = 227762450606463794L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "vendor_adrs_id")
private Long vendorAdrsId;

@Column(name = "vend_adrs_ordinal")
private Integer vendAdrsOrdinal;

// bi-directional many-to-one association to City
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "city_id")
private City city;

// bi-directional many-to-one association to State
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "state_id")
private State state;

// bi-directional many-to-one association to Country
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "country_id")
private Country country;

....

// bi-directional many-to-one association to Vendor
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "vendor_id")
private Vendor vendor;
}

使用 DTO 通过网络将数据发送到客户端,并使用 Dozer 映射工具将数据从实体复制到 DTO。

这是保存 Vendor 及其地址的 EJB 方法。 Cascade 选项设置为 CascadeType.PERSIST,因此子实体 VendorAddress 也会与父实体 Vendor 一起保存。//新供应商正在被持久化,没有任何错误。

@Override
public VendorTO saveVendor(VendorTO vendorTo) throws ErpMiniAppException {

if (vendorTo.getVendorAddresses() != null) {
Iterator<VendorAddressTO> iter = vendorTo.getVendorAddresses().iterator();
if (iter.hasNext()) {
VendorAddressTO addressTo = iter.next();
addressTo.setVendAdrsOrdinal(1); // for new Vendor, address ordinal starts with 1
} else
throw new ErpMiniAppException("Address details no associated with Vendor.");
} else {
throw new ErpMiniAppException("Address details no associated with Vendor.");
}

Vendor vendor = (Vendor) ErpMiniMapper.getEntity(vendorTo, new Vendor());
Vendor persistedVendor = daoFactory.getVendorDAO().insert(vendor);
_logger.debug("InventoryServiceImpl :: saveVendor() new Entity inserted success!");
return (VendorTO) ErpMiniMapper.getTO(persistedVendor, new VendorTO());
}

更新供应商及其地址的 EJB 方法。 Cascade选项设置为CascadeType.MERGE,但在提交事务期间抛出异常。

@Override
public VendorTO updateVendor(VendorTO vendorTo) throws ErpMiniAppException {
_logger.debug("VendorTO details to be updated -> " + vendorTo.toString());
Vendor vendor = (Vendor) ErpMiniMapper.getEntity(vendorTo, new Vendor());
Vendor updatedVendor = daoFactory.getVendorDAO().update(vendor);
_logger.debug("Entity updated success!");
return (VendorTO) ErpMiniMapper.getTO(updatedVendor, new VendorTO());
}

引起:java.lang.IllegalStateException:org.hibernate.TransientPropertyValueException:对象引用未保存的 transient 实例 - 在刷新之前保存 transient 实例:com.itsys.erp.server.dal.entities.VendorAddress.vendor -> com.itsys.erp.server.dal.entities.Vendor。

因此,我检查集合 VendorAddress 是否为空以及是否存在于父实体 Vendor 中。子实体 VendorAddress 存在于父实体 Vendor 中,因为您可以看到两个主键都存在:vendor_id-> 9 和vendor_address_id-> 28。这是日志调试信息:

InventoryServiceImpl.java:265) - VendorTO 详细信息待更新 ->vendor_id->9 名称->Tetra Pak India Pvt. Ltd. 电子邮件 -> info.us@tetrapak.com 电话 -> 02135 678 101/1800 1555 4488 业务类型 -> 制造商供应商代码 -> tetrapak 地址详细信息 ->
19:14:46,860 INFO [stdout](默认任务-1)vendor_address_id-> 28 ordinal-> 1 供应商地址类型-> 制造地址_1-> 地 block 编号 53,MIDC Chakan 第 2 期,address_2-> Village Vasuli,Tal Khed,联系人 -> 02135 678101/02135 661801 供应商电子邮件 ID -> info.us@tetrapak.com 供应商信息 ->

尝试使用级联选项 CascadeType.ALL,得到相同的异常。是什么导致异常将其标记为 transient 实体?

最佳答案

VendorVendorAddresses 之间存在双向关联。将 VendorAddress 添加到 Vendor.vendorAddresses 时,您还必须调用 VendorAdress.setVendor

public void addAddressToVendor(VendorAddress address){
this.vendorAddresses.add(address);
address.setVendor(this)
}

关于java - JPA合并错误: "org.hibernate.TransientPropertyValueException: object references an unsaved transient instance -",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60658480/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com