I want to know if JpaRepository has a method to update only a specific value of an object without getting the whole object from the database. This is my code to explain it a bit more:
Controller
@PatchMapping("/{labelKeyUuid}")
@ApiOperation("Update the current version of an existing label key")
fun updateCurrentVersion(@PathVariable labelKeyUuid: UUID,
@RequestBody labelValueUuidRequest: LabelValueUuidRequest) {
val labelValue = labelValueService.findByUuid(labelValueUuidRequest.uuid)
?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "Label value with uuid '\'${labelValueUuidRequest.uuid}\'' not found")
return labelKeyService.updateCurrentVersion(labelKeyUuid, labelValue)
}
Service
fun findByUuid(uuid: UUID) : LabelValueEntity? {
return labelValueRepository.findByIdOrNull(uuid)
}
fun updateCurrentVersion(labelKeyUuid: UUID, labelValue: LabelValueEntity) {
labelKeyRepository.save(labelValue, labelKeyUuid)
}
Repository
@Repository
interface LabelKeyRepository : JpaRepository<LabelKeyEntity, UUID>
JpaRepository has save(entity) method. I know i can work around it by getting the object from the database and set the labelValue with the new one and save it to the database. Is there a faster way to do this?