```//Controller @PatchMapping("/comment/{id}/") ...
# spring
n
Copy code
//Controller
 @PatchMapping("/comment/{id}/")
    fun PatchCommentById(@PathVariable id: Long, @RequestBody comment: Comment): ResponseEntity<Comment> {
    
        val updatedComment = repo.save(Comment(
                id = id,
                body = comment.body,
                video_link = comment.video_link,
                image_link = comment.image_link,
        ))
        return ResponseEntity.ok(updatedComment)
    }
how would u partial update this?
a
You could do something like this:
val updatedComment = Comment()
updatedComment.body = comment.body.let { it }
updatedComment.video_link = comment.video_link.let { it }
n
if i do it that way, it says no value passed for parameter for the first line
t
with jpa I would suggest to always work with attached entities, so
Copy code
val comment = repo.getById(id).apply {
  body = comment.body
  ...
}
return ResponseEntity.ok(repo.save(comment))
there is also some sorcery I am never sure about, but if you in a
@Transactional
repo.save
might happen implicitly, so you would not need to write the code yourself.
otoh, I don’t think
PATCH
is that simplistic. from the rfc (https://tools.ietf.org/html/rfc5789#section-2)
Copy code
With PATCH, however, the enclosed entity contains a set of instructions describing how a resource currently residing on the origin server should be modified to produce a new version.
then json-patch was created with more specs on the operation part: https://tools.ietf.org/html/rfc6902#page-4
a
@nathan Try not use database entities in the signature of your function. You could use a DTO or ViewModel here.
c
First of all, separate your entity object from the request object then you have a world of options. For example, you can create a request object similar to this (pseudocode):
Copy code
class CommentRequest(
    val body: JsonNullable<String> = JsonNullable.undefined(),
    // etc.
)
with the help of https://github.com/OpenAPITools/jackson-databind-nullable. You can then do this in your service:
Copy code
if (commentRequest.body.isPresent()) {
     comment.body = commentRequest.body
 }
 // etc.