Java 休眠和意外结束子树异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2066361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate and Unexpected end of Subtree exception
提问by Scott Fines
I'm a newbie to Hibernate.
我是 Hibernate 的新手。
I have an Item
POJO which contains a Set<String>
consisting of labels. The labels are contained on another Database table from the Item
table, so I do a join to populate the pojo.
我有一个Item
包含Set<String>
标签的POJO 。标签包含在表中的另一个数据库表Item
中,因此我执行连接以填充 pojo。
I'm trying to run a simple example query from the book "Java Persistance with Hibernate" where I query from Item item where 'hello' member of item.labels
. Only, for some reason I am getting a
我正在尝试从“Java Persistance with Hibernate”一书中运行一个简单的示例查询,我在其中查询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]`
What might be causing this issue?
什么可能导致这个问题?
Here are my POJOs:
这是我的 POJO:
public class Item
private int uuid;
private Set<String>labels = new HashSet<String>();
@Id
public int getUuid(){
return uuid;
}
@CollectionOfElements
@JoinTable(name="labels", joinColumns=@JoinColumn(name="uuid"))
@Column(name="label")
public Set<String> getLabels(){
return labels;
}
}
采纳答案by Yuri.Bulkin
For primitives collections you should use HQL query like this:
对于基元集合,您应该像这样使用 HQL 查询:
from Item item join item.labels lbls where 'hello' in (lbls)
PS: 'join' is required because 'labels' is OneToMany or ManyToMany variant, parentheses are required because 'lbls' is a collection
PS:'join' 是必需的,因为 'labels' 是 OneToMany 或 ManyToMany 变体,括号是必需的,因为 'lbls' 是一个集合
回答by PanCrit
From googling around, it appears that your parameter collection may be empty. I'd add an empty check before calling the query.
通过谷歌搜索,您的参数集合似乎是空的。在调用查询之前,我会添加一个空支票。
The lesson is that Google is your friend. When you can't figure out an error message, try typing it into Google (or your favorite engine.) You are unlikely to be the first person to have been confused by it.
教训是谷歌是你的朋友。当您无法找出错误消息时,请尝试将其输入到 Google(或您最喜欢的引擎)中。您不太可能是第一个被它弄糊涂的人。
回答by jtbradle
The member of command in the HQL is reserved for the use of non-primitive objects. There are two things you can do. You can either create a SQLQuery
as follows:
HQL 中的 command 成员保留供非原始对象使用。你可以做两件事。您可以创建一个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();
Or you can create a class called Label
and do the following in your HQL:
或者您可以创建一个名为的类Label
并在您的 HQL 中执行以下操作:
from Item item, Label label
where label member of item.labels
and label.label = 'hello'
Hope this helps :)
希望这可以帮助 :)
回答by Tom Anderson
Based on the comments on the bug HHH-5209, which is about the same exception being thrown from a similar JPQL query, i believe the correct form here is:
基于对错误HHH-5209的评论,它与类似的 JPQL 查询抛出的异常大致相同,我相信这里的正确形式是:
select item from Item item where 'hello' in elements(item.labels)
The elements
function there is the key. This is perhaps slightly simpler than Yuri's suggestion, because it avoids the explicit join.
elements
那里的功能是关键。这可能比 Yuri 的建议稍微简单一些,因为它避免了显式连接。