Hibernate / JPA EntityGraph fetchGraph 不会获取所有实体,因为它基于内连接

发布于 2025-01-11 05:24:28 字数 1923 浏览 0 评论 0原文

我在 CriteriaApi 上的 EntityGraph 上遇到问题。这就是我正在做的事情:

CriteriaBuilder builder = entityManager.getCriteriaBuilder();

EntityGraph<LabelFamily> entityGraph = entityManager.createEntityGraph(LabelFamily.class);
entityGraph.addAttributeNodes("childs");
    
CriteriaQuery<LabelFamily> criteria = builder.createQuery(LabelFamily.class);
Root<LabelFamily> fromPost = criteria.from(LabelFamily.class);
    
criteria.select(fromPost);
criteria.where(specification.toPredicate(fromPost, criteria, builder));

return entityManager.createQuery(criteria)
                    .setHint("javax.persistence.fetchgraph", entityGraph)
                    .getResultList();

我的标准中有一些规范,定义了“子项”的内部联接(这是遗留代码)。然后我想在一个请求中加载每个子实体(由 OneToMany 定义),因此我使用 EntityGraph。 我的问题是这样的:EntityGraph 使用我的规范中声明的内部联接,但它用于过滤请求的结果。

这是为了更好地理解潜在问题的 sql 请求:

select distinct labelfamil0_.*,
            childs1_.*
from label_family labelfamil0_
     inner join label_family childs1_ on labelfamil0_.id = childs1_.family_parent_id
     inner join flow flows2_ on childs1_.id = flows2_.label_family_project_id
     cross join project project3_
where X

但正如我们所看到的,关系 childs1_ 是通过其他内部联接过滤的。

我想要的结果是这样的:

select distinct labelfamil0_.*,
            childs2_.*
from label_family labelfamil0_
     inner join label_family childs2_ on labelfamil0_.id = childs2_.family_parent_id
     inner join label_family childs1_ on labelfamil0_.id = childs1_.family_parent_id
     inner join flow flows2_ on childs1_.id = flows2_.label_family_project_id
     cross join project project3_
where X

我想使用其子实体的过滤器来过滤我的“父”实体,但随后我想要生成的父实体的所有子实体。

如果我不使用实体图并且我只是急切(或懒惰)获取关系,我会得到正确的结果。 因此,如果我只是这样做:

parent.getChilds()

在不使用 EntityGraph 的情况下,我会按照我想要的方式获得所有子项,因为

我正在使用 MySQL 5.7,我不知道这是否有任何重要性。 Spring版本:2.5.8(我使用从父pom导入的Hibernate)

I have a problem with EntityGraph on CriteriaApi. Here is what I am doing:

CriteriaBuilder builder = entityManager.getCriteriaBuilder();

EntityGraph<LabelFamily> entityGraph = entityManager.createEntityGraph(LabelFamily.class);
entityGraph.addAttributeNodes("childs");
    
CriteriaQuery<LabelFamily> criteria = builder.createQuery(LabelFamily.class);
Root<LabelFamily> fromPost = criteria.from(LabelFamily.class);
    
criteria.select(fromPost);
criteria.where(specification.toPredicate(fromPost, criteria, builder));

return entityManager.createQuery(criteria)
                    .setHint("javax.persistence.fetchgraph", entityGraph)
                    .getResultList();

I have some specifications in my criteria that defines an inner join on the "childs" (It's legacy code). Then i want to load every child entity (defined by an OneToMany) in one request, therefore i use EntityGraph.
My problem is this: EntityGraph use the inner join declared in my specifications but it is used to filter the result of the request.

Here is the sql request to better understand the underlying problem:

select distinct labelfamil0_.*,
            childs1_.*
from label_family labelfamil0_
     inner join label_family childs1_ on labelfamil0_.id = childs1_.family_parent_id
     inner join flow flows2_ on childs1_.id = flows2_.label_family_project_id
     cross join project project3_
where X

But as we can see the relation childs1_ is filtered through an other inner join.

The result I want is something like this:

select distinct labelfamil0_.*,
            childs2_.*
from label_family labelfamil0_
     inner join label_family childs2_ on labelfamil0_.id = childs2_.family_parent_id
     inner join label_family childs1_ on labelfamil0_.id = childs1_.family_parent_id
     inner join flow flows2_ on childs1_.id = flows2_.label_family_project_id
     cross join project project3_
where X

I want to filter my "parent" entity using a filter on its child, but then i want all the childs of my resulting parents.

I have the correct result if I do not use an entityGraph and I simply EAGER (or LAZY) fetch the relation.
So if I simply do that:

parent.getChilds()

Without using EntityGraph, I get all childs as I wanted

I am using MySQL 5.7, i do not know if that is of any importance.
Version of Spring: 2.5.8 (I use Hibernate imported from the parent pom)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文