From 42433dff6614031b3d7105c80d3038101b868c44 Mon Sep 17 00:00:00 2001 From: Igor Rudytskyy Date: Fri, 8 Sep 2023 12:45:07 +0200 Subject: [PATCH 1/5] changes on the way --- .../bootstrap/BootstrapData.java | 87 +++++++++++++++ .../spring6webapp/domain/Author.java | 45 +++++++- .../spring6webapp/domain/Book.java | 58 +++++++++- .../spring6webapp/domain/Publisher.java | 100 ++++++++++++++++++ .../repositories/AuthorRepository.java | 7 ++ .../repositories/BookRepository.java | 7 ++ .../repositories/PublisherRepository.java | 7 ++ 7 files changed, 303 insertions(+), 8 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..2d3bfcbb --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java @@ -0,0 +1,87 @@ +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("54767685"); + + Author rodSaved = authorRepository.save(rod); + Book noEJBSaved = bookRepository.save(noEJB); + + + ericSaved.getBooks().add(dddSaved); + rodSaved.getBooks().add(noEJBSaved); + + Publisher publisher = new Publisher(); + + publisher.setPublisherName("myPublisher"); + publisher.setAddress("streetStreet"); + publisher.setCity("City"); + publisher.setState("BW"); + publisher.setZip("12345"); + + Publisher savedPublisher = publisherRepository.save(publisher); + + ddd.setPublisher(savedPublisher); + noEJB.setPublisher(savedPublisher); + + authorRepository.save(ericSaved); + authorRepository.save(rodSaved); + bookRepository.save(dddSaved); + bookRepository.save(noEJB); + + + + + System.out.println("In Bootstrap"); + System.out.println("Publisher Count: " + publisherRepository.count()); + System.out.println("Author Count: " + authorRepository.count()); + System.out.println("Book Count: " + bookRepository.count()); + + + + + } +} diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Author.java b/src/main/java/guru/springframework/spring6webapp/domain/Author.java index b3abed06..cad8713f 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Author.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Author.java @@ -1,9 +1,10 @@ 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.HashSet; +import java.util.Objects; +import java.util.Set; /** * Created by jt, Spring Framework Guru. @@ -17,6 +18,17 @@ public class Author { private String firstName; private String lastName; + @ManyToMany(mappedBy = "authors") + private Set books = new HashSet<>(); + + public Set getBooks() { + return books; + } + + public void setBooks(Set books) { + this.books = books; + } + public Long getId() { return id; } @@ -40,4 +52,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 == null || getClass() != o.getClass()) return false; + + Author author = (Author) o; + + return Objects.equals(id, author.id); + } + + @Override + public int hashCode() { + return id != null ? id.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 7ff68c4c..3dccdda7 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Book.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Book.java @@ -1,9 +1,10 @@ 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.HashSet; +import java.util.Objects; +import java.util.Set; /** * Created by jt, Spring Framework Guru. @@ -17,6 +18,30 @@ 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 = new HashSet<>(); + + @ManyToOne + private Publisher publisher; + + public Publisher getPublisher() { + return publisher; + } + + public void setPublisher(Publisher publisher) { + this.publisher = publisher; + } + + public Set getAuthors() { + return authors; + } + + public void setAuthors(Set authors) { + this.authors = authors; + } + public Long getId() { return id; } @@ -40,4 +65,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 == null || getClass() != o.getClass()) return false; + + Book book = (Book) o; + + return Objects.equals(id, book.id); + } + + @Override + public int hashCode() { + return id != null ? id.hashCode() : 0; + } } 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..e87404c4 --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/domain/Publisher.java @@ -0,0 +1,100 @@ +package guru.springframework.spring6webapp.domain; + +import jakarta.persistence.*; + +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; + +@Entity +public class Publisher { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + private String publisherName; + private String address; // street name + private String city; + private String state; + private String zip; + + + @OneToMany(mappedBy = "publisher") + private Set books; + + + 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 boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Publisher publisher = (Publisher) o; + + return Objects.equals(id, publisher.id); + } + + @Override + public int hashCode() { + return id != null ? id.hashCode() : 0; + } + + @Override + public String toString() { + return "Publisher{" + + "id=" + id + + ", publisherName='" + publisherName + '\'' + + ", address='" + address + '\'' + + ", city='" + city + '\'' + + ", state='" + state + '\'' + + ", zip='" + zip + '\'' + + '}'; + } +} 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..384b6ad4 --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/repositories/BookRepository.java @@ -0,0 +1,7 @@ +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 d141f8abb7d7b1e3433b311faa184ad47dfd344c Mon Sep 17 00:00:00 2001 From: Igor Rudytskyy Date: Fri, 8 Sep 2023 19:12:31 +0200 Subject: [PATCH 2/5] author publisher fixed --- .../spring6webapp/bootstrap/BootstrapData.java | 5 ++++- .../springframework/spring6webapp/domain/Publisher.java | 7 +++++++ src/main/resources/application.properties | 1 + 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java b/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java index 2d3bfcbb..a193d1a1 100644 --- a/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java +++ b/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java @@ -50,9 +50,10 @@ public void run(String... args) throws Exception { Author rodSaved = authorRepository.save(rod); Book noEJBSaved = bookRepository.save(noEJB); - ericSaved.getBooks().add(dddSaved); rodSaved.getBooks().add(noEJBSaved); + dddSaved.getAuthors().add(ericSaved); + noEJBSaved.getAuthors().add(rodSaved); Publisher publisher = new Publisher(); @@ -63,6 +64,8 @@ public void run(String... args) throws Exception { publisher.setZip("12345"); Publisher savedPublisher = publisherRepository.save(publisher); + authorRepository.save(ericSaved); + authorRepository.save(rodSaved); ddd.setPublisher(savedPublisher); noEJB.setPublisher(savedPublisher); diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Publisher.java b/src/main/java/guru/springframework/spring6webapp/domain/Publisher.java index e87404c4..2a623de4 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Publisher.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Publisher.java @@ -22,6 +22,13 @@ public class Publisher { @OneToMany(mappedBy = "publisher") 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/resources/application.properties b/src/main/resources/application.properties index 8b137891..7555f052 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,2 @@ +spring.h2.console.enabled=true From 1a23b9d45506441b634a9ff821bbb278203d3d07 Mon Sep 17 00:00:00 2001 From: Igor Rudytskyy Date: Sat, 9 Sep 2023 09:46:39 +0200 Subject: [PATCH 3/5] changes compare --- .../spring6webapp/bootstrap/BootstrapData.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java b/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java index a193d1a1..f294be47 100644 --- a/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java +++ b/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java @@ -30,7 +30,6 @@ public void run(String... args) throws Exception { eric.setFirstName("Eric"); eric.setLastName("Evans"); - Book ddd = new Book(); ddd.setTitle("Domain Driven Design"); ddd.setIsbn("123456"); @@ -39,7 +38,6 @@ public void run(String... args) throws Exception { Book dddSaved = bookRepository.save(ddd); Author rod = new Author(); - rod.setFirstName("Rod"); rod.setLastName("Johnson"); @@ -76,8 +74,6 @@ public void run(String... args) throws Exception { bookRepository.save(noEJB); - - System.out.println("In Bootstrap"); System.out.println("Publisher Count: " + publisherRepository.count()); System.out.println("Author Count: " + authorRepository.count()); From 7ca9ff0ec3ff991f836a6c9c0bcbd6ada3c68045 Mon Sep 17 00:00:00 2001 From: Igor Rudytskyy Date: Sat, 9 Sep 2023 09:46:39 +0200 Subject: [PATCH 4/5] changes compare --- .../spring6webapp/bootstrap/BootstrapData.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java b/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java index a193d1a1..f294be47 100644 --- a/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java +++ b/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java @@ -30,7 +30,6 @@ public void run(String... args) throws Exception { eric.setFirstName("Eric"); eric.setLastName("Evans"); - Book ddd = new Book(); ddd.setTitle("Domain Driven Design"); ddd.setIsbn("123456"); @@ -39,7 +38,6 @@ public void run(String... args) throws Exception { Book dddSaved = bookRepository.save(ddd); Author rod = new Author(); - rod.setFirstName("Rod"); rod.setLastName("Johnson"); @@ -76,8 +74,6 @@ public void run(String... args) throws Exception { bookRepository.save(noEJB); - - System.out.println("In Bootstrap"); System.out.println("Publisher Count: " + publisherRepository.count()); System.out.println("Author Count: " + authorRepository.count()); From c4e652d922e9a013e698a5227848673b7a2c8a00 Mon Sep 17 00:00:00 2001 From: Igor Rudytskyy Date: Sun, 10 Sep 2023 14:53:25 +0200 Subject: [PATCH 5/5] changes compare --- .../springframework/spring6webapp/bootstrap/BootstrapData.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java b/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java index f294be47..3b1ddcbc 100644 --- a/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java +++ b/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java @@ -74,6 +74,7 @@ public void run(String... args) throws Exception { bookRepository.save(noEJB); + System.out.println("In Bootstrap"); System.out.println("Publisher Count: " + publisherRepository.count()); System.out.println("Author Count: " + authorRepository.count());