Hibernate 中多个 JQPL 获取连接失败

发布于 2024-12-06 22:27:01 字数 1260 浏览 0 评论 0原文

使用 Hibernate 3.6.7 和 JPA 2,我不能在一个查询中有两个 fetch join。实体有一个称为父级的自引用字段。 localizedTexts 是 Java 类型的 Map 的 @ElementCollection。 entity.getParent() 有一个带有 EAGER 加载策略的 @ManyToOne。

这是实体的样子:

@Entity
public class Entity extends BaseEntity {
    /* ... */

    @ManyToOne(fetch = FetchType.EAGER)
    public Entity getParent() {
        return parent;
    }

    @ElementCollection
    @MapKeyColumn(name = "language")
    @Column(name = "text")
    public Map<String, String> getLocalizedTexts() {
        return localizedTexts;
    }

    /* ... */
}

以下两个查询有效:

select e from Entity e join fetch e.parent.localizedTexts

select e from Entity e join fetch e.localizedTexts

但这不起作用:

select e from Entity e join fetch e.localizedTexts join fetch e.parent.localizedTexts 

Hibernate 抱怨: 查询指定联接获取,但所获取关联的所有者不存在于选择列表中 [FromElement{explicit,collection join,fetch join,fetch non-lazy properties,classAlias=null,role=net.company.project .Entity.localizedTexts,tableName={none},tableAlias=localizedt3_,origin=null,columns={,className=null}}] [从net.company.project.Entity e join fetch e.localizedTexts join fetch e.parent.localizedTexts]

Using Hibernate 3.6.7 and JPA 2, I cannot have two fetch joins in one query. Entity has a self referencing field called parent. localizedTexts is an @ElementCollection, of Java type of Map. entity.getParent() has a @ManyToOne with EAGER loading strategy.

Here is How entity looks like:

@Entity
public class Entity extends BaseEntity {
    /* ... */

    @ManyToOne(fetch = FetchType.EAGER)
    public Entity getParent() {
        return parent;
    }

    @ElementCollection
    @MapKeyColumn(name = "language")
    @Column(name = "text")
    public Map<String, String> getLocalizedTexts() {
        return localizedTexts;
    }

    /* ... */
}

The following two queries work:

select e from Entity e join fetch e.parent.localizedTexts

select e from Entity e join fetch e.localizedTexts

But this doesn't work:

select e from Entity e join fetch e.localizedTexts join fetch e.parent.localizedTexts 

Hibernate complains:
query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,collection join,fetch join,fetch non-lazy properties,classAlias=null,role=net.company.project.Entity.localizedTexts,tableName={none},tableAlias=localizedt3_,origin=null,columns={,className=null}}] [select e from net.company.project.Entity e join fetch e.localizedTexts join fetch e.parent.localizedTexts]

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏雨凉 2024-12-13 22:27:01

如果您想预加载实体“父级”关联以及父级的“localizedTexts”关联,您应该声明以正确顺序遍历模型树的连接:

select e from Entity e join fetch e.parent p joint fetch p.localizedTexts

If you want to preload the Entities "parent" associations as well as the parent's "localizedTexts" association you should declare joins that traverse the model tree in the correct order:

select e from Entity e join fetch e.parent p joint fetch p.localizedTexts
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文