我有 2 个引用的实体 Author 和 Book,并且在它之间有引用 Many2one=>one2many,我在实现方面有问题吗?当进入时,/authors我得到了所有作者的书,但每本书中都有他的作者(以及所有自己的书),反之亦然一点问题,我需要得到 /authors- 所有没有他的书的作者/author/{id}和他的所有书(里面的书不需要作者) /books所有书里有作者的书(但里面的书不需要作者) /book/{id}有作者的书(里面没有他的书)@Entity(name = "Author")@Table(name = "authors")@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})public class Author { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; @Column(name = "title") private String title; @Column(name = "description") private String description; @OneToMany(fetch = FetchType.LAZY, mappedBy = "author", cascade = CascadeType.ALL) private List<Book> books; ....和@Entity@Table(name = "books")@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})public class Book{ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; @Column(name = "price") private double price; @Column(name = "title") private String title; @Column(name = "description") private String description; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "author_id") private Author author; ....
1 回答
守着一只汪
TA贡献1872条经验 获得超3个赞
我会从作者中删除private List<Book> books;并创建一个服务:
public class BookService {
public List<Book> getBooks(int authorId) {
// look up the books by author_id
}
}
就像人类作者不会一直随身携带所有出版的书籍一样,Java 作家也不应该随身携带。
添加回答
举报
0/150
提交
取消