我遇到HQL联接查询问题。任何人都可以告诉我,我的下面加入HQL查询有什么问题吗? 我使用的是Hibernate 4.3.6,JDK 7和Groovy 2.2
def query = 'select lip.referenceId from Parcel as lip left join TransportFile tf where lip.statusDisplayName != tf.statusDisplayName'
def hqlQuery = session.createQuery(query)
def hqlcount = hqlQuery.list().size
当我在代码
上面运行时出现以下错误com.dc.core.common.exception.BaseApplicationException: org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join! [select lip.referenceId from com.dc.apps.cp.ebilling.model.impl.Parcel as lip left join TransportFile tf where lip.statusDisplayName != tf.statusDisplayName]
以下是我的宗地实体
package com.dc.apps.cp.ebilling.model.impl
@Entity
@Audited
public class Parcel implements IPersistentEntityInstance {
private static final long serialVersionUID = 1L;
private Long id;
@AttributeReadPermission(name = "SUBMITTEDFILE.READ")
@AttributeWritePermission(name = "SUBMITTEDFILE.UPDATE")
private File submittedFile;
private ParcelType type;
private boolean isBeingSubmitted;
private TransportFile transportFile;
}
以下是我的TransportFile实体
package com.dc.apps.cp.legacy.model.impl;
@Entity
@EntityMetadataDefaults(editable = false)
public class TransportFile implements ITransportObject {
private static final long serialVersionUID = 1L;
private Long id;
private String statusDisplayName;
// [ReferenceID] [varchar](30) NOT NULL
private String referenceId;
// [sent] [datetime] NULL,
private Timestamp sentDate;
// [received] [datetime] NULL,
private Timestamp receivedDate;
// [Status] [varchar](4) NULL,
private String statusCode;
private List<TransportLog> TransportLogs;
private String rejectionReason;
}
我参考了这篇文章HQL left join: Path expected for join,但我没有看到任何关于我的HQL联接查询的内容。
答案 0 :(得分:8)
这个例外&#34;期望加入&#34; 的路径是:
提供从一个实体到另一个实体的路径。连接由映射
定义
所以我们需要:
select lip.referenceId
from Parcel as lip
// instead of this
// left join TransportFile tf
// we need a path from Parcel to TransportFile
left join lip.transportFile tf
where lip.statusDisplayName != tf.statusDisplayName'
...
语句left join TransportFile tf
更像是一个SQL ......而不是HQL的情况。
我们的陈述必须表达导航:left join lip.transportFile tf
- 即加入与transportFile
相关的lip
。