I receive following error when I save the object using Hibernate
object references an unsaved transient instance - save the transient instance before flushi
In my case it was caused by not having CascadeType
on the @ManyToOne
side of the bidirectional relationship. To be more precise, I had CascadeType.ALL
on @OneToMany
side and did not have it on @ManyToOne
. Adding CascadeType.ALL
to @ManyToOne
resolved the issue.
One-to-many side:
@OneToMany(cascade = CascadeType.ALL, mappedBy="globalConfig", orphanRemoval = true)
private SetgcScopeSet;
Many-to-one side (caused the problem)
@ManyToOne
@JoinColumn(name="global_config_id")
private GlobalConfig globalConfig;
Many-to-one (fixed by adding CascadeType.PERSIST
)
@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name="global_config_id")
private GlobalConfig globalConfig;