org.hibernate.PersistentObjectException: detached entity passed to persist
You didn't provide many relevant details so I will guess that you called getInvoice
and then you used result object to set some values and call save
with assumption that your object changes will be saved.
However, persist
operation is intended for brand new transient objects and it fails if id is already assigned. In your case you probably want to call saveOrUpdate
instead of persist
.
You can find some discussion and references here "detached entity passed to persist error" with JPA/EJB code
This exists in @ManyToOne relation. I solved this issue by just using CascadeType.MERGE instead of CascadeType.PERSIST or CascadeType.ALL. Hope it helps you.
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="updated_by", referencedColumnName = "id")
private Admin admin;
Solution:
@ManyToOne(cascade = CascadeType.MERGE)
@JoinColumn(name="updated_by", referencedColumnName = "id")
private Admin admin;