Ошибка слияния JPA: «org.hibernate.TransientPropertyValueException: объект ссылается на несохраненный временный экземпляр -» - PullRequest
0 голосов
/ 12 марта 2020

Получение следующей ошибки при обновлении родительского объекта: обратите внимание, что при сохранении нового объекта Parent-child ошибка отсутствует, ошибка возникает только во время операции слияния.

org.hibernate.TransientPropertyValueException: объект ссылается на несохраненный переходный процесс instance - сохранить временный экземпляр перед сбросом:

Вот моя структура Entity:

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 для копирования данных из Entities в DTO .

Вот метод EJB для сохранения Vendor и его адрес. Для параметра 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, так как вы видите, что присутствуют оба первичных ключа: 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 получая такое же исключение. Что вызывает исключение, чтобы пометить его как временную сущность?

1 Ответ

0 голосов
/ 12 марта 2020

У вас двунаправленная ассоциация между Vendor и VendorAddresses. Когда вы добавляете VendorAddress к Vendor.vendorAddresses, вы также должны позвонить VendorAdress.setVendor.

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