From df9234976a7ef26a0486dde75c3f3ea991551614 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Wed, 20 Jul 2022 14:57:53 -0400 Subject: [PATCH] This commit is done by Tanu Shree Sharma --- README.md | 8 +- .../spring5webapp/BootStrap/BootStrap.java | 65 +++++++++++ .../spring5webapp/domain/Author.java | 92 +++++++++++++++ .../spring5webapp/domain/Book.java | 104 +++++++++++++++++ .../spring5webapp/domain/Publisher.java | 106 ++++++++++++++++++ .../repositories/AuthorRepository.java | 10 ++ .../repositories/BookRepository.java | 10 ++ .../repositories/PublisherRepository.java | 10 ++ src/main/resources/application.properties | 1 + 9 files changed, 400 insertions(+), 6 deletions(-) create mode 100644 src/main/java/guru/springframework/spring5webapp/BootStrap/BootStrap.java create mode 100644 src/main/java/guru/springframework/spring5webapp/domain/Author.java create mode 100644 src/main/java/guru/springframework/spring5webapp/domain/Book.java create mode 100644 src/main/java/guru/springframework/spring5webapp/domain/Publisher.java create mode 100644 src/main/java/guru/springframework/spring5webapp/repositories/AuthorRepository.java create mode 100644 src/main/java/guru/springframework/spring5webapp/repositories/BookRepository.java create mode 100644 src/main/java/guru/springframework/spring5webapp/repositories/PublisherRepository.java diff --git a/README.md b/README.md index 7c94211cf9..15bf7b8dfd 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Got a question about your Spring Framework 5 course? [Checkout these FAQs!](http ### Recommended Versions | Recommended | Reference | Notes | | ----------- | --------- | ----- | -| Oracle Java 8 JDK | [Download](https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html) | Java 11 is okay, see notes about Java [on the course wiki](https://github.com/springframeworkguru/spring5webapp/wiki/Java-Version) | +| Oracle Java 11 JDK | [Download]([https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html](https://www.oracle.com/java/technologies/javase/jdk11-archive-downloads.html)) | Java 8 or Java 17 may also be used. | | IntelliJ 2018 or Higher | [Download](https://www.jetbrains.com/idea/download/) | Ultimate Edition recommended. Students can get a free 120 trial license [here](https://github.com/springframeworkguru/spring5webapp/wiki/Which-IDE-to-Use%3F#how-do-i-get-the-free-120-day-trial-to-intellij-ultimate) | | Maven 3.6.0 or higher | [Download](https://maven.apache.org/download.cgi) | [Installation Instructions](https://maven.apache.org/install.html)| | Gradle 4.8 or higher | [Download](https://gradle.org/install/) | **Note:** Use Version 5 or higher if using Java 11 | @@ -25,7 +25,7 @@ Got a question about your Spring Framework 5 course? [Checkout these FAQs!](http ## All Spring Framework Guru Courses ### Spring Framework 5 -* [Spring Framework 5: Beginner to Guru](https://www.udemy.com/testing-spring-boot-beginner-to-guru/?couponCode=GITHUB_REPO) - Get the most modern and comprehensive course available for the Spring Framework! Join over 8,200 over Guru's in an Slack community exclusive to this course! More than 3,700 students have given this 53 hour course a 5 star review! +* [Spring Framework 5: Beginner to Guru](https://www.udemy.com/testing-spring-boot-beginner-to-guru/?couponCode=GITHUB_REPO) - Get the most modern and comprehensive course available for the Spring Framework! Join over 17,200 over Guru's in an Slack community exclusive to this course! More than 5,700 students have given this 53 hour course a 5 star review! * [Spring Boot Microservices with Spring Cloud Beginner to Guru](https://www.udemy.com/course/spring-boot-microservices-with-spring-cloud-beginner-to-guru/?referralCode=6142D427AE53031FEF38) - Master Microservice Architectures Using Spring Boot 2 and Cloud Based Deployments with Spring Cloud and Docker * [Reactive Programming with Spring Framework 5](https://www.udemy.com/reactive-programming-with-spring-framework-5/?couponCode=GITHUB_REPO_SF5B2G) - Keep your skills razor sharp and take a deep dive into Reactive Programming! * [Testing Spring Boot: Beginner to Guru](https://www.udemy.com/testing-spring-boot-beginner-to-guru/?couponCode=GITHUB_REPO_SF5B2G) - ** Best Selling Course** Become an expert in testing Java and Spring Applications with JUnit 5, Mockito and much more! @@ -41,12 +41,8 @@ Got a question about your Spring Framework 5 course? [Checkout these FAQs!](http * [Ready for Production with Spring Boot Actuator](https://www.udemy.com/ready-for-production-with-spring-boot-actuator/?couponCode=GITHUB_REPO_SF5B2G) - Learn how to leverage Spring Boot Actuator to monitor your applications running in production. ### Web Development with Spring Framework -* [Angular 4 Java Developers](https://www.udemy.com/angular-4-java-developers/?couponCode=GITHUB_REPO_SF5B2G) - Learn how to use Angular with Spring Boot. Two flaming hot technologies! This is the only course on Udemy where you can learn how to use JHipster to rapidly build your next application! Also, be sure to checkout the Slack community for this course! * [Mastering Thymeleaf with Spring Boot](https://www.udemy.com/mastering-thymeleaf-with-spring/?couponCode=GITHUB_REPO_SF5B2G) - Once you learn Thymeleaf, you'll never want to go back to using JSPs for web development! -### Spring Framework 4 -* [Spring Core](https://www.udemy.com/spring-core/) - Learn the core of Spring Framework 4! -* [Spring Core Advanced](https://www.udemy.com/spring-core-advanced-beyond-the-basics/?couponCode=GITHUB_REPO_SF5B2G) - Go beyond the basics! Learn about Aspect Oriented Programming, Spring Security, using Spring Events, JMS and more! ## Connect with Spring Framework Guru * Spring Framework Guru [Blog](https://springframework.guru/) diff --git a/src/main/java/guru/springframework/spring5webapp/BootStrap/BootStrap.java b/src/main/java/guru/springframework/spring5webapp/BootStrap/BootStrap.java new file mode 100644 index 0000000000..0a6657e577 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/BootStrap/BootStrap.java @@ -0,0 +1,65 @@ +package guru.springframework.spring5webapp.BootStrap; + +import guru.springframework.spring5webapp.domain.Author; +import guru.springframework.spring5webapp.domain.Book; +import guru.springframework.spring5webapp.domain.Publisher; +import guru.springframework.spring5webapp.repositories.AuthorRepository; +import guru.springframework.spring5webapp.repositories.BookRepository; +import guru.springframework.spring5webapp.repositories.PublisherRepository; +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; + +@Component +public class BootStrap implements CommandLineRunner { + + private AuthorRepository authorRepository; + private BookRepository bookRepository; + private PublisherRepository publisherRepository; + + public BootStrap (AuthorRepository authorRepository, BookRepository bookRepository, PublisherRepository publisherRepository) { + this.authorRepository = authorRepository; + this.bookRepository = bookRepository; + this.publisherRepository = publisherRepository; + } + + @Override + public void run(String... args) throws Exception { + System.out.println("Starting in BootStrap"); + Publisher publisher = new Publisher(); + publisher.setName("SFG Publishing"); + publisher.setCity("St Petersburg"); + publisher.setState("FL"); + + publisherRepository.save(publisher); + + System.out.println("Publisher Count: " + publisherRepository.count()); + + Author eric = new Author("Eric", "Evans"); + Book ddd = new Book("Domain Driven Design", "123123",publisher); + eric.getBooks().add(ddd); + ddd.getAuthors().add(eric); + + ddd.setPublisher(publisher); + eric.getBooks().add(ddd); + authorRepository.save(eric); + bookRepository.save(ddd); + publisherRepository.save(publisher); + + Author rod = new Author("Rod", "Johnson"); + Book noEJB = new Book("J2EE Development without EJB", "23444", publisher ); + rod.getBooks().add(noEJB); + noEJB.getAuthors().add(rod); + noEJB.setPublisher(publisher); + rod.getBooks().add(noEJB); + + authorRepository.save(rod); + bookRepository.save(noEJB); + publisherRepository.save(publisher); + + System.out.println("Started in Bootstrap"); + System.out.println("Number of Books: " + bookRepository.count()); + System.out.println("Publisher's number of books = " + publisher.getBooks().stream().count()); + + } + +} \ No newline at end of file diff --git a/src/main/java/guru/springframework/spring5webapp/domain/Author.java b/src/main/java/guru/springframework/spring5webapp/domain/Author.java new file mode 100644 index 0000000000..92485cb0c5 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/domain/Author.java @@ -0,0 +1,92 @@ +package guru.springframework.spring5webapp.domain; + +import javax.persistence.*; +import java.util.HashSet; +import java.util.Set; + +/** + * Created by jt on 5/16/17. + */ +@Entity +public class Author { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + private String firstName; + private String lastName; + + @ManyToMany(mappedBy = "authors") + private Set books = new HashSet<>(); + + public Author() { + } + + public Author(String firstName, String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } + + public Author(String firstName, String lastName, Set books) { + this.firstName = firstName; + this.lastName = lastName; + this.books = books; + } + + 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; + } + + public Set getBooks() { + return books; + } + + public void setBooks(Set books) { + this.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 id != null ? id.equals(author.id) : author.id == null; + } + + @Override + public int hashCode() { + return id != null ? id.hashCode() : 0; + } + + @Override + public String toString() { + return "Author{" + + "id=" + id + + ", firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + ", books=" + books + + '}'; + } +} diff --git a/src/main/java/guru/springframework/spring5webapp/domain/Book.java b/src/main/java/guru/springframework/spring5webapp/domain/Book.java new file mode 100644 index 0000000000..887f8d7d2a --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/domain/Book.java @@ -0,0 +1,104 @@ +package guru.springframework.spring5webapp.domain; + +import javax.persistence.*; +import java.util.HashSet; +import java.util.Set; + +@Entity +public class Book { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + private String title; + private String isbn; + + @ManyToOne + private Publisher publisher; + + @ManyToMany + @JoinTable(name = "author_book", joinColumns = @JoinColumn(name = "book_id"), + inverseJoinColumns = @JoinColumn(name = "author_id")) + private Set authors = new HashSet<>(); + + public Book() { + } + + public Book(String title, String isbn, Publisher publisher) { + this.title = title; + this.isbn = isbn; + this.publisher = publisher; + } + + public Book(String title, String isbn, Publisher publisher, Set authors) { + this.title = title; + this.isbn = isbn; + this.publisher = publisher; + this.authors = authors; + } + + 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; + } + + 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; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Book book = (Book) o; + + return id != null ? id.equals(book.id) : book.id == null; + } + + @Override + public int hashCode() { + return id != null ? id.hashCode() : 0; + } + + @Override + public String toString() { + return "Book{" + + "id=" + id + + ", title='" + title + '\'' + + ", isbn='" + isbn + '\'' + + ", publisher='" + publisher + '\'' + + ", authors=" + authors + + '}'; + } +} diff --git a/src/main/java/guru/springframework/spring5webapp/domain/Publisher.java b/src/main/java/guru/springframework/spring5webapp/domain/Publisher.java new file mode 100644 index 0000000000..4f5d220d8f --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/domain/Publisher.java @@ -0,0 +1,106 @@ +package guru.springframework.spring5webapp.domain; + +import javax.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 name; + private String addressLine1; + private String city; + private String state; + private String zip; + @OneToMany + @JoinColumn(name = "publisher_id") + private Set books = new HashSet<>(); + + public Publisher() { + } + + @Override + public String toString() { + return "Publisher{" + + "id=" + id + + ", name='" + name + '\'' + + ", addressLine1='" + addressLine1 + '\'' + + ", city='" + city + '\'' + + ", state='" + state + '\'' + + ", 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 Objects.hash(id); + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getAddressLine1() { + return addressLine1; + } + + public void setAddressLine1(String addressLine1) { + this.addressLine1 = addressLine1; + } + + 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; + } + + public Set getBooks() { + return books; + } + + public void setBooks(Set books) { + this.books = books; + } +} diff --git a/src/main/java/guru/springframework/spring5webapp/repositories/AuthorRepository.java b/src/main/java/guru/springframework/spring5webapp/repositories/AuthorRepository.java new file mode 100644 index 0000000000..c4b58debea --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/repositories/AuthorRepository.java @@ -0,0 +1,10 @@ +package guru.springframework.spring5webapp.repositories; + +import guru.springframework.spring5webapp.domain.Author; +import org.springframework.data.repository.CrudRepository; + +/** + * Created by jt on 5/16/17. + */ +public interface AuthorRepository extends CrudRepository { +} diff --git a/src/main/java/guru/springframework/spring5webapp/repositories/BookRepository.java b/src/main/java/guru/springframework/spring5webapp/repositories/BookRepository.java new file mode 100644 index 0000000000..32d87c96df --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/repositories/BookRepository.java @@ -0,0 +1,10 @@ +package guru.springframework.spring5webapp.repositories; + +import guru.springframework.spring5webapp.domain.Book; +import org.springframework.data.repository.CrudRepository; + +/** + * Created by jt on 5/16/17. + */ +public interface BookRepository extends CrudRepository { +} diff --git a/src/main/java/guru/springframework/spring5webapp/repositories/PublisherRepository.java b/src/main/java/guru/springframework/spring5webapp/repositories/PublisherRepository.java new file mode 100644 index 0000000000..622a907736 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/repositories/PublisherRepository.java @@ -0,0 +1,10 @@ +package guru.springframework.spring5webapp.repositories; + +import guru.springframework.spring5webapp.domain.Publisher; +import org.springframework.data.repository.CrudRepository; + +/** + * Created by jt on 5/17/17. + */ +public interface PublisherRepository extends CrudRepository { +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e69de29bb2..69b89983cb 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.h2.console.enabled=true \ No newline at end of file