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

List users in windows

List users and groups in Windows Type Users in search box or enter lusrmgr.msc in Run dialog box Win+R -> lusrmgr.msc -> enter

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…

Docker and Docker Compose

Docker Compose docker-compose.yml file contains the command to run, start, stop all the containers and services mentioned inside the file. up build or create or recreate container and start it docker-compose up build build the container but won’t start it…