diff --git a/pom.xml b/pom.xml index 4495165e07..eb12eaf80d 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.0.0.M5 + 2.1.0.RELEASE 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..a30f3a27f7 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/bootstrap/DevBootstrap.java @@ -0,0 +1,51 @@ +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", "Wrox" ); + rod.getBooks().add(noEJB); + noEJB.getAuthors().add(rod); + + authorRepository.save(rod); + bookRepository.save(noEJB); + } +} 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..1768e9c794 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/model/Author.java @@ -0,0 +1,92 @@ +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; + } + + @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 new file mode 100644 index 0000000000..a1886b742c --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/model/Book.java @@ -0,0 +1,105 @@ +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; + } + + @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/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 { +} 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