API used to update the object: PUT
Functionality: Add new object if not exist in db or update on existing object along with child class (one to one or one to many mapping were there)
During doing update with full object below steps I am performing,
Optional<OcpiCustomer> customer = ocpiCustomerRepository.findById(cust.getId());
Optional<OcpiLocations> existingLocation = ocpiLocationsRepository.findByLocationIDAndOcpiCustomerId(locationId, cust.getId());
OcpiLocations location = ocpiLocationsMapper.toEntity(ocpiLocationDTO);
location.setOcpiCustomer(customer.get());
logger.info("existing PUT location? "+ existingLocation.isPresent());
if(existingLocation.isPresent()) {
location.setId(existingLocation.get().getId());
}
location = ocpiLocationsRepository.save(location);
So first I am fetching existinglocation if exist then new location object will be updated with seeing if of existing object. But doing so the side object with OneToOne mapping is getting generated as new instance (the older one remain as it is) which I am looking for a solution.
When I test to update same object with PUT its getting updated and also created additional entry in child object and then during GET of same object it throw error,
Error:
Caused by: org.hibernate.HibernateException: More than one row with the given identifier was found: 15, for class: so.so...
Transaction silently rolled back because it has been marked as rollback-only in response