我在 UpdatesEntity
和 EntriesEntity
之间存在一对多关系(Update
有许多 Entries
)。关系如下:
@Entity
@Table(name = "entries", schema = "", catalog = "whatever")
public class EntriesEntity {
....
private UpdatesEntity update;
....
@ManyToOne
@JoinColumn(name = "update_id")
public UpdatesEntity getUpdate() {
return update;
}
public void setUpdate(UpdatesEntity update) {
this.update = update;
}
我正在尝试运行以下查询
FROM UpdatesEntity u, EntriesEntity e WHERE e.update = u
但我不断得到:
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: e near line 1, column 99 [FROM com.whatever.model.beans.UpdatesEntity u, com.whatever.model.beans.EntriesEntity e WHERE e.update = u]
但是,例如,如果在查询中我将 e.update
替换为 e.description
,则它可以工作。唯一的问题似乎是在尝试访问 EntriesEntity
的 update
字段时。
我现在非常困惑,因为我已经尝试过用我认为是这种关系的精确副本进行的操作,但是这种方法有效,而这种方法不行。
我已经调试了几个小时但无济于事。任何想法都非常受欢迎!
最佳答案
如果您为 UpdatesEntity
和 EntriesEntity
正确定义了关系,则可以使用“join”,如下所示:
内连接:
FROM UpdatesEntity u inner join u.entriesEntity e
或左外连接:
FROM UpdatesEntity u left outer join u.entriesEntity e
或者,加载 UpdatesEntity 的对象后直接从 UpdatesEntity 获取entriesEntity,如下所示:
UpdatesEntity myObj = session.get(UpdatesEntity.class, id);
Set<EntriesEntity> ent = myObj.getEntriesEntity()//assume you used "Set"
在上面的连接中,您可以在单个属性中添加“where”条件,例如“where e.aaa=xxx”。我认为您不能直接在“where”中使用实体对象,例如您的“WHERE e.update = u”。
关于java - Hibernate HQL 不断抛出 'QuerySyntaxException: unexpected token',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29134004/