Я пытаюсь вернуть все книги из моей базы данных, но после того, как я вставил значения в таблицу соединения с именем COLLECTION (для решения Rel-многие-ко-многим между BOOK и USER), вывод, который я получаю, выглядит как цикл (проверено с почтальоном).
Реализация Book Entity:
@Getter
@Setter
@ToString
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class Book {
@Id
private Integer id;
private String title;
private String author;
private String description;
@OneToMany(mappedBy = "book", cascade = CascadeType.ALL)
private Set<Collection> collections;
public Book(String title, String author, String description) {
this.title = title;
this.author = author;
this.description = description;
}
public Book(String title, String author Collection... collections){
this.title = title;
this.author = author;
for (Collection collection : collections){
collection.setBook(this);
}
this.collections = Stream.of(collections).collect(Collectors.toSet());
}
}
Реализация User Entity:
@Getter
@Setter
@NoArgsConstructor
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String email;
private String password;
private String username;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private Set<Collection> collections = new HashSet<>();
public User(String username, String password) {
this.username = username;
this.password = password;
}
}
Объект коллекции:
@Getter
@Setter
@NoArgsConstructor
@Entity
public class Collection implements Serializable {
@Id
@ManyToOne
@JoinColumn
private Book book;
@Id
@ManyToOne
@JoinColumn
private User user;
public Collection(User user){
this.user = user;
}
@Override
public boolean equals(Object o){
if(this == o) return true;
if(!(o instanceof Collection)) return false;
Collection that = (Collection) o;
return Objects.equals(book.getTitle(), that.book.getTitle()) &&
Objects.equals(book.getAuthor(), that.book.getAuthor()) &&
Objects.equals(user.getUsername(), that.user.getUsername());
}
@Override
public int hashCode(){
return Objects.hash(book.getTitle(), book.getAuthor(), user.getUsername());
}
}
И, наконец, есть BookRepository:
@Transactional
@Repository
public interface BookRepository extends JpaRepository<Book, Integer> {
// this causes sql syntax error
// @Query(value = "SELECT b.id, b.title, b.author FROM book b", nativeQuery = true)
// List<Book> getAllBooks();
// this returns the loopy output from postman detailed below
List<Book> findAll();
}
Вывод, который я получаю в Postman, выглядит как цикл:
[{"id":1,"title":"Pride and Prejudice","author":"Jane Austen","users":[{"id":1,"email":"a@a.com","password":"$2a$10$BVXUCumzWyec9zEUeCv1r.m2pFwvAe7Cp1dLjiGfXuEEIHkhn3jHO","username":"user","books":[{"id":1,"title":"Pride and Prejudice","author":"Jane Austen", "users":[{"id":1,"email":"a@a.com","password":"$2a$10$BVXUCumzWyec9zEUeCv1r.m2pFwvAe7Cp1dLjiGfXuEEIHkhn3jHO, "username":"user","books": .......
Ожидаемым выводом должен быть список всех книг, которые у меня есть в базе данных.