You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The BaseService.update method serializes the given entity, deserializes it again and saves it. This causes the resulting object to not be a proper hibernate proxy. (edit: to be more precise: any related objects are no hibernate proxies anymore, but merely the objects that got deserialized. It can be that the object only contains an Id if the field was using the BaseEntityIdSerializer). Also all other managed instances of the object are broken as well.
Firstly the question arises why this code with the serialization and deserialization exists:
@PreAuthorize("hasRole('ROLE_ADMIN') or hasPermission(#entity, 'UPDATE')")
@Transactional(isolation = Isolation.SERIALIZABLE)
publicSupdate(Longid, Sentity) throwsIOException {
Optional<S> persistedEntityOpt = repository.findById(id);
ObjectNodejsonObject = objectMapper.valueToTree(entity);
// Ensure the created timestamp will not be overridden.SpersistedEntity = persistedEntityOpt.orElseThrow();
OffsetDateTimecreatedTimestamp = persistedEntity.getCreated();
Stringserialized = createdTimestamp != null ? createdTimestamp.toInstant().toString() : null;
jsonObject.put("created", serialized);
SupdatedEntity = objectMapper.readerForUpdating(persistedEntity).readValue(jsonObject);
returnrepository.save(updatedEntity);
}
I suppose this in place to leave the entity intact. Is this really neccessary?
varupdatedEntity = super.update(entityId, entity);
entityManager.flush(); // flush database changesentityManager.detach(updatedEntity); // this way the broken object is no longer a managed hibernate object// without the detach the findById would just return the broken managed objectupdatedEntity = repository.findById(entityId).orElseThrow(); // get a new managed hibernate proxy object
This might not be feasible for the BaseService as I assume that flush does have performance issues if called often.
The text was updated successfully, but these errors were encountered:
The
BaseService.update
method serializes the given entity, deserializes it again and saves it. This causes the resulting object to not be a proper hibernate proxy. (edit: to be more precise: any related objects are no hibernate proxies anymore, but merely the objects that got deserialized. It can be that the object only contains an Id if the field was using theBaseEntityIdSerializer
). Also all other managed instances of the object are broken as well.Firstly the question arises why this code with the serialization and deserialization exists:
I suppose this in place to leave the entity intact. Is this really neccessary?
The simplest way to achieve the same would be:
Another way to solve this would be to use the lombok
@SuperBuilder
annotation and create a copy. See https://www.projectlombok.org/features/experimental/SuperBuilderFor our project we used the following workaround:
This might not be feasible for the
BaseService
as I assume thatflush
does have performance issues if called often.The text was updated successfully, but these errors were encountered: