FetchMode join makes no difference for ManyToMany relations in spring JPA repositories(FetchMode join 对 Spring JPA 存储库中的 ManyToMany 关系没有影响)
问题描述
我正在尝试这样做:
//...
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
这篇关于FetchMode join 对 Spring JPA 存储库中的 ManyToMany 关系没有影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:FetchMode join 对 Spring JPA 存储库中的 ManyToMany 关系没有影响


- 如何使用WebFilter实现授权头检查 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01