执行以下代码时,我遇到了这个异常
错误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解决使用视图和表的左连接?
答案 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)