【发布时间】:2020-06-24 18:17:36
【问题描述】:
更新父实体时出现以下错误: 注意持久化新的Parent-child Entity时不会出错,只会在merge操作时出错。
org.hibernate.TransientPropertyValueException: 对象引用了一个未保存的瞬态实例 - 在刷新之前保存瞬态实例:
这是我的实体结构:
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 Mapping 工具将数据从实体复制到 DTO。
这是持久化供应商及其地址的 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 方法来更新 Vendor 及其地址。 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: object references an unsaved transient instance - save the transient instance before flushing : com.itsys.erp.server.dal.entities.VendorAddress.vendor -> com.itsys.erp.server.dal.entities.Vendor.
所以我检查集合 VendorAddress 是否为空并且存在于父实体 Vendor 中。子实体 VendorAddress 存在于父实体供应商中,您可以看到两个主键都存在:vendor_id-> 9 and vendor_address_id-> 28.
这是日志调试信息:
InventoryServiceImpl.java:265) - VendorTO details to be updated -> vendor_id-> 9 name-> Tetra Pak India Pvt. Ltd. email -> info.us@tetrapak.com phone -> 02135 678 101 / 1800 1555 4488 biz type-> MFG vendor code-> tetrapak Address Details ->
19:14:46,860 INFO [stdout] (default task-1) vendor_address_id-> 28 ordinal-> 1 vendor Address Type-> Manufacturing address_1-> Plot No 53, MIDC Chakan Phase 2, address_2-> Village Vasuli, Tal Khed, contact-> 02135 678101 / 02135 661801 vendor email ID-> info.us@tetrapak.com vendor info->
尝试使用级联选项 CascadeType.ALL,得到相同的异常。 是什么导致异常将其标记为瞬态实体?
【问题讨论】: