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/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/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