更新父实体时出现以下错误:
请注意,在保留新的父子实体时没有错误,仅在合并操作时发生错误。

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通过网络将数据发送到客户端和推土机映射工具,以将数据从实体复制到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方法。 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 -> [email protected] 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-> [email protected] vendor info->

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

最佳答案

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

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

10-04 11:43