Select Page

Update database with Fluent API approach

Written by Buddhi

2022-04-06

 

public Book book(Long bookId, Book book) {
return bookRepository.findById(bookId).
map(foundBook -> {
foundBook.setName(book.getName());
foundBook.setDescription(book.getDescription());
foundBook.setImageUrl(book.getImageUrl());
return bookRepository.save(foundBook);
})
.orElseThrow(() -> new BookNotFoundException(bookId));
}

How it works?

  • Find book by its id.
  • If exists, map the found book and set the new values that needs to be updated.
  • Save the book and return immediately.
  • Throw not found exception if book is not found

Related Articles

Using scp to copy files

Copy file from local pc to remote server   scp <filename> <useronremotesystem>@<ipaddressofremotesystem>:<remotedirpath>/<remotefilename> scp mytext.txt user2@192.168.1.25:/home/user2/mytext.txt  Copy file from remote server to local pc…

Thread Communication in Java

Thread Communication in Java

Thread Communication in Java 0 Comments Written by Buddhi 2020-05-12 Interthread communication in Java package org.example; public class ThreadCommunication { private static final int DATA_SIZE = 1000; public static void main (String[] args) { //common channel for…

Dependency Injection Summary

Dependency Injection Summary

Dependency Injection Summary 0 Comments Written by Buddhi 2018-08-05 Summary of dependency injection in Spring Boot. All the necessary dependencies are handed over to the object by spring. It frees the object from resolving the dependencies. It greatly simplifies the…