diff --git a/src/main/java/ru/redrise/marinesco/data/BookRepository.java b/src/main/java/ru/redrise/marinesco/data/BookRepository.java index 00d4da0..f656335 100644 --- a/src/main/java/ru/redrise/marinesco/data/BookRepository.java +++ b/src/main/java/ru/redrise/marinesco/data/BookRepository.java @@ -5,16 +5,10 @@ import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; -import ru.redrise.marinesco.library.Author; import ru.redrise.marinesco.library.Book; - - - @Repository public interface BookRepository extends JpaRepository{ List findBySeriesContainingIgnoreCase(String title); List findByTitleContainingIgnoreCase(String title); - - List findAllByAuthorsContains(Author author); } \ No newline at end of file diff --git a/src/main/java/ru/redrise/marinesco/library/Author.java b/src/main/java/ru/redrise/marinesco/library/Author.java index f36e673..c109bee 100644 --- a/src/main/java/ru/redrise/marinesco/library/Author.java +++ b/src/main/java/ru/redrise/marinesco/library/Author.java @@ -1,7 +1,10 @@ package ru.redrise.marinesco.library; +import java.util.List; + import jakarta.persistence.Entity; import jakarta.persistence.Id; +import jakarta.persistence.ManyToMany; import lombok.AccessLevel; import lombok.Data; import lombok.NoArgsConstructor; @@ -14,6 +17,9 @@ public class Author { private Long id; private String authorName; + @ManyToMany(mappedBy = "authors") + private List books; + public Author(String name){ this.authorName = name; this.id = (long) name.hashCode(); diff --git a/src/main/java/ru/redrise/marinesco/library/Book.java b/src/main/java/ru/redrise/marinesco/library/Book.java index b6398a1..2c5d7f0 100644 --- a/src/main/java/ru/redrise/marinesco/library/Book.java +++ b/src/main/java/ru/redrise/marinesco/library/Book.java @@ -8,6 +8,7 @@ import java.util.Set; import jakarta.persistence.Entity; import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToMany; import jakarta.persistence.Transient; import lombok.AccessLevel; diff --git a/src/main/java/ru/redrise/marinesco/library/web/AuthorController.java b/src/main/java/ru/redrise/marinesco/library/web/AuthorController.java index 4b1b793..44c39a6 100644 --- a/src/main/java/ru/redrise/marinesco/library/web/AuthorController.java +++ b/src/main/java/ru/redrise/marinesco/library/web/AuthorController.java @@ -9,7 +9,6 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import ru.redrise.marinesco.data.AuthorRepository; -import ru.redrise.marinesco.data.BookRepository; import ru.redrise.marinesco.library.Author; import ru.redrise.marinesco.library.Book; @@ -17,11 +16,9 @@ import ru.redrise.marinesco.library.Book; @RequestMapping("/author") public class AuthorController { private AuthorRepository authorRepository; - private BookRepository bookRepository; - public AuthorController(AuthorRepository authorRepository, BookRepository bookRepository){ + public AuthorController(AuthorRepository authorRepository){ this.authorRepository = authorRepository; - this.bookRepository = bookRepository; } @GetMapping("/{authorId}") @@ -33,7 +30,7 @@ public class AuthorController { return "author"; } - List books = bookRepository.findAllByAuthorsContains(author); + List books = author.getBooks(); model.addAttribute("author", author); model.addAttribute("books", books);