Reputation: 5319
I'm trying to save the MotoPayment object. But i have to check the customer with code already exists or not before saving motopayment object.
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;
}
controller method
@RequestMapping(value = "/motopaymnetorder", method = RequestMethod.POST)
@ResponseBody public MotoPaymentOrder create(@RequestBody MotoPaymentOrder domain){
prepareCustomer(domain.getCustomer());
return (MotoPaymentOrder) baseDocumentService.saveDocument(domain);
}
The below method is used to check the customer with code exists or not otherwise save customer object.
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);
}
}
I'm getting below exception while saving MotoPaymentOrder object:
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)
Upvotes: 0
Views: 10932
Reputation: 4821
See here save the transient instance before flushing in SpringBoot 2.0.4 app
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "CUSTOMER")
private Customer customer;
Upvotes: 2
Reputation: 3978
Suppose for a MotoPaymentOrder
, the associated Customer
is not persisted, the JPA
will should persist it, which is not implemented by your approach. You can use CascadeType.REFRESH
to tell hibernate what to do with your entity, if that is not already persisted.
@ManyToOne(cascade=CascadeType.REFRESH)
@JoinColumn(name = "CUSTOMER")
private Customer customer;
Upvotes: 1