I am using hibernate.enhancer.enableLazyInitialization = true in my Spring boot application.
hibernate version - 5.6.15
I have an entity Sample.java
@Entity
@Table(name = "sample")
public class Sample {
@Id
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "sample_config_id")
private SampleConfig sampleConfig;
@Column(name = "satus", length = 10)
private String status;
}
And I am trying to do
Sample sample = sampleRepository.findById(1L);
sample.setStatus("ACTIVE");
sampleRepository.save(sample) ;
but this is throwing error
object references an unsaved transient instance - save the transient instance before flushing: com.xxx.xxx.xxx.Sample.sampleConfig
It is working as expected if I remove fetch type Lazy in @ManyToOne. But my requirement is that I want to fetch the entity lazily but while saving, I don't want to Cascade on any operation.
If I do CascadeType.ALL it is updating the sample_config table also with null fields.
If I do enableLazyInitialization = false and CascadeType.ALL it is not updating the sample_config but saving the sample ( This is my expected behaviour but I require lazy fetch )