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

093gszye  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(330)

获取异常:
with子句引用了两个不同于子句的元素
使用以下实体执行以下查询时:

@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.querysyntaxception:with子句引用了org.hibernate.hql.internal.ast.querysyntaxception.convert(querysyntaxception)中的两个不同于子句的元素。java:91)~[hibernate-core-4.3.6.最终版本。jar:4.3.6.final]在org.hibernate.hql.internal.ast.errorcounter.throwqueryexception(错误计数器。java:109)~[hibernate-core-4.3.6.最终版本。jar:4.3.6.final]在org.hibernate.hql.internal.ast.querytranslatorimpl.analyze(querytranslatorimpl。java:284)~[hibernate-core-4.3.6.最终版本。jar:4.3.6.final]在org.hibernate.hql.internal.ast.querytranslatorimpl.docomFile(querytranslatorimpl。java:206)~[hibernate-core-4.3.6.最终版本。jar:4.3.6.final]在org.hibernate.hql.internal.ast.querytranslatorimpl.compile(querytranslatorimpl。java:158)~[hibernate-core-4.3.6.最终版本。jar:4.3.6.final]在org.hibernate.engine.query.spi.hqlqueryplan。java:126) ~[hibernate-core-4.3.6.最终版本。jar:4.3.6.final]在org.hibernate.engine.query.spi.hqlqueryplan。java:88)~[hibernate-core-4.3.6.最终版本。jar:4.3.6.final]在org.hibernate.engine.query.spi.queryplanche.gethqlqueryplan(queryplanche。java:167)~[hibernate-core-4.3.6.最终版本。jar:4.3.6.final]在org.hibernate.internal.abstractsessionimpl.gethqlqueryplan(抽象会话Impl。java:301)~[hibernate-core-4.3.6.最终版本。jar:4.3.6.final]在org.hibernate.internal.abstractsessionimpl.createquery(abstractsessionimpl。java:236)~[hibernate-core-4.3.6.最终版本。jar:4.3.6.final]在org.hibernate.internal.sessionimpl.createquery(sessionimpl。java:1800) ~[hibernate-core-4.3.6.最终版本。jar:4.3.6.final]在org.hibernate.jpa.spi.abstractentitymanagerimpl.createquery(abstractentitymanagerimpl。java:328)~[hibernate-entitymanager-4.3.6.最终版本。jar:4.3.6.final] ... 省略187个公共框架

fnatzsnv

fnatzsnv1#

查询中不需要on子句。hibernate会自动链接模型。

yrwegjxp

yrwegjxp2#

在查询中不需要on子句和条件,因为只使用fk,这是隐式的。
试试这个:

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

相关问题