【发布时间】:2018-05-14 07:38:45
【问题描述】:
我有两个表/实体。一切正常(getter 和 setter 省略)。
@Entity
public class Book() {
@Id;
int id;
@ManyToMany
List<Category> categories;
}
@Entity
public class Category {
@Id
int id;
@ManyToMany
List<Book> books;
}
JPA/Hibernate 使用名为 book_category 的连接表创建数据库结构,其中包含字段 book_id 和 category_id。
类别是预先创建的,因此并非所有类别都有书籍。 我想查询其中包含书籍的不同类别。
下面的 SQL 查询可以做到这一点:
SELECT DISTINCT <things> FROM book_category JOIN category ON category.id = book_category.category_id
但是如何从存储库中获取类别列表?
【问题讨论】:
标签: java mysql jpa spring-boot spring-data-jpa