jpa获取连接查询

时间:2011-11-19 16:02:36

标签: java jpa jpa-2.0 jpql

这就是我的域名:

public class Template implements Serializable {
    private static final long serialVersionUID = 1L;    
    @OneToOne(cascade=CascadeType.ALL)
    private FieldConfig fieldConfig;
}

public class FieldConfig implements Serializable {
    private static final long serialVersionUID = 1L;

    @OneToMany(cascade= CascadeType.PERSIST)
    @JoinColumn(name = "fieldConfigId")
    private Set<Field> fieldSet;
}

如果我从自动加载fieldConfig的db和该fieldconfig的fieldSet加载模板,我想实现。

我目前的JPQL:

TypedQuery<Template> query = em.createQuery("SELECT t from Template t LEFT JOIN FETCH t.fieldConfig"
                + " fconfig LEFT JOIN FETCH fconfig.fieldSet where t.id = :id", Template.class);

我的例外:

Internal Exception: NoViableAltException(80@[()* loopback of 477:9: (node= join )*])
Exception Description: Syntax error parsing the query [SELECT t from Template t LEFT JOIN FETCH t.fieldConfig fconfig LEFT JOIN FETCH fconfig.fieldSet where t.id = :id], line 1, column 55: unexpected token [fconfig].

有关创建此类查询的任何想法吗?

1 个答案:

答案 0 :(得分:3)

您无法在JPQL中使用联接提取别名,但规范不允许这样做。

EclipseLink允许通过查询提示

进行嵌套连接提取
"eclipselink.join-fetch"="t.fieldConfig.fieldSet"