with子句在hSQL中引用了两个不同的from子句元素

时间:2015-01-15 06:26:46

标签: java hibernate

获得例外:

  

With子句引用了两个不同的from子句元素

使用以下实体执行以下查询时:

@Entity
public class A {

    @Id
    private Long id;

    @Column(name = "A_ID")
    private Long aId;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name = "A_ID")
    private B b;
}

@Entity
public class B {

    @Id
    private Long id;

    @OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
    @JoinColumn(name="C_ID" , referencedColumnName="ID")
    private List<C> c;
}

@Entity
public class C {

    @Id
    private Long id;

    @Column(name="C_ID")
    private Long cId;

    @Column
    private String addType;
}

我的HQL查询是:

SELECT * FROM A a 
INNER JOIN a.b b ON a.aId=b.id 
LEFT OUTER JOIN b.c c ON b.id=c.cId AND c.addType='PermanentAddress' 
LEFT OUTER JOIN b.c d ON b.id=d.cId AND d.addType='ResidentialAddress'
LEFT OUTER JOIN b.c e ON b.id=e.cId AND e.addType='OfficeAddress'
WHERE a.id  =:id

我得到的例外是:

  

引起:org.hibernate.hql.internal.ast.QuerySyntaxException:with-clause引用了两个不同的from-clause元素         在org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:91)   〜[休眠核-4.3.6.Final.jar:4.3.6.Final]         在org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:109)   〜[休眠核-4.3.6.Final.jar:4.3.6.Final]         在org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:284)   〜[休眠核-4.3.6.Final.jar:4.3.6.Final]         在org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206)   〜[休眠核-4.3.6.Final.jar:4.3.6.Final]         在org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)   〜[休眠核-4.3.6.Final.jar:4.3.6.Final]         在org.hibernate.engine.query.spi.HQLQueryPlan。(HQLQueryPlan.java:126)   〜[休眠核-4.3.6.Final.jar:4.3.6.Final]         在org.hibernate.engine.query.spi.HQLQueryPlan。(HQLQueryPlan.java:88)   〜[休眠核-4.3.6.Final.jar:4.3.6.Final]         在org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:167)   〜[休眠核-4.3.6.Final.jar:4.3.6.Final]         at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)   〜[休眠核-4.3.6.Final.jar:4.3.6.Final]         在org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)   〜[休眠核-4.3.6.Final.jar:4.3.6.Final]         在org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1800)   〜[休眠核-4.3.6.Final.jar:4.3.6.Final]         在org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:328)   〜[休眠-的EntityManager-4.3.6.Final.jar:4.3.6.Final]         ...省略了187个常见帧

2 个答案:

答案 0 :(得分:13)

您的查询中不需要ON子句。 Hibernate会自动链接模型。

答案 1 :(得分:0)

您不需要查询中的ON子句和条件,因为您只使用FK,而FK还是隐式的。

尝试一下:

ActiveJob