【发布时间】:2019-09-15 04:04:37
【问题描述】:
我正在尝试从我的数据库中返回所有书籍,但是一旦我将值插入名为 COLLECTION 的连接表(用于解决 BOOK 和 USER 之间的多对多关系),我收到的输出看起来就像一个循环(用邮递员测试)。
书籍实体的实现:
@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());
}
}
用户实体的实现:
@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": .......
预期的输出应该是我在数据库中所有书籍的列表。
【问题讨论】:
-
请使用
@ManyToMany注释,而不是@ManyToOne与垃圾实体。另外,不要滥用 lombok 注释,不要在实体上使用@ToString,这会造成toString()调用的无限循环。
标签: hibernate spring-boot jpa many-to-many findall