From 1c79e3a2983a811991338e87121cce5bad5581dc Mon Sep 17 00:00:00 2001 From: John Thompson Date: Tue, 16 May 2017 15:13:58 -0400 Subject: [PATCH 01/15] adding JPA example --- .../spring5webapp/model/Author.java | 67 ++++++++++++++++ .../spring5webapp/model/Book.java | 79 +++++++++++++++++++ src/main/resources/application.properties | 1 + 3 files changed, 147 insertions(+) create mode 100644 src/main/java/guru/springframework/spring5webapp/model/Author.java create mode 100644 src/main/java/guru/springframework/spring5webapp/model/Book.java diff --git a/src/main/java/guru/springframework/spring5webapp/model/Author.java b/src/main/java/guru/springframework/spring5webapp/model/Author.java new file mode 100644 index 0000000000..ff7d926325 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/model/Author.java @@ -0,0 +1,67 @@ +package guru.springframework.spring5webapp.model; + +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; + } +} diff --git a/src/main/java/guru/springframework/spring5webapp/model/Book.java b/src/main/java/guru/springframework/spring5webapp/model/Book.java new file mode 100644 index 0000000000..eb04853b53 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/model/Book.java @@ -0,0 +1,79 @@ +package guru.springframework.spring5webapp.model; + +import javax.persistence.*; +import java.util.HashSet; +import java.util.Set; + +/** + * Created by jt on 5/16/17. + */ +@Entity +public class Book { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + private String title; + private String isbn; + private String 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, String publisher) { + this.title = title; + this.isbn = isbn; + this.publisher = publisher; + } + + public Book(String title, String isbn, String 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 String getPublisher() { + return publisher; + } + + public void setPublisher(String publisher) { + this.publisher = publisher; + } + + public Set getAuthors() { + return authors; + } + + public void setAuthors(Set authors) { + this.authors = authors; + } +} 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 From af9c8e81c7a9e34f984eb621334aaff1de8b032e Mon Sep 17 00:00:00 2001 From: John Thompson Date: Tue, 16 May 2017 16:16:14 -0400 Subject: [PATCH 02/15] adding JPA example --- .../spring5webapp/repositories/AuthorRepository.java | 10 ++++++++++ .../spring5webapp/repositories/BookRepository.java | 10 ++++++++++ 2 files changed, 20 insertions(+) create mode 100644 src/main/java/guru/springframework/spring5webapp/repositories/AuthorRepository.java create mode 100644 src/main/java/guru/springframework/spring5webapp/repositories/BookRepository.java 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..e0946f3e5c --- /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.model.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..a38ff57e5f --- /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.model.Book; +import org.springframework.data.repository.CrudRepository; + +/** + * Created by jt on 5/16/17. + */ +public interface BookRepository extends CrudRepository { +} From 80bf60bd81c295e811907318c89feeeb34600f43 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Tue, 16 May 2017 16:44:15 -0400 Subject: [PATCH 03/15] adding bootstrap class --- .../spring5webapp/bootstrap/DevBootstrap.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/main/java/guru/springframework/spring5webapp/bootstrap/DevBootstrap.java diff --git a/src/main/java/guru/springframework/spring5webapp/bootstrap/DevBootstrap.java b/src/main/java/guru/springframework/spring5webapp/bootstrap/DevBootstrap.java new file mode 100644 index 0000000000..d298bdd1d4 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/bootstrap/DevBootstrap.java @@ -0,0 +1,50 @@ +package guru.springframework.spring5webapp.bootstrap; + +import guru.springframework.spring5webapp.model.Author; +import guru.springframework.spring5webapp.model.Book; +import guru.springframework.spring5webapp.repositories.AuthorRepository; +import guru.springframework.spring5webapp.repositories.BookRepository; +import org.springframework.context.ApplicationListener; +import org.springframework.context.event.ContextRefreshedEvent; +import org.springframework.stereotype.Component; + +/** + * Created by jt on 5/16/17. + */ +@Component +public class DevBootstrap implements ApplicationListener { + + private AuthorRepository authorRepository; + private BookRepository bookRepository; + + public DevBootstrap(AuthorRepository authorRepository, BookRepository bookRepository) { + this.authorRepository = authorRepository; + this.bookRepository = bookRepository; + } + + @Override + public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { + initData(); + } + + private void initData(){ + + //Eric + Author eric = new Author("Eric", "Evans"); + Book ddd = new Book("Domain Driven Design", "1234", "Harper Collins"); + eric.getBooks().add(ddd); + ddd.getAuthors().add(eric); + + authorRepository.save(eric); + bookRepository.save(ddd); + + + //Rod + Author rod = new Author("Rod", "Johnson"); + Book noEJB = new Book("J2EE Development without EJB", "23444", "Worx" ); + rod.getBooks().add(noEJB); + + authorRepository.save(rod); + bookRepository.save(noEJB); + } +} From b3c04f4d470cf662706ac6bb7b15a81ffb73d408 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Mon, 19 Jun 2017 13:54:32 -0400 Subject: [PATCH 04/15] Updating Spring Boot Version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index deb18b1374..a9350676b0 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.0.0.BUILD-SNAPSHOT + 2.0.0.M2 From 405e6a6a22d82a1589b4828a9901cb9d6f66935f Mon Sep 17 00:00:00 2001 From: John Thompson Date: Mon, 19 Jun 2017 13:55:31 -0400 Subject: [PATCH 05/15] Updating Spring Boot Version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index deb18b1374..a9350676b0 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.0.0.BUILD-SNAPSHOT + 2.0.0.M2 From 248ef06d7385669200bda7e0b5ec6a43304b080c Mon Sep 17 00:00:00 2001 From: John Thompson Date: Mon, 19 Jun 2017 13:55:59 -0400 Subject: [PATCH 06/15] Updating Spring Boot Version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index deb18b1374..a9350676b0 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.0.0.BUILD-SNAPSHOT + 2.0.0.M2 From ee5272e39285327876d7bd04d96a0ff2573cf9d8 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Mon, 31 Jul 2017 16:44:32 -0400 Subject: [PATCH 07/15] Updating Spring Boot Version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index a9350676b0..db64bfd00c 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.0.0.M2 + 2.0.0.M3 From a0f05efae5d0bd1c5e2c2c61fc6d492e854129f4 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Mon, 31 Jul 2017 16:46:31 -0400 Subject: [PATCH 08/15] Updating Spring Boot Version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index a9350676b0..db64bfd00c 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.0.0.M2 + 2.0.0.M3 From 6648cfb90dfe9a93dd7aafddff2f93bc8719f45a Mon Sep 17 00:00:00 2001 From: John Thompson Date: Mon, 31 Jul 2017 16:47:03 -0400 Subject: [PATCH 09/15] Updating Spring Boot Version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index a9350676b0..db64bfd00c 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.0.0.M2 + 2.0.0.M3 From 57ad1aeaff0fef4814d8cb64c6cfc2103005ae8f Mon Sep 17 00:00:00 2001 From: Tiago Teixeira Date: Thu, 24 Aug 2017 01:15:58 +0100 Subject: [PATCH 10/15] Corrected typo Worx to Wrox Book link: http://www.wrox.com/WileyCDA/WroxTitle/Expert-One-on-One-J2EE-Development-without-EJB.productCd-0764558315.html --- .../springframework/spring5webapp/bootstrap/DevBootstrap.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/guru/springframework/spring5webapp/bootstrap/DevBootstrap.java b/src/main/java/guru/springframework/spring5webapp/bootstrap/DevBootstrap.java index d298bdd1d4..3917936801 100644 --- a/src/main/java/guru/springframework/spring5webapp/bootstrap/DevBootstrap.java +++ b/src/main/java/guru/springframework/spring5webapp/bootstrap/DevBootstrap.java @@ -41,7 +41,7 @@ private void initData(){ //Rod Author rod = new Author("Rod", "Johnson"); - Book noEJB = new Book("J2EE Development without EJB", "23444", "Worx" ); + Book noEJB = new Book("J2EE Development without EJB", "23444", "Wrox" ); rod.getBooks().add(noEJB); authorRepository.save(rod); From 3692bd466bbd04e2f4da28c773262786a4fa679c Mon Sep 17 00:00:00 2001 From: John Thompson Date: Thu, 7 Sep 2017 14:53:36 -0400 Subject: [PATCH 11/15] adding equals, hash, and toString --- .../spring5webapp/model/Author.java | 25 ++++++++++++++++++ .../spring5webapp/model/Book.java | 26 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/main/java/guru/springframework/spring5webapp/model/Author.java b/src/main/java/guru/springframework/spring5webapp/model/Author.java index ff7d926325..1768e9c794 100644 --- a/src/main/java/guru/springframework/spring5webapp/model/Author.java +++ b/src/main/java/guru/springframework/spring5webapp/model/Author.java @@ -64,4 +64,29 @@ public Set getBooks() { 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/model/Book.java b/src/main/java/guru/springframework/spring5webapp/model/Book.java index eb04853b53..a1886b742c 100644 --- a/src/main/java/guru/springframework/spring5webapp/model/Book.java +++ b/src/main/java/guru/springframework/spring5webapp/model/Book.java @@ -76,4 +76,30 @@ public Set getAuthors() { 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 + + '}'; + } } From cade2fbcc036d9cc38da32526e59b40a75939726 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Wed, 4 Oct 2017 08:35:32 -0400 Subject: [PATCH 12/15] fix to Rod mapping --- .../springframework/spring5webapp/bootstrap/DevBootstrap.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/guru/springframework/spring5webapp/bootstrap/DevBootstrap.java b/src/main/java/guru/springframework/spring5webapp/bootstrap/DevBootstrap.java index 3917936801..a30f3a27f7 100644 --- a/src/main/java/guru/springframework/spring5webapp/bootstrap/DevBootstrap.java +++ b/src/main/java/guru/springframework/spring5webapp/bootstrap/DevBootstrap.java @@ -43,6 +43,7 @@ private void initData(){ Author rod = new Author("Rod", "Johnson"); Book noEJB = new Book("J2EE Development without EJB", "23444", "Wrox" ); rod.getBooks().add(noEJB); + noEJB.getAuthors().add(rod); authorRepository.save(rod); bookRepository.save(noEJB); From 4da7a2dfc3bfc15ed29fcdc83e9f031505d1f77d Mon Sep 17 00:00:00 2001 From: springframeworkguru Date: Sat, 9 Dec 2017 23:06:00 +0530 Subject: [PATCH 13/15] Updated to 2.0.0.M7 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 4495165e07..209f115ce4 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.0.0.M5 + 2.0.0.M7 From a13d2c30bcda9bcbe6f21b587f5e6b0c3fd3b0d8 Mon Sep 17 00:00:00 2001 From: springframeworkguru Date: Sat, 17 Mar 2018 19:40:40 +0530 Subject: [PATCH 14/15] Updated to Spring Boot 2.0.0.RELEASE --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 209f115ce4..c1260dc5bd 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.0.0.M7 + 2.0.0.RELEASE From edf52477423e2606f2db4f43fea0fc8e5c336629 Mon Sep 17 00:00:00 2001 From: springframeworkguru Date: Tue, 4 Dec 2018 00:25:08 +0530 Subject: [PATCH 15/15] Upgraded to Spring Boot 2.1.0.RELEASE --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index c1260dc5bd..eb12eaf80d 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.0.0.RELEASE + 2.1.0.RELEASE