FetchMode join 对 Spring JPA 存储库中的 ManyToMany 关系没有影响

2022-01-18 00:00:00 spring java Hibernate jpa spring-data

我正在尝试这样做:

//...
class Person {
    @ManyToMany(fetch = FetchType.EAGER)
    @Fetch(FetchMode.JOIN)
    private Set<Group> groups;
//...
}

当我通过 Spring JPA 存储库执行 personRepository.findAll(); 时,它会生成 n+1 个查询,就好像我没有设置任何 @Fetch.(一个查询首先获取所有人员,然后每个人一个查询获取组).

it generates n+1 queries when I do personRepository.findAll(); through a Spring JPA repository, just as if I didn't have any @Fetch set. (One query first to get all the persons, and then one query per person to fetch the groups).

虽然使用 @Fetch(FetchMode.SUBSELECT) 有效!它只生成 2 个查询.(所有人一份,然后团体一份).所以hibernate对一些获取参数做出反应,而不是JOIN.

Using @Fetch(FetchMode.SUBSELECT) works, though! It only generates 2 queries. (One for all persons, and then one for the groups). So hibernate reacts to some fetch parameters, just not the JOIN.

我也尝试过删除 EAGER 获取但没有成功.

I have also tried removing the EAGER fetching with no luck.

//...
class Person {
    @ManyToMany()
    @Fetch(FetchMode.JOIN)
    private Set<Group> groups;
//...
}

我正在使用 Spring JPA,这是我的存储库的代码:

I am using Spring JPA, and this is the code for my repository:

public interface PersonRepository extends JpaRepository<Person, Long> {
}

JOIN 只是不能通过 Spring JPA 工作,还是我做错了什么?

Does JOIN just not work through Spring JPA, or am I doing something wrong?

推荐答案

浏览许多论坛和博客来解决您的问题(我想您可能在发布之前已经这样做了)我也认为

Going through many forums and blogs to read for your problem (I guess you might have done that before posting it here) I too think that

@Fetch(FetchMode.JOIN) 如果使用 Query 接口将被忽略(例如:session.createQuery()),但如果你使用它将被正确使用标准界面.

@Fetch(FetchMode.JOIN) will be ignored if you use the Query interface (e.g.: session.createQuery()) but it will be properly used if you use the Criteria interface.

这实际上是 Hibernate 中从未解决的错误.它是不幸的是,因为很多应用程序使用查询接口和无法轻松迁移到 Criteria 界面.

This is practically a bug in Hibernate which was never resolved. It is unfortunate because a lot of applications use the Query interface and cannot be migrated easily to the Criteria interface.

如果您使用查询接口,您总是必须添加 JOIN FETCH手动将语句写入 HQL.

If you use the Query interface you always have to add JOIN FETCH statements into the HQL manually.

参考资料休眠论坛春季论坛类似问题1

相关文章