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..118329aee0 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/domain/Author.java @@ -0,0 +1,79 @@ +package guru.springframework.spring5webapp.domain; + +import javax.persistence.*; +import java.util.Set; + +@Entity +public class Author { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long id; + private String firstName; + private String lastName; + + @ManyToMany(mappedBy = "authors") + private Set books; + + public Set getBooks() { + return books; + } + + public void setBooks(Set books) { + 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; + } + + @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 id == author.id; + } + + @Override + public int hashCode() { + return Long.hashCode(id); + } +} + + + + + + + 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..26a67f2d29 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/domain/Book.java @@ -0,0 +1,81 @@ +package guru.springframework.spring5webapp.domain; + +import javax.persistence.*; +import java.util.Set; + +@Entity +public class Book { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long id; + private String title; + private String isbn; + + @ManyToMany(mappedBy = "books") + @JoinTable(name = "author_book", joinColumns = @JoinColumn(name = "book_id"), inverseJoinColumns = @JoinColumn(name = "author_id")) + private Set authors; + + public Set getAuthors() { + return authors; + } + + public void setAuthors(Set authors) { + 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; + } + + @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 id == book.id; + } + + @Override + public int hashCode() { + return Long.hashCode(id); + } +} + + + + + + + +