Hibernate和意外的Subtreeexception结束
我是Hibernate的新手。
我有一个Item
POJO,其中包含一个由标签组成的Set
。 标签包含在Item
表中的另一个Database表中,因此我进行了连接以填充pojo。
我正在尝试从“Java Persistance with Hibernate”一书中运行一个简单的示例查询,其中我from Item item where 'hello' member of item.labels
查询from Item item where 'hello' member of item.labels
。 只是,由于某种原因,我得到了一个
`org.hibernate.hql.ast.QuerySyntaxException: unexpected end of subtree[from /*qualified class path*/.Item item where 'hello' member of item.labels]`
可能导致此问题的原因是什么?
这是我的POJO:
public class Item private int uuid; private Setlabels = new HashSet(); @Id public int getUuid(){ return uuid; } @CollectionOfElements @JoinTable(name="labels", joinColumns=@JoinColumn(name="uuid")) @Column(name="label") public Set getLabels(){ return labels; } }
对于基元集合,您应该使用如下的HQL查询:
from Item item join item.labels lbls where 'hello' in (lbls)
PS:’join’是必需的,因为’标签’是OneToMany或ManyToMany变体,括号是必需的,因为’lbls’是一个集合
从谷歌搜索,您的参数集合似乎是空的。 我在调用查询之前添加一个空的检查。
教训是谷歌是你的朋友。 如果您无法找出错误消息,请尝试将其键入Google(或您最喜欢的引擎)。您不太可能成为第一个被它迷惑的人。
HQL中的命令成员保留用于非基本对象。 你可以做两件事。 您可以按如下方式创建SQLQuery
:
SQLQuery sQuery = session.createSQLQuery("select * from item_table it inner join label_table lt where it.id = lt.item_id and lt.label = 'hello'"); sQuery.list();
或者,您可以创建一个名为Label
的类,并在HQL中执行以下操作:
from Item item, Label label where label member of item.labels and label.label = 'hello'
希望这可以帮助 :)
基于对HHH-5209错误的评论,这是从类似的JPQL查询抛出的相同exception,我相信这里的正确forms是:
select item from Item item where 'hello' in elements(item.labels)
elements
function就是关键。 这可能比Yuri的建议稍微简单一些,因为它避免了显式连接。