I have a composite key in one of my persistence class ProductVersionElement for id and versionid. For each key, i have a custom hibernate sequencer as IdGenerator and VersionIdGenerator.
Code:
@Id
@GenericGenerator(name = "custom_id", strategy = "com.model.persistent.sequencer.IdGenerator")
@GeneratedValue(generator = "custom_id")
protected String id;
@Id
@GenericGenerator(name = "custom_versionId", strategy = "com.model.persistent.sequencer.VersionIDGenerator")
@GeneratedValue(generator = "custom_versionId")
protected String versionId;
Case 1. When both the id and versionid are null. Then the sequencer populates the sequence for both fields and successfully save the ProductVersionElement. After the save, in debug it shows the generated values for both fields.
Case 2: When the id is set(i know the value beforehand and only want to generate the versionid) and versionid is null. For this i have changed the sequencer such that : if the value of field is null, then generate the sequence else return the same value.
Code:
if (id == null) {
//Generate the sequence and return
} else{
// return same id
}
In this case, it is not saving the ProductVersionElement. Even after the save , it is only showing the value of id(that was already set) and versionid is still null after save.Although it does not result in any exception and after that when i try to save one other Object : ProductCharacteristics which contains ProductVersionElement(to save one of its id), it is resulting in the following error:
org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.orange.obsit.sando.cibinternational.model.persistent.ProductCharacteristic.productVersionElement -> com.orange.obsit.sando.cibinternational.model.persistent.ProductVersionElement;Could you please give any suggestions on this?