From 6b3f0add5fed2409da55108c25b9a5fb076a14f3 Mon Sep 17 00:00:00 2001 From: AndrewBergstrom Date: Sat, 25 Feb 2023 17:54:38 -0700 Subject: [PATCH 1/2] Setting up Author and Book class, creating new entities, getters, setters, equals, hascodes, and using toString() --- .../spring5webapp/domain/Author.java | 83 +++++++++++++++++ .../spring5webapp/domain/Book.java | 91 +++++++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 src/main/java/guru/springframework/spring5webapp/domain/Author.java create mode 100644 src/main/java/guru/springframework/spring5webapp/domain/Book.java 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..3d48ca67dc --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/domain/Author.java @@ -0,0 +1,83 @@ +package guru.springframework.spring5webapp.domain; + +import javax.persistence.*; +import java.util.Objects; +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 Author() { + } + + 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 String toString() { + return "Author{" + + "id=" + id + + ", firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + ", books=" + books + + '}'; + } + + // SETTING UP THE EQUALS & HASHCODE FUNCTION + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Author author = (Author) o; + + return Objects.equals(id, author.id); + } + + @Override + public int hashCode() { + return id != null ? id.hashCode() : 0; + } +} 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..a577f12d74 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/domain/Book.java @@ -0,0 +1,91 @@ +package guru.springframework.spring5webapp.domain; + +import javax.persistence.*; +import java.util.Objects; +import java.util.Set; + +@Entity +public class Book { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + private String title; + private String isbn; + + /* + Setting up the relationships between author and books + */ + @ManyToMany + @JoinTable(name = "author_book", joinColumns = @JoinColumn(name = "book_id"), + inverseJoinColumns = @JoinColumn(name = "author_id")) + private Set authors; + + public Book() { + } + + public Book(String title, String isbn, Set authors) { + this.title = title; + this.isbn = isbn; + + 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 Set getAuthors() { + return authors; + } + + public void setAuthors(Set authors) { + this.authors = authors; + } + + @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 Objects.equals(id, book.id); + } + + @Override + public int hashCode() { + return id != null ? id.hashCode() : 0; + } +} From 29d6d78d5407071849e0859eee032b2531c76934 Mon Sep 17 00:00:00 2001 From: AndrewBergstrom Date: Sat, 25 Feb 2023 18:02:14 -0700 Subject: [PATCH 2/2] author and book class set up' --- .../java/guru/springframework/spring5webapp/domain/Author.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/guru/springframework/spring5webapp/domain/Author.java b/src/main/java/guru/springframework/spring5webapp/domain/Author.java index 3d48ca67dc..5d578fb591 100644 --- a/src/main/java/guru/springframework/spring5webapp/domain/Author.java +++ b/src/main/java/guru/springframework/spring5webapp/domain/Author.java @@ -55,6 +55,8 @@ public void setBooks(Set books) { this.books = books; } + + // TO STRING FOR JSON TYPE DATA @Override public String toString() { return "Author{" +