From c46332e869a66e299fc003d6e5640506eb915e7c Mon Sep 17 00:00:00 2001 From: John Thompson Date: Thu, 21 Jul 2022 14:51:27 -0400 Subject: [PATCH 01/15] adding hibernate equity --- .../spring6webapp/domain/Author.java | 36 +++++++++++++++++++ .../spring6webapp/domain/Book.java | 33 +++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Author.java b/src/main/java/guru/springframework/spring6webapp/domain/Author.java index 8085f82e..815a3584 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Author.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Author.java @@ -50,4 +50,40 @@ public String getLastName() { public void setLastName(String lastName) { this.lastName = lastName; } + + @Override + public String toString() { + return "Author{" + + "id=" + id + + ", firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + ", books=" + books + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Author)) return false; + + Author author = (Author) o; + + return getId() != null ? getId().equals(author.getId()) : author.getId() == null; + } + + @Override + public int hashCode() { + return getId() != null ? getId().hashCode() : 0; + } } + + + + + + + + + + + diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Book.java b/src/main/java/guru/springframework/spring6webapp/domain/Book.java index cd3f6a3b..c6ea1d56 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Book.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Book.java @@ -52,4 +52,37 @@ public String getIsbn() { public void setIsbn(String isbn) { this.isbn = isbn; } + + @Override + public String toString() { + return "Book{" + + "id=" + id + + ", title='" + title + '\'' + + ", isbn='" + isbn + '\'' + + ", authors=" + authors + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Book)) return false; + + Book book = (Book) o; + + return getId() != null ? getId().equals(book.getId()) : book.getId() == null; + } + + @Override + public int hashCode() { + return getId() != null ? getId().hashCode() : 0; + } } + + + + + + + + From a43498c8ff816cec194087c8821f8bb96bcc3c95 Mon Sep 17 00:00:00 2001 From: Janek1010 Date: Sun, 12 Mar 2023 11:43:03 +0100 Subject: [PATCH 02/15] Lessonn 18 done --- .../spring6webapp/domain/Author.java | 25 +++++++++++++++++++ .../spring6webapp/domain/Book.java | 25 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Author.java b/src/main/java/guru/springframework/spring6webapp/domain/Author.java index 8085f82e..2ede0d63 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Author.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Author.java @@ -50,4 +50,29 @@ public String getLastName() { public void setLastName(String lastName) { this.lastName = lastName; } + + @Override + public String toString() { + return "Author{" + + "id=" + id + + ", firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + ", books=" + books + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Author)) return false; + + Author author = (Author) o; + + return getId() != null ? getId().equals(author.getId()) : author.getId() == null; + } + + @Override + public int hashCode() { + return getId() != null ? getId().hashCode() : 0; + } } diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Book.java b/src/main/java/guru/springframework/spring6webapp/domain/Book.java index cd3f6a3b..a5b78fc5 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Book.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Book.java @@ -52,4 +52,29 @@ public String getIsbn() { public void setIsbn(String isbn) { this.isbn = isbn; } + + @Override + public String toString() { + return "Book{" + + "id=" + id + + ", title='" + title + '\'' + + ", isbn='" + isbn + '\'' + + ", authors=" + authors + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Book)) return false; + + Book book = (Book) o; + + return getId() != null ? getId().equals(book.getId()) : book.getId() == null; + } + + @Override + public int hashCode() { + return getId() != null ? getId().hashCode() : 0; + } } From 994d6fd60de2ea261ac4ee5eaa3abbdc9524edf2 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Wed, 20 Jul 2022 15:57:56 -0400 Subject: [PATCH 03/15] adding example of JPA Entities --- .../spring6webapp/domain/Author.java | 43 +++++++++++++++++++ .../spring6webapp/domain/Book.java | 43 +++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 src/main/java/guru/springframework/spring6webapp/domain/Author.java create mode 100644 src/main/java/guru/springframework/spring6webapp/domain/Book.java diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Author.java b/src/main/java/guru/springframework/spring6webapp/domain/Author.java new file mode 100644 index 00000000..b3abed06 --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/domain/Author.java @@ -0,0 +1,43 @@ +package guru.springframework.spring6webapp.domain; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; + +/** + * Created by jt, Spring Framework Guru. + */ +@Entity +public class Author { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + private String firstName; + private String lastName; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } +} diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Book.java b/src/main/java/guru/springframework/spring6webapp/domain/Book.java new file mode 100644 index 00000000..7ff68c4c --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/domain/Book.java @@ -0,0 +1,43 @@ +package guru.springframework.spring6webapp.domain; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; + +/** + * Created by jt, Spring Framework Guru. + */ +@Entity +public class Book { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + private String title; + private String isbn; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getIsbn() { + return isbn; + } + + public void setIsbn(String isbn) { + this.isbn = isbn; + } +} From 7f203202bd49910618cf88242af588844b87a3e8 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Wed, 20 Jul 2022 16:34:14 -0400 Subject: [PATCH 04/15] adding relationships example --- .../spring6webapp/domain/Author.java | 18 +++++++++++++---- .../spring6webapp/domain/Book.java | 20 +++++++++++++++---- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Author.java b/src/main/java/guru/springframework/spring6webapp/domain/Author.java index b3abed06..8085f82e 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Author.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Author.java @@ -1,9 +1,8 @@ package guru.springframework.spring6webapp.domain; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; +import jakarta.persistence.*; + +import java.util.Set; /** * Created by jt, Spring Framework Guru. @@ -17,6 +16,17 @@ public class Author { private String firstName; private String lastName; + @ManyToMany(mappedBy = "authors") + private Set books; + + public Set getBooks() { + return books; + } + + public void setBooks(Set books) { + this.books = books; + } + public Long getId() { return id; } diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Book.java b/src/main/java/guru/springframework/spring6webapp/domain/Book.java index 7ff68c4c..cd3f6a3b 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Book.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Book.java @@ -1,9 +1,8 @@ package guru.springframework.spring6webapp.domain; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; +import jakarta.persistence.*; + +import java.util.Set; /** * Created by jt, Spring Framework Guru. @@ -17,6 +16,19 @@ public class Book { private String title; private String isbn; + @ManyToMany + @JoinTable(name = "author_book", joinColumns = @JoinColumn(name = "book_id"), + inverseJoinColumns = @JoinColumn(name = "author_id")) + private Set authors; + + public Set getAuthors() { + return authors; + } + + public void setAuthors(Set authors) { + this.authors = authors; + } + public Long getId() { return id; } From 62133330c0bfc8cbe75fdcdfd4576b9ed15a46a0 Mon Sep 17 00:00:00 2001 From: Janek1010 Date: Sun, 12 Mar 2023 11:43:03 +0100 Subject: [PATCH 05/15] Lessonn 18 done --- .../spring6webapp/domain/Author.java | 25 +++++++++++++++++++ .../spring6webapp/domain/Book.java | 25 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Author.java b/src/main/java/guru/springframework/spring6webapp/domain/Author.java index 8085f82e..2ede0d63 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Author.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Author.java @@ -50,4 +50,29 @@ public String getLastName() { public void setLastName(String lastName) { this.lastName = lastName; } + + @Override + public String toString() { + return "Author{" + + "id=" + id + + ", firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + ", books=" + books + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Author)) return false; + + Author author = (Author) o; + + return getId() != null ? getId().equals(author.getId()) : author.getId() == null; + } + + @Override + public int hashCode() { + return getId() != null ? getId().hashCode() : 0; + } } diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Book.java b/src/main/java/guru/springframework/spring6webapp/domain/Book.java index cd3f6a3b..a5b78fc5 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Book.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Book.java @@ -52,4 +52,29 @@ public String getIsbn() { public void setIsbn(String isbn) { this.isbn = isbn; } + + @Override + public String toString() { + return "Book{" + + "id=" + id + + ", title='" + title + '\'' + + ", isbn='" + isbn + '\'' + + ", authors=" + authors + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Book)) return false; + + Book book = (Book) o; + + return getId() != null ? getId().equals(book.getId()) : book.getId() == null; + } + + @Override + public int hashCode() { + return getId() != null ? getId().hashCode() : 0; + } } From 9f78ddde5b28f254fda44e7a3c8dd26403d2cd0b Mon Sep 17 00:00:00 2001 From: Janek1010 Date: Sun, 12 Mar 2023 11:49:41 +0100 Subject: [PATCH 06/15] Lessonn 18 done --- .../spring6webapp/domain/Author.java | 14 -------------- .../springframework/spring6webapp/domain/Book.java | 13 +------------ 2 files changed, 1 insertion(+), 26 deletions(-) diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Author.java b/src/main/java/guru/springframework/spring6webapp/domain/Author.java index f8fc6537..2ede0d63 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Author.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Author.java @@ -76,17 +76,3 @@ public int hashCode() { return getId() != null ? getId().hashCode() : 0; } } -<<<<<<< HEAD -======= - - - - - - - - - - - ->>>>>>> origin/2.1-jpa-equals diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Book.java b/src/main/java/guru/springframework/spring6webapp/domain/Book.java index 18364bfd..8d4833e8 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Book.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Book.java @@ -77,15 +77,4 @@ public boolean equals(Object o) { public int hashCode() { return getId() != null ? getId().hashCode() : 0; } -} -<<<<<<< HEAD -======= - - - - - - - - ->>>>>>> origin/2.1-jpa-equals +} \ No newline at end of file From d52d9976ea65cb362b3b09b20f8981f60533db9d Mon Sep 17 00:00:00 2001 From: Janek1010 Date: Tue, 14 Mar 2023 12:33:35 +0100 Subject: [PATCH 07/15] Lesson 20 done --- .../bootstrap/BootstrapData.java | 66 +++++++++++++ .../spring6webapp/domain/Author.java | 3 +- .../spring6webapp/domain/Book.java | 3 +- .../spring6webapp/domain/Publisher.java | 94 +++++++++++++++++++ .../repositories/AuthorRepository.java | 7 ++ .../repositories/BookRepository.java | 8 ++ .../repositories/PublisherRepository.java | 7 ++ 7 files changed, 186 insertions(+), 2 deletions(-) create mode 100644 src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java create mode 100644 src/main/java/guru/springframework/spring6webapp/domain/Publisher.java create mode 100644 src/main/java/guru/springframework/spring6webapp/repositories/AuthorRepository.java create mode 100644 src/main/java/guru/springframework/spring6webapp/repositories/BookRepository.java create mode 100644 src/main/java/guru/springframework/spring6webapp/repositories/PublisherRepository.java diff --git a/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java b/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java new file mode 100644 index 00000000..4934872b --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java @@ -0,0 +1,66 @@ +package guru.springframework.spring6webapp.bootstrap; + +import guru.springframework.spring6webapp.domain.Author; +import guru.springframework.spring6webapp.domain.Book; +import guru.springframework.spring6webapp.domain.Publisher; +import guru.springframework.spring6webapp.repositories.AuthorRepository; +import guru.springframework.spring6webapp.repositories.BookRepository; +import guru.springframework.spring6webapp.repositories.PublisherRepository; +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; + +@Component +public class BootstrapData implements CommandLineRunner { + private final AuthorRepository authorRepository; + private final BookRepository bookRepository; + private final PublisherRepository publisherRepository; + + public BootstrapData(AuthorRepository authorRepository, BookRepository bookRepository, PublisherRepository publisherRepository) { + this.authorRepository = authorRepository; + this.bookRepository = bookRepository; + this.publisherRepository = publisherRepository; + } + + @Override + public void run(String... args) throws Exception { + Author eric = new Author(); + eric.setFirstName("Eric"); + eric.setLastName("Evans"); + + Book ddd = new Book(); + ddd.setTitle("Domain Driven Design"); + ddd.setIsbn("123456"); + + Author ericSaved = authorRepository.save(eric); + Book dddSaved = bookRepository.save(ddd); + + Author rod = new Author(); + rod.setFirstName("Rod"); + rod.setLastName("Johnson"); + + Book noEJB = new Book(); + noEJB.setTitle("J2EE Development without EJB"); + noEJB.setIsbn("54757585"); + + Author rodSaved = authorRepository.save(rod); + Book noEJBSaved = bookRepository.save(noEJB); + + ericSaved.getBooks().add(dddSaved); + rodSaved.getBooks().add(noEJBSaved); + + authorRepository.save(ericSaved); + authorRepository.save(rodSaved); + + // my Task: + Publisher john = new Publisher(); + john.setPublisherName("John"); + john.setAddress("Warynskiego 6"); + + publisherRepository.save(john); + + System.out.println("In Bootstrap"); + System.out.println("Author Count: "+ authorRepository.count()); + System.out.println("Book Count: "+ bookRepository.count()); + System.out.println("Publisher Count: "+ publisherRepository.count()); + } +} diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Author.java b/src/main/java/guru/springframework/spring6webapp/domain/Author.java index 2ede0d63..1ce11859 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Author.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Author.java @@ -2,6 +2,7 @@ import jakarta.persistence.*; +import java.util.HashSet; import java.util.Set; /** @@ -17,7 +18,7 @@ public class Author { private String lastName; @ManyToMany(mappedBy = "authors") - private Set books; + private Set books = new HashSet<>(); public Set getBooks() { return books; diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Book.java b/src/main/java/guru/springframework/spring6webapp/domain/Book.java index 8d4833e8..5c402cb1 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Book.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Book.java @@ -2,6 +2,7 @@ import jakarta.persistence.*; +import java.util.HashSet; import java.util.Set; /** @@ -19,7 +20,7 @@ public class Book { @ManyToMany @JoinTable(name = "author_book", joinColumns = @JoinColumn(name = "book_id"), inverseJoinColumns = @JoinColumn(name = "author_id")) - private Set authors; + private Set authors = new HashSet<>(); public Set getAuthors() { return authors; diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Publisher.java b/src/main/java/guru/springframework/spring6webapp/domain/Publisher.java new file mode 100644 index 00000000..c6085937 --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/domain/Publisher.java @@ -0,0 +1,94 @@ +package guru.springframework.spring6webapp.domain; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; + +@Entity +public class Publisher { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + private String publisherName; + private String address; + private String city; + private String state; + private String zip; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getPublisherName() { + return publisherName; + } + + public void setPublisherName(String publisherName) { + this.publisherName = publisherName; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + public String getZip() { + return zip; + } + + public void setZip(String zip) { + this.zip = zip; + } + + @Override + public String toString() { + return "Publisher{" + + "id=" + id + + ", publisherName='" + publisherName + '\'' + + ", address='" + address + '\'' + + ", city='" + city + '\'' + + ", state='" + state + '\'' + + ", zip='" + zip + '\'' + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Publisher)) return false; + + Publisher publisher = (Publisher) o; + + return getId() != null ? getId().equals(publisher.getId()) : publisher.getId() == null; + } + + @Override + public int hashCode() { + return getId() != null ? getId().hashCode() : 0; + } +} diff --git a/src/main/java/guru/springframework/spring6webapp/repositories/AuthorRepository.java b/src/main/java/guru/springframework/spring6webapp/repositories/AuthorRepository.java new file mode 100644 index 00000000..5ff4c917 --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/repositories/AuthorRepository.java @@ -0,0 +1,7 @@ +package guru.springframework.spring6webapp.repositories; + +import guru.springframework.spring6webapp.domain.Author; +import org.springframework.data.repository.CrudRepository; + +public interface AuthorRepository extends CrudRepository { +} diff --git a/src/main/java/guru/springframework/spring6webapp/repositories/BookRepository.java b/src/main/java/guru/springframework/spring6webapp/repositories/BookRepository.java new file mode 100644 index 00000000..5b3dc62d --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/repositories/BookRepository.java @@ -0,0 +1,8 @@ +package guru.springframework.spring6webapp.repositories; + +import guru.springframework.spring6webapp.domain.Book; +import org.springframework.data.repository.CrudRepository; + +public interface BookRepository extends CrudRepository { + +} diff --git a/src/main/java/guru/springframework/spring6webapp/repositories/PublisherRepository.java b/src/main/java/guru/springframework/spring6webapp/repositories/PublisherRepository.java new file mode 100644 index 00000000..c0b3218c --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/repositories/PublisherRepository.java @@ -0,0 +1,7 @@ +package guru.springframework.spring6webapp.repositories; + +import guru.springframework.spring6webapp.domain.Publisher; +import org.springframework.data.repository.CrudRepository; + +public interface PublisherRepository extends CrudRepository { +} From 63240fdbdd540452dfe21bec530855c210c1ceb7 Mon Sep 17 00:00:00 2001 From: Janek1010 Date: Tue, 14 Mar 2023 21:11:58 +0100 Subject: [PATCH 08/15] Lesson 26 done --- pom.xml | 5 ++++ .../bootstrap/BootstrapData.java | 14 +++++++---- .../controllers/BookController.java | 23 +++++++++++++++++++ .../spring6webapp/domain/Book.java | 11 +++++++++ .../spring6webapp/domain/Publisher.java | 10 ++++---- .../spring6webapp/services/BookService.java | 8 +++++++ .../services/BookServiceImpl.java | 20 ++++++++++++++++ src/main/resources/application.properties | 2 +- src/main/resources/templates/books.html | 22 ++++++++++++++++++ 9 files changed, 106 insertions(+), 9 deletions(-) create mode 100644 src/main/java/guru/springframework/spring6webapp/controllers/BookController.java create mode 100644 src/main/java/guru/springframework/spring6webapp/services/BookService.java create mode 100644 src/main/java/guru/springframework/spring6webapp/services/BookServiceImpl.java create mode 100644 src/main/resources/templates/books.html diff --git a/pom.xml b/pom.xml index dcbb6cb4..cbe5d93e 100644 --- a/pom.xml +++ b/pom.xml @@ -27,6 +27,10 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-thymeleaf + com.h2database h2 @@ -37,6 +41,7 @@ spring-boot-starter-test test + diff --git a/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java b/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java index 4934872b..143295f6 100644 --- a/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java +++ b/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java @@ -47,16 +47,22 @@ public void run(String... args) throws Exception { ericSaved.getBooks().add(dddSaved); rodSaved.getBooks().add(noEJBSaved); - - authorRepository.save(ericSaved); - authorRepository.save(rodSaved); + dddSaved.getAuthors().add(ericSaved); + noEJBSaved.getAuthors().add(rodSaved); // my Task: Publisher john = new Publisher(); john.setPublisherName("John"); john.setAddress("Warynskiego 6"); + Publisher savedPublisher = publisherRepository.save(john); - publisherRepository.save(john); + dddSaved.setPublisher(savedPublisher); + noEJB.setPublisher(savedPublisher); + + authorRepository.save(ericSaved); + authorRepository.save(rodSaved); + bookRepository.save(dddSaved); + bookRepository.save(noEJBSaved); System.out.println("In Bootstrap"); System.out.println("Author Count: "+ authorRepository.count()); diff --git a/src/main/java/guru/springframework/spring6webapp/controllers/BookController.java b/src/main/java/guru/springframework/spring6webapp/controllers/BookController.java new file mode 100644 index 00000000..f2d7dfb3 --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/controllers/BookController.java @@ -0,0 +1,23 @@ +package guru.springframework.spring6webapp.controllers; + +import guru.springframework.spring6webapp.services.BookService; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +public class BookController { + private final BookService bookService; + + public BookController(BookService bookService) { + this.bookService = bookService; + } + + @RequestMapping("/books") + public String getBooks(Model model){ + + model.addAttribute("books", bookService.findAll()); + + return "books"; + } +} diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Book.java b/src/main/java/guru/springframework/spring6webapp/domain/Book.java index 5c402cb1..4de55676 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Book.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Book.java @@ -22,6 +22,17 @@ public class Book { inverseJoinColumns = @JoinColumn(name = "author_id")) private Set authors = new HashSet<>(); + @ManyToOne + private Publisher publisher; + + public Publisher getPublisher() { + return publisher; + } + + public void setPublisher(Publisher publisher) { + this.publisher = publisher; + } + public Set getAuthors() { return authors; } diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Publisher.java b/src/main/java/guru/springframework/spring6webapp/domain/Publisher.java index c6085937..a1674068 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Publisher.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Publisher.java @@ -1,9 +1,8 @@ package guru.springframework.spring6webapp.domain; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; +import jakarta.persistence.*; + +import java.util.Set; @Entity public class Publisher { @@ -17,6 +16,9 @@ public class Publisher { private String state; private String zip; + @OneToMany(mappedBy = "publisher") + private Set books; + public Long getId() { return id; } diff --git a/src/main/java/guru/springframework/spring6webapp/services/BookService.java b/src/main/java/guru/springframework/spring6webapp/services/BookService.java new file mode 100644 index 00000000..1cfef45f --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/services/BookService.java @@ -0,0 +1,8 @@ +package guru.springframework.spring6webapp.services; + +import guru.springframework.spring6webapp.domain.Book; + +public interface BookService { + + Iterable findAll(); +} diff --git a/src/main/java/guru/springframework/spring6webapp/services/BookServiceImpl.java b/src/main/java/guru/springframework/spring6webapp/services/BookServiceImpl.java new file mode 100644 index 00000000..d1b2b067 --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/services/BookServiceImpl.java @@ -0,0 +1,20 @@ +package guru.springframework.spring6webapp.services; + +import guru.springframework.spring6webapp.domain.Book; +import guru.springframework.spring6webapp.repositories.BookRepository; +import org.springframework.stereotype.Service; + +@Service +public class BookServiceImpl implements BookService { + + private final BookRepository bookRepository; + + public BookServiceImpl(BookRepository bookRepository) { + this.bookRepository = bookRepository; + } + + @Override + public Iterable findAll() { + return bookRepository.findAll(); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b137891..88bee100 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1 @@ - +spring.h2.console.enabled= true \ No newline at end of file diff --git a/src/main/resources/templates/books.html b/src/main/resources/templates/books.html new file mode 100644 index 00000000..bdc56407 --- /dev/null +++ b/src/main/resources/templates/books.html @@ -0,0 +1,22 @@ + + + + + Spring Framework Guru + + +

Book list

+ + + + + + + + + + + +
IDTitlePublisher
123Spring in Action Wrox
+ + \ No newline at end of file From 5c7a54c111407d34c9f443e707b015ebc3a8eb39 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Wed, 20 Jul 2022 15:57:56 -0400 Subject: [PATCH 09/15] adding example of JPA Entities --- .../spring6webapp/domain/Author.java | 13 +++++++++++++ .../springframework/spring6webapp/domain/Book.java | 13 +++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Author.java b/src/main/java/guru/springframework/spring6webapp/domain/Author.java index 2ede0d63..e1922692 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Author.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Author.java @@ -1,8 +1,15 @@ package guru.springframework.spring6webapp.domain; +<<<<<<< HEAD import jakarta.persistence.*; import java.util.Set; +======= +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +>>>>>>> 15c2a5a (adding example of JPA Entities) /** * Created by jt, Spring Framework Guru. @@ -16,6 +23,7 @@ public class Author { private String firstName; private String lastName; +<<<<<<< HEAD @ManyToMany(mappedBy = "authors") private Set books; @@ -27,6 +35,8 @@ public void setBooks(Set books) { this.books = books; } +======= +>>>>>>> 15c2a5a (adding example of JPA Entities) public Long getId() { return id; } @@ -50,6 +60,7 @@ public String getLastName() { public void setLastName(String lastName) { this.lastName = lastName; } +<<<<<<< HEAD @Override public String toString() { @@ -75,4 +86,6 @@ public boolean equals(Object o) { public int hashCode() { return getId() != null ? getId().hashCode() : 0; } +======= +>>>>>>> 15c2a5a (adding example of JPA Entities) } diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Book.java b/src/main/java/guru/springframework/spring6webapp/domain/Book.java index a5b78fc5..a1883131 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Book.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Book.java @@ -1,8 +1,15 @@ package guru.springframework.spring6webapp.domain; +<<<<<<< HEAD import jakarta.persistence.*; import java.util.Set; +======= +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +>>>>>>> 15c2a5a (adding example of JPA Entities) /** * Created by jt, Spring Framework Guru. @@ -16,6 +23,7 @@ public class Book { private String title; private String isbn; +<<<<<<< HEAD @ManyToMany @JoinTable(name = "author_book", joinColumns = @JoinColumn(name = "book_id"), inverseJoinColumns = @JoinColumn(name = "author_id")) @@ -29,6 +37,8 @@ public void setAuthors(Set authors) { this.authors = authors; } +======= +>>>>>>> 15c2a5a (adding example of JPA Entities) public Long getId() { return id; } @@ -52,6 +62,7 @@ public String getIsbn() { public void setIsbn(String isbn) { this.isbn = isbn; } +<<<<<<< HEAD @Override public String toString() { @@ -77,4 +88,6 @@ public boolean equals(Object o) { public int hashCode() { return getId() != null ? getId().hashCode() : 0; } +======= +>>>>>>> 15c2a5a (adding example of JPA Entities) } From 2ba1874b9c3c99ff0cbd0a87337f89ee2ab95ac9 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Wed, 20 Jul 2022 16:34:14 -0400 Subject: [PATCH 10/15] adding relationships example --- .../spring6webapp/domain/Author.java | 38 ------------------- .../spring6webapp/domain/Book.java | 38 ------------------- 2 files changed, 76 deletions(-) diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Author.java b/src/main/java/guru/springframework/spring6webapp/domain/Author.java index e1922692..8085f82e 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Author.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Author.java @@ -1,15 +1,8 @@ package guru.springframework.spring6webapp.domain; -<<<<<<< HEAD import jakarta.persistence.*; import java.util.Set; -======= -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; ->>>>>>> 15c2a5a (adding example of JPA Entities) /** * Created by jt, Spring Framework Guru. @@ -23,7 +16,6 @@ public class Author { private String firstName; private String lastName; -<<<<<<< HEAD @ManyToMany(mappedBy = "authors") private Set books; @@ -35,8 +27,6 @@ public void setBooks(Set books) { this.books = books; } -======= ->>>>>>> 15c2a5a (adding example of JPA Entities) public Long getId() { return id; } @@ -60,32 +50,4 @@ public String getLastName() { public void setLastName(String lastName) { this.lastName = lastName; } -<<<<<<< HEAD - - @Override - public String toString() { - return "Author{" + - "id=" + id + - ", firstName='" + firstName + '\'' + - ", lastName='" + lastName + '\'' + - ", books=" + books + - '}'; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof Author)) return false; - - Author author = (Author) o; - - return getId() != null ? getId().equals(author.getId()) : author.getId() == null; - } - - @Override - public int hashCode() { - return getId() != null ? getId().hashCode() : 0; - } -======= ->>>>>>> 15c2a5a (adding example of JPA Entities) } diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Book.java b/src/main/java/guru/springframework/spring6webapp/domain/Book.java index a1883131..cd3f6a3b 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Book.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Book.java @@ -1,15 +1,8 @@ package guru.springframework.spring6webapp.domain; -<<<<<<< HEAD import jakarta.persistence.*; import java.util.Set; -======= -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; ->>>>>>> 15c2a5a (adding example of JPA Entities) /** * Created by jt, Spring Framework Guru. @@ -23,7 +16,6 @@ public class Book { private String title; private String isbn; -<<<<<<< HEAD @ManyToMany @JoinTable(name = "author_book", joinColumns = @JoinColumn(name = "book_id"), inverseJoinColumns = @JoinColumn(name = "author_id")) @@ -37,8 +29,6 @@ public void setAuthors(Set authors) { this.authors = authors; } -======= ->>>>>>> 15c2a5a (adding example of JPA Entities) public Long getId() { return id; } @@ -62,32 +52,4 @@ public String getIsbn() { public void setIsbn(String isbn) { this.isbn = isbn; } -<<<<<<< HEAD - - @Override - public String toString() { - return "Book{" + - "id=" + id + - ", title='" + title + '\'' + - ", isbn='" + isbn + '\'' + - ", authors=" + authors + - '}'; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof Book)) return false; - - Book book = (Book) o; - - return getId() != null ? getId().equals(book.getId()) : book.getId() == null; - } - - @Override - public int hashCode() { - return getId() != null ? getId().hashCode() : 0; - } -======= ->>>>>>> 15c2a5a (adding example of JPA Entities) } From eeb6c437e47ed930391cedc3794f32cbee28ea5f Mon Sep 17 00:00:00 2001 From: John Thompson Date: Thu, 21 Jul 2022 14:51:27 -0400 Subject: [PATCH 11/15] adding hibernate equity --- .../spring6webapp/domain/Author.java | 36 +++++++++++++++++++ .../spring6webapp/domain/Book.java | 33 +++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Author.java b/src/main/java/guru/springframework/spring6webapp/domain/Author.java index 8085f82e..815a3584 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Author.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Author.java @@ -50,4 +50,40 @@ public String getLastName() { public void setLastName(String lastName) { this.lastName = lastName; } + + @Override + public String toString() { + return "Author{" + + "id=" + id + + ", firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + ", books=" + books + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Author)) return false; + + Author author = (Author) o; + + return getId() != null ? getId().equals(author.getId()) : author.getId() == null; + } + + @Override + public int hashCode() { + return getId() != null ? getId().hashCode() : 0; + } } + + + + + + + + + + + diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Book.java b/src/main/java/guru/springframework/spring6webapp/domain/Book.java index cd3f6a3b..c6ea1d56 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Book.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Book.java @@ -52,4 +52,37 @@ public String getIsbn() { public void setIsbn(String isbn) { this.isbn = isbn; } + + @Override + public String toString() { + return "Book{" + + "id=" + id + + ", title='" + title + '\'' + + ", isbn='" + isbn + '\'' + + ", authors=" + authors + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Book)) return false; + + Book book = (Book) o; + + return getId() != null ? getId().equals(book.getId()) : book.getId() == null; + } + + @Override + public int hashCode() { + return getId() != null ? getId().hashCode() : 0; + } } + + + + + + + + From 639a3fbd73134a3c4f347487423b0087e741e424 Mon Sep 17 00:00:00 2001 From: Janek1010 Date: Sun, 12 Mar 2023 11:49:41 +0100 Subject: [PATCH 12/15] Lessonn 18 done --- .../springframework/spring6webapp/domain/Author.java | 11 ----------- .../springframework/spring6webapp/domain/Book.java | 10 +--------- 2 files changed, 1 insertion(+), 20 deletions(-) diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Author.java b/src/main/java/guru/springframework/spring6webapp/domain/Author.java index 815a3584..2ede0d63 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Author.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Author.java @@ -76,14 +76,3 @@ public int hashCode() { return getId() != null ? getId().hashCode() : 0; } } - - - - - - - - - - - diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Book.java b/src/main/java/guru/springframework/spring6webapp/domain/Book.java index c6ea1d56..8d4833e8 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Book.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Book.java @@ -77,12 +77,4 @@ public boolean equals(Object o) { public int hashCode() { return getId() != null ? getId().hashCode() : 0; } -} - - - - - - - - +} \ No newline at end of file From 3a6ae9d788c2cafdeb0a1f7cbf1fc8facdc79ad8 Mon Sep 17 00:00:00 2001 From: Janek1010 Date: Tue, 14 Mar 2023 12:33:35 +0100 Subject: [PATCH 13/15] Lesson 20 done --- .../bootstrap/BootstrapData.java | 66 +++++++++++++ .../spring6webapp/domain/Author.java | 3 +- .../spring6webapp/domain/Book.java | 3 +- .../spring6webapp/domain/Publisher.java | 94 +++++++++++++++++++ .../repositories/AuthorRepository.java | 7 ++ .../repositories/BookRepository.java | 8 ++ .../repositories/PublisherRepository.java | 7 ++ 7 files changed, 186 insertions(+), 2 deletions(-) create mode 100644 src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java create mode 100644 src/main/java/guru/springframework/spring6webapp/domain/Publisher.java create mode 100644 src/main/java/guru/springframework/spring6webapp/repositories/AuthorRepository.java create mode 100644 src/main/java/guru/springframework/spring6webapp/repositories/BookRepository.java create mode 100644 src/main/java/guru/springframework/spring6webapp/repositories/PublisherRepository.java diff --git a/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java b/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java new file mode 100644 index 00000000..4934872b --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java @@ -0,0 +1,66 @@ +package guru.springframework.spring6webapp.bootstrap; + +import guru.springframework.spring6webapp.domain.Author; +import guru.springframework.spring6webapp.domain.Book; +import guru.springframework.spring6webapp.domain.Publisher; +import guru.springframework.spring6webapp.repositories.AuthorRepository; +import guru.springframework.spring6webapp.repositories.BookRepository; +import guru.springframework.spring6webapp.repositories.PublisherRepository; +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; + +@Component +public class BootstrapData implements CommandLineRunner { + private final AuthorRepository authorRepository; + private final BookRepository bookRepository; + private final PublisherRepository publisherRepository; + + public BootstrapData(AuthorRepository authorRepository, BookRepository bookRepository, PublisherRepository publisherRepository) { + this.authorRepository = authorRepository; + this.bookRepository = bookRepository; + this.publisherRepository = publisherRepository; + } + + @Override + public void run(String... args) throws Exception { + Author eric = new Author(); + eric.setFirstName("Eric"); + eric.setLastName("Evans"); + + Book ddd = new Book(); + ddd.setTitle("Domain Driven Design"); + ddd.setIsbn("123456"); + + Author ericSaved = authorRepository.save(eric); + Book dddSaved = bookRepository.save(ddd); + + Author rod = new Author(); + rod.setFirstName("Rod"); + rod.setLastName("Johnson"); + + Book noEJB = new Book(); + noEJB.setTitle("J2EE Development without EJB"); + noEJB.setIsbn("54757585"); + + Author rodSaved = authorRepository.save(rod); + Book noEJBSaved = bookRepository.save(noEJB); + + ericSaved.getBooks().add(dddSaved); + rodSaved.getBooks().add(noEJBSaved); + + authorRepository.save(ericSaved); + authorRepository.save(rodSaved); + + // my Task: + Publisher john = new Publisher(); + john.setPublisherName("John"); + john.setAddress("Warynskiego 6"); + + publisherRepository.save(john); + + System.out.println("In Bootstrap"); + System.out.println("Author Count: "+ authorRepository.count()); + System.out.println("Book Count: "+ bookRepository.count()); + System.out.println("Publisher Count: "+ publisherRepository.count()); + } +} diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Author.java b/src/main/java/guru/springframework/spring6webapp/domain/Author.java index 2ede0d63..1ce11859 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Author.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Author.java @@ -2,6 +2,7 @@ import jakarta.persistence.*; +import java.util.HashSet; import java.util.Set; /** @@ -17,7 +18,7 @@ public class Author { private String lastName; @ManyToMany(mappedBy = "authors") - private Set books; + private Set books = new HashSet<>(); public Set getBooks() { return books; diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Book.java b/src/main/java/guru/springframework/spring6webapp/domain/Book.java index 8d4833e8..5c402cb1 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Book.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Book.java @@ -2,6 +2,7 @@ import jakarta.persistence.*; +import java.util.HashSet; import java.util.Set; /** @@ -19,7 +20,7 @@ public class Book { @ManyToMany @JoinTable(name = "author_book", joinColumns = @JoinColumn(name = "book_id"), inverseJoinColumns = @JoinColumn(name = "author_id")) - private Set authors; + private Set authors = new HashSet<>(); public Set getAuthors() { return authors; diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Publisher.java b/src/main/java/guru/springframework/spring6webapp/domain/Publisher.java new file mode 100644 index 00000000..c6085937 --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/domain/Publisher.java @@ -0,0 +1,94 @@ +package guru.springframework.spring6webapp.domain; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; + +@Entity +public class Publisher { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + private String publisherName; + private String address; + private String city; + private String state; + private String zip; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getPublisherName() { + return publisherName; + } + + public void setPublisherName(String publisherName) { + this.publisherName = publisherName; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + public String getZip() { + return zip; + } + + public void setZip(String zip) { + this.zip = zip; + } + + @Override + public String toString() { + return "Publisher{" + + "id=" + id + + ", publisherName='" + publisherName + '\'' + + ", address='" + address + '\'' + + ", city='" + city + '\'' + + ", state='" + state + '\'' + + ", zip='" + zip + '\'' + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Publisher)) return false; + + Publisher publisher = (Publisher) o; + + return getId() != null ? getId().equals(publisher.getId()) : publisher.getId() == null; + } + + @Override + public int hashCode() { + return getId() != null ? getId().hashCode() : 0; + } +} diff --git a/src/main/java/guru/springframework/spring6webapp/repositories/AuthorRepository.java b/src/main/java/guru/springframework/spring6webapp/repositories/AuthorRepository.java new file mode 100644 index 00000000..5ff4c917 --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/repositories/AuthorRepository.java @@ -0,0 +1,7 @@ +package guru.springframework.spring6webapp.repositories; + +import guru.springframework.spring6webapp.domain.Author; +import org.springframework.data.repository.CrudRepository; + +public interface AuthorRepository extends CrudRepository { +} diff --git a/src/main/java/guru/springframework/spring6webapp/repositories/BookRepository.java b/src/main/java/guru/springframework/spring6webapp/repositories/BookRepository.java new file mode 100644 index 00000000..5b3dc62d --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/repositories/BookRepository.java @@ -0,0 +1,8 @@ +package guru.springframework.spring6webapp.repositories; + +import guru.springframework.spring6webapp.domain.Book; +import org.springframework.data.repository.CrudRepository; + +public interface BookRepository extends CrudRepository { + +} diff --git a/src/main/java/guru/springframework/spring6webapp/repositories/PublisherRepository.java b/src/main/java/guru/springframework/spring6webapp/repositories/PublisherRepository.java new file mode 100644 index 00000000..c0b3218c --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/repositories/PublisherRepository.java @@ -0,0 +1,7 @@ +package guru.springframework.spring6webapp.repositories; + +import guru.springframework.spring6webapp.domain.Publisher; +import org.springframework.data.repository.CrudRepository; + +public interface PublisherRepository extends CrudRepository { +} From 844a99e6ae5c5402ea2bcd474297cc4229b73a76 Mon Sep 17 00:00:00 2001 From: Janek1010 Date: Tue, 14 Mar 2023 21:11:58 +0100 Subject: [PATCH 14/15] Lesson 26 done --- pom.xml | 5 ++++ .../bootstrap/BootstrapData.java | 14 +++++++---- .../controllers/BookController.java | 23 +++++++++++++++++++ .../spring6webapp/domain/Book.java | 11 +++++++++ .../spring6webapp/domain/Publisher.java | 10 ++++---- .../spring6webapp/services/BookService.java | 8 +++++++ .../services/BookServiceImpl.java | 20 ++++++++++++++++ src/main/resources/application.properties | 2 +- src/main/resources/templates/books.html | 22 ++++++++++++++++++ 9 files changed, 106 insertions(+), 9 deletions(-) create mode 100644 src/main/java/guru/springframework/spring6webapp/controllers/BookController.java create mode 100644 src/main/java/guru/springframework/spring6webapp/services/BookService.java create mode 100644 src/main/java/guru/springframework/spring6webapp/services/BookServiceImpl.java create mode 100644 src/main/resources/templates/books.html diff --git a/pom.xml b/pom.xml index dcbb6cb4..cbe5d93e 100644 --- a/pom.xml +++ b/pom.xml @@ -27,6 +27,10 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-thymeleaf + com.h2database h2 @@ -37,6 +41,7 @@ spring-boot-starter-test test + diff --git a/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java b/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java index 4934872b..143295f6 100644 --- a/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java +++ b/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java @@ -47,16 +47,22 @@ public void run(String... args) throws Exception { ericSaved.getBooks().add(dddSaved); rodSaved.getBooks().add(noEJBSaved); - - authorRepository.save(ericSaved); - authorRepository.save(rodSaved); + dddSaved.getAuthors().add(ericSaved); + noEJBSaved.getAuthors().add(rodSaved); // my Task: Publisher john = new Publisher(); john.setPublisherName("John"); john.setAddress("Warynskiego 6"); + Publisher savedPublisher = publisherRepository.save(john); - publisherRepository.save(john); + dddSaved.setPublisher(savedPublisher); + noEJB.setPublisher(savedPublisher); + + authorRepository.save(ericSaved); + authorRepository.save(rodSaved); + bookRepository.save(dddSaved); + bookRepository.save(noEJBSaved); System.out.println("In Bootstrap"); System.out.println("Author Count: "+ authorRepository.count()); diff --git a/src/main/java/guru/springframework/spring6webapp/controllers/BookController.java b/src/main/java/guru/springframework/spring6webapp/controllers/BookController.java new file mode 100644 index 00000000..f2d7dfb3 --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/controllers/BookController.java @@ -0,0 +1,23 @@ +package guru.springframework.spring6webapp.controllers; + +import guru.springframework.spring6webapp.services.BookService; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +public class BookController { + private final BookService bookService; + + public BookController(BookService bookService) { + this.bookService = bookService; + } + + @RequestMapping("/books") + public String getBooks(Model model){ + + model.addAttribute("books", bookService.findAll()); + + return "books"; + } +} diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Book.java b/src/main/java/guru/springframework/spring6webapp/domain/Book.java index 5c402cb1..4de55676 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Book.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Book.java @@ -22,6 +22,17 @@ public class Book { inverseJoinColumns = @JoinColumn(name = "author_id")) private Set authors = new HashSet<>(); + @ManyToOne + private Publisher publisher; + + public Publisher getPublisher() { + return publisher; + } + + public void setPublisher(Publisher publisher) { + this.publisher = publisher; + } + public Set getAuthors() { return authors; } diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Publisher.java b/src/main/java/guru/springframework/spring6webapp/domain/Publisher.java index c6085937..a1674068 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Publisher.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Publisher.java @@ -1,9 +1,8 @@ package guru.springframework.spring6webapp.domain; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; +import jakarta.persistence.*; + +import java.util.Set; @Entity public class Publisher { @@ -17,6 +16,9 @@ public class Publisher { private String state; private String zip; + @OneToMany(mappedBy = "publisher") + private Set books; + public Long getId() { return id; } diff --git a/src/main/java/guru/springframework/spring6webapp/services/BookService.java b/src/main/java/guru/springframework/spring6webapp/services/BookService.java new file mode 100644 index 00000000..1cfef45f --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/services/BookService.java @@ -0,0 +1,8 @@ +package guru.springframework.spring6webapp.services; + +import guru.springframework.spring6webapp.domain.Book; + +public interface BookService { + + Iterable findAll(); +} diff --git a/src/main/java/guru/springframework/spring6webapp/services/BookServiceImpl.java b/src/main/java/guru/springframework/spring6webapp/services/BookServiceImpl.java new file mode 100644 index 00000000..d1b2b067 --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/services/BookServiceImpl.java @@ -0,0 +1,20 @@ +package guru.springframework.spring6webapp.services; + +import guru.springframework.spring6webapp.domain.Book; +import guru.springframework.spring6webapp.repositories.BookRepository; +import org.springframework.stereotype.Service; + +@Service +public class BookServiceImpl implements BookService { + + private final BookRepository bookRepository; + + public BookServiceImpl(BookRepository bookRepository) { + this.bookRepository = bookRepository; + } + + @Override + public Iterable findAll() { + return bookRepository.findAll(); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b137891..88bee100 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1 @@ - +spring.h2.console.enabled= true \ No newline at end of file diff --git a/src/main/resources/templates/books.html b/src/main/resources/templates/books.html new file mode 100644 index 00000000..bdc56407 --- /dev/null +++ b/src/main/resources/templates/books.html @@ -0,0 +1,22 @@ + + + + + Spring Framework Guru + + +

Book list

+ + + + + + + + + + + +
IDTitlePublisher
123Spring in Action Wrox
+ + \ No newline at end of file From cb1cd425af3cda620a1e838d38ef74c136a6eabb Mon Sep 17 00:00:00 2001 From: Janek1010 Date: Mon, 24 Apr 2023 22:05:43 +0200 Subject: [PATCH 15/15] update --- .../bootstrap/BootstrapData.java | 3 +++ .../controllers/AuthorController.java | 23 +++++++++++++++++++ .../spring6webapp/services/AuthorService.java | 7 ++++++ .../services/AuthorServiceImpl.java | 19 +++++++++++++++ src/main/resources/templates/authors.html | 22 ++++++++++++++++++ 5 files changed, 74 insertions(+) create mode 100644 src/main/java/guru/springframework/spring6webapp/controllers/AuthorController.java create mode 100644 src/main/java/guru/springframework/spring6webapp/services/AuthorService.java create mode 100644 src/main/java/guru/springframework/spring6webapp/services/AuthorServiceImpl.java create mode 100644 src/main/resources/templates/authors.html diff --git a/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java b/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java index 143295f6..e1e7eee4 100644 --- a/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java +++ b/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java @@ -30,6 +30,7 @@ public void run(String... args) throws Exception { Book ddd = new Book(); ddd.setTitle("Domain Driven Design"); ddd.setIsbn("123456"); + System.out.println(); Author ericSaved = authorRepository.save(eric); Book dddSaved = bookRepository.save(ddd); @@ -59,6 +60,8 @@ public void run(String... args) throws Exception { dddSaved.setPublisher(savedPublisher); noEJB.setPublisher(savedPublisher); + + authorRepository.save(ericSaved); authorRepository.save(rodSaved); bookRepository.save(dddSaved); diff --git a/src/main/java/guru/springframework/spring6webapp/controllers/AuthorController.java b/src/main/java/guru/springframework/spring6webapp/controllers/AuthorController.java new file mode 100644 index 00000000..8fc98ecb --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/controllers/AuthorController.java @@ -0,0 +1,23 @@ +package guru.springframework.spring6webapp.controllers; + +import guru.springframework.spring6webapp.services.AuthorService; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + + +@Controller +public class AuthorController { + private final AuthorService authorService; + + public AuthorController(AuthorService authorService) { + this.authorService = authorService; + } + @RequestMapping("/authors") + public String getAuthors(Model model){ + + model.addAttribute("authors", authorService.findAll()); + return "authors"; + } + +} diff --git a/src/main/java/guru/springframework/spring6webapp/services/AuthorService.java b/src/main/java/guru/springframework/spring6webapp/services/AuthorService.java new file mode 100644 index 00000000..3f6ade1a --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/services/AuthorService.java @@ -0,0 +1,7 @@ +package guru.springframework.spring6webapp.services; + +import guru.springframework.spring6webapp.domain.Author; + +public interface AuthorService { + Iterable findAll(); +} diff --git a/src/main/java/guru/springframework/spring6webapp/services/AuthorServiceImpl.java b/src/main/java/guru/springframework/spring6webapp/services/AuthorServiceImpl.java new file mode 100644 index 00000000..77dbe6a2 --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/services/AuthorServiceImpl.java @@ -0,0 +1,19 @@ +package guru.springframework.spring6webapp.services; + +import guru.springframework.spring6webapp.domain.Author; +import guru.springframework.spring6webapp.repositories.AuthorRepository; +import org.springframework.stereotype.Service; + +@Service +public class AuthorServiceImpl implements AuthorService{ + private final AuthorRepository authorRepository; + + public AuthorServiceImpl(AuthorRepository authorRepository) { + this.authorRepository = authorRepository; + } + + @Override + public Iterable findAll() { + return authorRepository.findAll(); + } +} diff --git a/src/main/resources/templates/authors.html b/src/main/resources/templates/authors.html new file mode 100644 index 00000000..0aaabaef --- /dev/null +++ b/src/main/resources/templates/authors.html @@ -0,0 +1,22 @@ + + + + + Spring Framework Guru + + +

Author list

+ + + + + + + + + + + +
IDTitlePublisher
123Spring in Action Wrox
+ + \ No newline at end of file