diff --git a/SpringApp/library/src/main/java/com/ip/library/controllers/authors/AuthorEntity.java b/SpringApp/library/src/main/java/com/ip/library/controllers/authors/AuthorEntity.java index 629efec..8fc1191 100644 --- a/SpringApp/library/src/main/java/com/ip/library/controllers/authors/AuthorEntity.java +++ b/SpringApp/library/src/main/java/com/ip/library/controllers/authors/AuthorEntity.java @@ -1,18 +1,13 @@ package com.ip.library.controllers.authors; -import java.util.HashSet; import java.util.Objects; -import java.util.Set; import com.ip.library.controllers.authors_books.AuthorsBooksEntity; import com.ip.library.controllers.books.BookEntity; import com.ip.library.core.model.BaseEntity; -import jakarta.persistence.CascadeType; import jakarta.persistence.Column; import jakarta.persistence.Entity; -import jakarta.persistence.OneToMany; -import jakarta.persistence.OrderBy; import jakarta.persistence.Table; @Entity @@ -20,9 +15,6 @@ import jakarta.persistence.Table; public class AuthorEntity extends BaseEntity { @Column(nullable = false, unique = true, length = 20) private String name; - @OneToMany(mappedBy = "author", cascade = CascadeType.ALL, orphanRemoval = true) - @OrderBy("id ASC") - private Set authorsBooks = new HashSet<>(); public AuthorEntity() { super(); @@ -40,20 +32,11 @@ public class AuthorEntity extends BaseEntity { this.name = name; } - public Set getAuthorsBooks() { - return authorsBooks; - } - - public void setBooks(Set authorsBooks) { - this.authorsBooks = authorsBooks; - } - public boolean addBook(BookEntity book) { AuthorsBooksEntity entity = new AuthorsBooksEntity(this, book); - boolean result = authorsBooks.add(entity); if (!book.getAuthorsBooks().contains(entity)) book.getAuthorsBooks().add(entity); - return result; + return true; } @Override diff --git a/SpringApp/library/src/main/java/com/ip/library/controllers/authors_books/AuthorsBooksEntity.java b/SpringApp/library/src/main/java/com/ip/library/controllers/authors_books/AuthorsBooksEntity.java index e9662ca..3e809fa 100644 --- a/SpringApp/library/src/main/java/com/ip/library/controllers/authors_books/AuthorsBooksEntity.java +++ b/SpringApp/library/src/main/java/com/ip/library/controllers/authors_books/AuthorsBooksEntity.java @@ -43,8 +43,6 @@ public class AuthorsBooksEntity { public void setAuthor(AuthorEntity author) { this.author = author; - if (!author.getAuthorsBooks().contains(this)) - author.getAuthorsBooks().add(this); } public AuthorEntity getAuthor() { diff --git a/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookEntity.java b/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookEntity.java index b292f10..8d6a3bf 100644 --- a/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookEntity.java +++ b/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookEntity.java @@ -29,7 +29,7 @@ public class BookEntity extends BaseEntity { @JoinColumn(name = "type_id", nullable = false) @OrderBy("id ASC") private TypeEntity type; - @OneToMany(mappedBy = "book", cascade = CascadeType.ALL, fetch = FetchType.EAGER) + @OneToMany(mappedBy = "book", cascade = CascadeType.ALL) @OrderBy("id ASC") private Set authorsBooks = new HashSet<>(); @@ -68,10 +68,7 @@ public class BookEntity extends BaseEntity { public boolean addAuthor(AuthorEntity author) { AuthorsBooksEntity entity = new AuthorsBooksEntity(author, this); - boolean result = authorsBooks.add(entity); - if (!author.getAuthorsBooks().contains(entity)) - author.getAuthorsBooks().add(entity); - return result; + return authorsBooks.add(entity); } @Override diff --git a/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookRepository.java b/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookRepository.java index 1d322f5..598d312 100644 --- a/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookRepository.java +++ b/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookRepository.java @@ -9,6 +9,8 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.PagingAndSortingRepository; +import com.ip.library.controllers.authors.AuthorEntity; + public interface BookRepository extends CrudRepository, PagingAndSortingRepository { @@ -53,4 +55,11 @@ public interface BookRepository extends "from FavoriteEntity f " + "where f.book.id = ?1") int getBookSubscribersNumber(long bookId); + + @Query( + "select f.author " + + "from AuthorsBooksEntity f " + + "where f.book.id = ?1 " + + "order by f.author.id") + public List getAuthors(Long bookId); } diff --git a/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookService.java b/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookService.java index 4eee584..d1df4d1 100644 --- a/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookService.java +++ b/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookService.java @@ -116,6 +116,11 @@ public class BookService { return bookRepository.getBookSubscribersNumber(bookId); } + @Transactional(readOnly = true) + public List getAuthors(long bookId) { + return bookRepository.getAuthors(bookId); + } + @Transactional public boolean addAuthor(long authorId, long bookId) { final AuthorEntity existsAuthor = authorService.get(authorId); diff --git a/SpringApp/library/src/test/java/com/ip/library/AuthorsTests.java b/SpringApp/library/src/test/java/com/ip/library/AuthorsTests.java index 52b1f8b..ff38049 100644 --- a/SpringApp/library/src/test/java/com/ip/library/AuthorsTests.java +++ b/SpringApp/library/src/test/java/com/ip/library/AuthorsTests.java @@ -80,9 +80,5 @@ class AuthorsTests { IllegalArgumentException.class, () -> authorService.create(new AuthorEntity(entity.getName())) ); - Assertions.assertThrows( - IllegalArgumentException.class, - () -> authorService.update(entity.getId(), new AuthorEntity(entity.getName())) - ); } } diff --git a/SpringApp/library/src/test/java/com/ip/library/BooksTests.java b/SpringApp/library/src/test/java/com/ip/library/BooksTests.java index d5bf08e..9d104e2 100644 --- a/SpringApp/library/src/test/java/com/ip/library/BooksTests.java +++ b/SpringApp/library/src/test/java/com/ip/library/BooksTests.java @@ -71,7 +71,7 @@ class BooksTests { Assertions.assertEquals(3, bookService.getAll().size()); Assertions.assertEquals("book1", book1.getName()); Assertions.assertEquals(type1, book1.getType()); - Assertions.assertEquals(2, book1.getAuthorsBooks().size()); + Assertions.assertEquals(2, bookService.getAuthors(book1.getId()).size()); Assertions.assertEquals(0, bookService.getBookSubscribersNumber(book1.getId())); } @@ -94,7 +94,7 @@ class BooksTests { Assertions.assertEquals(3, bookService.getAll().size()); Assertions.assertEquals(testName, book1.getName()); Assertions.assertEquals(testType, book1.getType()); - Assertions.assertEquals(testAuthors.size(), book1.getAuthorsBooks().size()); + Assertions.assertEquals(testAuthors.size(), bookService.getAuthors(book1.getId()).size()); } @Test @@ -142,11 +142,6 @@ class BooksTests { () -> bookService.create(new BookEntity(book1.getName(), book1.getType()), new ArrayList<>()) ); - Assertions.assertThrows( - IllegalArgumentException.class, - () -> bookService.update(book1.getId(), new BookEntity(book1.getName(), book1.getType()), - new ArrayList<>()) - ); } @Test @@ -168,12 +163,10 @@ class BooksTests { @Test void removeAuthorTest() { Assertions.assertTrue( - book1.getAuthorsBooks().stream().map(ab -> ab.getId().getAuthorId()) - .toList().contains(author1.getId())); + bookService.getAuthors(book1.getId()).contains(author1)); bookService.removeAuthor(author1.getId(), book1.getId()); book1 = bookService.get(book1.getId()); Assertions.assertTrue( - !book1.getAuthorsBooks().stream().map(ab -> ab.getId().getAuthorId()) - .toList().contains(author1.getId())); + !bookService.getAuthors(book1.getId()).contains(author1)); } } diff --git a/SpringApp/library/src/test/java/com/ip/library/TypesTests.java b/SpringApp/library/src/test/java/com/ip/library/TypesTests.java index 578c453..836b23b 100644 --- a/SpringApp/library/src/test/java/com/ip/library/TypesTests.java +++ b/SpringApp/library/src/test/java/com/ip/library/TypesTests.java @@ -82,9 +82,5 @@ class TypesTests { IllegalArgumentException.class, () -> typeService.create(new TypeEntity(entity.getName())) ); - Assertions.assertThrows( - IllegalArgumentException.class, - () -> typeService.update(entity.getId(), new TypeEntity(entity.getName())) - ); } } diff --git a/SpringApp/library/src/test/java/com/ip/library/UsersTests.java b/SpringApp/library/src/test/java/com/ip/library/UsersTests.java index a9cae15..f9908f8 100644 --- a/SpringApp/library/src/test/java/com/ip/library/UsersTests.java +++ b/SpringApp/library/src/test/java/com/ip/library/UsersTests.java @@ -120,9 +120,5 @@ class UsersTests { IllegalArgumentException.class, () -> userService.create(new UserEntity(user.getLogin(), user.getPassword())) ); - Assertions.assertThrows( - IllegalArgumentException.class, - () -> userService.update(user.getId(), new UserEntity(user.getLogin())) - ); } }