【问题标题】:Multiple JQPL fetch joins fail with HibernateHibernate 的多个 JPQL 提取连接失败
【发布时间】:2011-11-24 14:23:27
【问题描述】:

使用 Hibernate 3.6.7 和 JPA 2,我不能在一个查询中有两个 fetch 连接。实体有一个称为父的自引用字段。本地化文本是一个@ElementCollection,属于 Java 类型的 Map。 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 

休眠抱怨: 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]

【问题讨论】:

    标签: java hibernate join fetch jpql


    【解决方案1】:

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

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

    【讨论】:

    • 没有区别。此查询引发相同的异常: select e from Entity e join e.parent p join fetch e.localizedTexts join fetch p.localizedTexts
    • 如果你有一个从 e 到 parent 的 OneToMany 或 oneTOOne 关联,以及从 parent 到 localizedTexts 的 onetoMany,那么上面的连接应该可以工作。请发布这些 bean 的代码片段以及它们之间的关联
    • 之前提到localizedTexts是MapKeyColumn,getParent()上有ManyToOne
    猜你喜欢
    • 2015-07-15
    • 2015-02-16
    • 2019-03-18
    • 2018-11-01
    • 2020-05-10
    • 2018-07-06
    • 2021-09-05
    • 2013-12-21
    • 2012-09-12
    相关资源
    最近更新 更多