使用Hibernate时,多个JPQL提取连接失败

时间:2011-09-25 11:10:10

标签: java hibernate join fetch jpql

使用Hibernate 3.6.7和JPA 2,我在一个查询中不能有两个fetch连接。实体有一个名为parent的自引用字段。 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抱怨: 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]

1 个答案:

答案 0 :(得分:0)

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

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