开发者

Multiple JQPL fetch joins fail with Hibernate

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开发者_C百科 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]


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜