HQL Left Join - org.hibernate.hql.internal.ast.ErrorCounter - 连接所需的路径

时间:2016-06-14 10:09:55

标签: java hibernate jpa hql jpql

执行以下代码时,我遇到了这个异常

  

错误org.hibernate.hql.internal.ast.ErrorCounter - 期望的路径   加入!

DB视图的实体类:

@NamedQueries({
@NamedQuery(name = "find", 
            query = "SELECT v.cxxx, trk.cxxx, trk.ixxx"
        + "FROM AxxxCxxx as v LEFT JOIN AxxxCxxxTxxx as trk "
        + "ON v.cxxx = trk.cxxx "
        )
})

@Entity
@Table(name = "V_XXX")
public class AxxxCxxx implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@Column(name = "CXXX")
private String cxxx;
//getter, setter 
}

DB表的Entity1类:

@Entity
@Table(name = "T_XXX")
public class AxxxCxxxTxxx extends Super implements
    Serializable {

private static final long serialVersionUID = 1L;

@Column(name = "CXXX")
private String cxxx;
//getter, setter
}

超级课程:

@MappedSuperclass
public abstract class Super implements Serializable {
private static final long serialVersionUID = -1413197336548058176L;

@Id
@Column(name = "TXXX", nullable = false)
private Integer txxx;

@Column(name = "IXXX", length = 15)
private String ixxx;
//getter, setter

}

当我尝试执行NamedQuery时,我遇到了这个例外:

Caused by: org.hibernate.HibernateException: Errors in named queries: find
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:504)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:802)
... 26 more

org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join! [SELECT v.cxxx, trk.cxxx, trk.ixxx"
    + "FROM AxxxCxxx as v LEFT JOIN AxxxCxxxTxxx as trk "
    + "ON v.cxxx = trk.cxxx]
org.hibernate.hql.internal.ast.InvalidPathException: Invalid path: 'trk.ixxx'
at org.hibernate.hql.internal.ast.util.LiteralProcessor.lookupConstant(LiteralProcessor.java:114) ~[hibernate-core-5.0.5.Final.jar:5.0.5.Final]

拜托,有人可以帮帮我吗?如何使用JPQL为MappedSuperClass解决使用视图和表的左连接?

2 个答案:

答案 0 :(得分:0)

您必须使用@NamedNativeQuery进行SQL查询。使用@NamedQuery,查询表达式为HQL。

考虑到上面的映射,你无法真正做到HQL加入,你可以只使用笛卡尔积

@NamedQuery(name = "find", 
        query = "select v.cxxx, trk.cxxx, trk.ixxx "
    + "from AxxxCxxx as v, AxxxCxxxTxxx as trk "
    + "where v.cxxx = trk.cxxx "
    )

})

答案 1 :(得分:0)

如果要在JPQL查询中使用JOIN,则需要映射实体之间的关系。

这样的东西
  FROM AxxxCxxx as v INNER JOIN v.axxxCxxxTxxx as trk.

其中axxxCxxxTxxx是在AxxxCxxx类中声明的字段,并根据数据库结构正确映射。

可以获取其他信息herehere