From 28d42eff87da99a81612b245cfafe527abb28bce Mon Sep 17 00:00:00 2001 From: Pekka Helenius Date: Sat, 3 Oct 2020 01:27:44 +0300 Subject: [PATCH] Extend BookRepository interface; implement updateWithoutPrice method Signed-off-by: Pekka Helenius --- .../bookstore/model/book/BookRepository.java | 4 +- .../model/book/BookRepositoryCustom.java | 12 ++++ .../model/book/BookRepositoryImpl.java | 57 +++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 bookstore/src/main/java/com/fjordtek/bookstore/model/book/BookRepositoryCustom.java create mode 100644 bookstore/src/main/java/com/fjordtek/bookstore/model/book/BookRepositoryImpl.java diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/model/book/BookRepository.java b/bookstore/src/main/java/com/fjordtek/bookstore/model/book/BookRepository.java index aa3a0a2..b3a6123 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/model/book/BookRepository.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/model/book/BookRepository.java @@ -24,7 +24,7 @@ import org.springframework.data.rest.core.annotation.RestResource; itemResourceRel = "booklist", exported = true ) -public interface BookRepository extends CrudRepository { +public interface BookRepository extends CrudRepository, BookRepositoryCustom { @Override //@RestResource(exported = false) @@ -50,7 +50,7 @@ public interface BookRepository extends CrudRepository { /* * We need to override native delete method due to book hash id usage. - * This is a native query, do not unnecessarily validate it. + * This is a native HQL query, do not unnecessarily validate it. */ @Override @Modifying diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/model/book/BookRepositoryCustom.java b/bookstore/src/main/java/com/fjordtek/bookstore/model/book/BookRepositoryCustom.java new file mode 100644 index 0000000..c93e555 --- /dev/null +++ b/bookstore/src/main/java/com/fjordtek/bookstore/model/book/BookRepositoryCustom.java @@ -0,0 +1,12 @@ +package com.fjordtek.bookstore.model.book; + +/** +* +* @author Pekka Helenius +*/ + +public interface BookRepositoryCustom { + + void updateWithoutPrice(Book book); + +} \ No newline at end of file diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/model/book/BookRepositoryImpl.java b/bookstore/src/main/java/com/fjordtek/bookstore/model/book/BookRepositoryImpl.java new file mode 100644 index 0000000..5f5d778 --- /dev/null +++ b/bookstore/src/main/java/com/fjordtek/bookstore/model/book/BookRepositoryImpl.java @@ -0,0 +1,57 @@ +package com.fjordtek.bookstore.model.book; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.Query; + +import org.springframework.transaction.annotation.Transactional; + +/** + * + * @author Pekka Helenius + */ + +@Transactional +public class BookRepositoryImpl implements BookRepositoryCustom { + + @PersistenceContext + EntityManager entityManager; + + /* + * Selective update + * Hibernate takes care of entityManager transactions (begin, commit, rollback, close, etc.) + */ + @Override + public void updateWithoutPrice(Book book) { + try { + + + /* + * Book: refers to entity class, not table name! + */ + Query query = entityManager.createQuery( + "UPDATE Book SET" + + " author_id = :author," + + " category_id = :category," + + " isbn = :isbn," + + " title = :title," + + " year = :year" + + " WHERE id = :id" + ); + + query.setParameter("author", book.getAuthor().getId()); + query.setParameter("category", book.getCategory().getId()); + query.setParameter("isbn", book.getIsbn()); + query.setParameter("title", book.getTitle()); + query.setParameter("year", book.getYear()); + query.setParameter("id", book.getId()); + + query.executeUpdate(); + + } catch (Exception e) { + e.printStackTrace(); + } + + } + +} \ No newline at end of file