Reputation: 13471
I´m having an issue trying to persist two new entities on database with a mapping between them
The Parent entity:
@OneToOne(cascade = CascadeType.MERGE, mappedBy = "conflictOfInterest")
@XmlTransient
@JsonIgnore
@Getter
@Setter
private RequestForCorrection requestForCorrection;
The child entity:
@OneToOne()
@JoinColumn(name = "conflict_of_interest_id")
@JsonIgnore
@XmlTransient
@Getter
@Setter
private ConflictOfInterest conflictOfInterest;
When RequestForCorrection and ConflictOfInterest are ID null and I have
requestForCorrection.setConflictOfInterest(conflictOfInterest)
save(requestForCorrection)
Hibernate is throwing the exception
Caused by: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing: com.greenvalley.etendering.domain.RequestForCorrection.conflictOfInterest -> com.greenvalley.etendering.domain.ConflictOfInterest
at org.hibernate.engine.spi.CascadingAction$8.noCascade(CascadingAction.java:380) ~[hibernate-core-4.2.7.Final.jar:4.2.7.Final]
I try change the cascade annotation of the parent to ALL and PERSIST without any success. I´m looking to save on cascade, so
save(conflictOfInterest)
requestForCorrection.setConflictOfInterest(conflictOfInterest)
save(requestForCorrection)
I don't consider a valid solution
Upvotes: 0
Views: 947
Reputation: 26
When saving the Parent table hibernate it is trying to create the Child table that have a new reference to the parent. I suggest you 2 changes.
First, change the CascadeType from Merge to ALL if you want that all the changes in the parent go to the child
Second, add nullable = false to the Child to force the fk is added in the insert.
@OneToOne(cascade = CascadeType.ALL, mappedBy = "conflictOfInterest")
@XmlTransient
@JsonIgnore
@Getter
@Setter
private RequestForCorrection requestForCorrection;
@OneToOne
@JoinColumn(name = "conflict_of_interest_id", nullable = false)
@JsonIgnore
@Getter
@Setter
private ConflictOfInterest conflictOfInterest;
Upvotes: 1