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

Dependency Injection Summary

Dependency Injection Summary

Update database with Fluent API approach 0 Comments Written by Buddhi2022-04-06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…

Spring Annotations and Component Scanning

Spring Annotations and Component Scanning

Update database with Fluent API approach 0 Comments Written by Buddhi2022-04-06Best practices for defining components and scanning them Annotate the class by including the exact place where to scan for components with…

Various description of Big O – Time complexity algorithms

Various description of Big O – Time complexity algorithms

Update database with Fluent API approach 0 Comments Written by Buddhi2022-04-06Understanding the time complexity of Big O and log. O(log N) means time goes up linearly while the n goes up exponentially. So if it takes 1 second to compute 10 elements, it will take 2…