0
我試圖保存MotoPayment對象。但我必須檢查客戶與代碼已經存在或沒有保存motopayment對象之前。在保存對象之前保存臨時實例,然後保存對象
public class MotoPaymentOrder extends BaseDocument {
@ManyToOne
@JoinColumn(name = "CUSTOMER")
private Customer customer;
@Column(name = "AMOUNT")
private Double amount = Double.valueOf(0);
@ManyToOne
@JoinColumn(name = "PAYMENTSTATUS")
protected SystemTypeLookupData status ;
@Column(name = "PNRNO")
private String pnrNo;
@Column(name = "RESERVATIONNO")
private String reservationNo;
}
控制器方法
@RequestMapping(value = "/motopaymnetorder", method = RequestMethod.POST)
@ResponseBody public MotoPaymentOrder create(@RequestBody MotoPaymentOrder domain){
prepareCustomer(domain.getCustomer());
return (MotoPaymentOrder) baseDocumentService.saveDocument(domain);
}
的以下方法使用具有代碼存在,或不另外保存客戶對象來檢查客戶。
public void prepareCustomer(Customer customer){
Customer existingCustomer = (Customer) appService.findOneWhere("WHERE o.code = '"+customer.getCode()+"'",Customer.class);
if(existingCustomer==null){
customer.setType(ApplicationConstants.CustomerType.Person);
PersonCustomer personCustomer = new PersonCustomer();
personCustomer.setEmail(customer.getCustomerEmail());
personCustomer.setFirstName(customer.getPersonCustomer().getFirstName());
personCustomer.setLastName(customer.getPersonCustomer().getLastName());
personCustomer.setCustomer(customer);
customer.setPersonCustomer(personCustomer);
}
}
我得到異常以下,同時節省MotoPaymentOrder對象:
org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.erp.hospitality.domain.MotoPaymentOrder.customer -> com.erp.core.domain.common.Customer
at org.hibernate.engine.spi.CascadingActions$8.noCascade(CascadingActions.java:379)
at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:126)
at org.hibernate.event.internal.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:150)
at org.hibernate.event.internal.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:141)
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:74)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:38)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1282)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1300)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
好讀。 https://vladmihalcea.com/2015/03/05/a-beginners-guide-to-jpa-and-hibernate-cascade-types/ –
嘗試手動保留臨時子對象,然後將其設置爲父對象。 –