diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java b/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java index 1b994a7..010fab7 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java @@ -267,17 +267,42 @@ public class BookController { BookHash bookHash = bookHashRepository.findByHashId(bookHashId); if (bookHash == null) { - bindingResult.rejectValue("name", "error.user", "Wrong book"); - } - Long bookId = bookHash.getBookId(); - - // TODO consider better solution. Add custom Hibernate annotation for Book class? - Book bookI = bookRepository.findByIsbn(book.getIsbn()); - - // If existing ISBN value is not attached to the current book... - if (bookI != null) { - if (bookI.getId() != bookId) { - bindingResult.rejectValue("isbn", "error.user", "ISBN code already exists"); + bindingResult.rejectValue("name", "error.user", "Unknown book"); + } else { + + // One-to-one unidirectional relationship handling + /* + * Must be set. Otherwise, we get TemplateProcessingException + * when user puts invalid field values + * (Thymeleaf template engine can't handle book.bookHash.hashId). + */ + book.setBookHash(bookHash); + /* + * Must be set. Otherwise, we may get merge errors when user + * has preceding error inputs in this view. + */ + bookHash.setBook(book); + + // TODO consider better solution. Add custom Hibernate annotation for Book class? + Book bookI = bookRepository.findByIsbn(book.getIsbn()); + Long bookId = bookHash.getBookId(); + + if (bookId == null) { + bindingResult.rejectValue("name", "error.user", "Unknown book"); + } else { + + // If existing ISBN value is not attached to the current book... + if (bookI != null) { + if (bookI.getId() != bookId) { + bindingResult.rejectValue("isbn", "error.user", "ISBN code already exists"); + } + } + + /* + * This is necessary so that Hibernate does not attempt to INSERT data + * but UPDATEs current table data. + */ + book.setId(bookId); } } @@ -287,12 +312,6 @@ public class BookController { return bookEditPageView; } - /* - * This is necessary so that Hibernate does not attempt to INSERT data - * but UPDATEs current table data. - */ - book.setId(bookId); - bookAuthorHelper.detectAndSaveUpdateAuthorForBook(book); bookRepository.save(book);