Hibernate Criteria API - 添加一个标准:字符串应该在集合中
我必须遵循实体对象
@Entity
public class Foobar {
...
private List<String> uuids;
...
}
现在我想做一个条件查询,它将获取其 uuids 列表包含字符串“abc123”的所有 Foobar pojo,我只是不确定如何制定适当的条件。
I have to following entity object
@Entity
public class Foobar {
...
private List<String> uuids;
...
}
Now I'd like to make a criteria query which would fetch all Foobar pojos whose uuids list contains the string "abc123", I'm just not sure how to make the appropriate criterion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我假设您使用的是实现 JPA 2.0 的 Hibernate 版本。这是一个 JPA 2.0 解决方案,应该适用于任何合规的实现。
请使用 JPA 的
@ElementCollection
注释来注释uuids
。不要使用其他一些答案评论中提到的 Hibernate 的@CollectionOfElements
。后者具有相同的功能,但已弃用。Foobar.java
大致如下所示:以下是如何构建
CriteriaQuery
来选择其uuids
的所有Foobar
> 包含“abc123”。我在玩这个的时候制作了一个独立的概念验证程序。我现在不能把它推出去。如果您希望将 POC 推送到 github,请评论。
I assume you are using a version of Hibernate that implements JPA 2.0. Here's a JPA 2.0 solution that should work with any compliant implementation.
Please annotate
uuids
with JPA's@ElementCollection
annotation. Don't use Hibernate's@CollectionOfElements
as mentioned in some of the other answer comments. The latter has equivalent functionality but is being deprecated.Foobar.java
will look approximately like this:Here's how you can build a
CriteriaQuery
to select allFoobar
s whoseuuids
contain "abc123".I made a self-contained proof-of-concept program while playing with this. I can't push it out right now. Please comment if you want the POC pushed to github.
我知道这是老问题,但我刚刚遇到这个问题并找到了解决方案。
如果您想使用 Hibernate Criteria,您可以加入
uuids
集合并使用其属性elements
来匹配元素。就像这样:I know this is old question, but I have just encountered this issue and found solution.
If you want to use Hibernate Criteria you can join your
uuids
collection and use its propertyelements
to match elements. Just like that:您可以像下面的示例一样使用查询,也可以将其转换为命名查询。不幸的是,似乎没有办法用 Criteria 来做到这一点。
You could use a Query as in the example below or you could convert this to a NamedQuery. Unfortunately there doesn't seem to be a way to do this with Criteria.
我发现一年前的这篇文章,并且我做了这个方法,如果它可以帮助任何人解决我几个小时前遇到的同样问题。
用一组弦也做了同样的事情。
它适用于“or”语句,但可以轻松地用“and”语句替换。
I've found this post from one year ago, and I've made this method, if it can help anybody with the same problem I had a few hours ago.
Made the same with a group of strings too.
It works with "or" statements, but can easily be replaced by "and" statements.
休眠不支持您所问的问题。请参阅http://opensource.atlassian.com/projects/hibernate/browse/HHH -869
这是 jira 票证中提供的解决方法:
What you are asking is not supported out of the box by hibernate. See http://opensource.atlassian.com/projects/hibernate/browse/HHH-869
Here is a workaround available in the jira ticket :
jira 的 sqlRestriction 的解决方案
http://opensource.atlassian.com/projects/hibernate/browse/HHH-869
这对我来说似乎是最好的方法,因为我大量使用标准 API。我必须编辑蒂埃里的代码,以便它在我的案例
模型中起作用:
标准调用:
The solution with the sqlRestriction from jira
http://opensource.atlassian.com/projects/hibernate/browse/HHH-869
seemed the best way to go for me since i heavily use criteria api. I had to edit Thierry's code so it worked in my case
Model:
Criteria call:
首先,我不认为 Hibernate 可以映射
List
。但是,它可以映射其他实体的列表。因此,如果您的代码如下所示:
并且
EntityObject
有一个名为str
的String
属性,则条件可能如下所示:For starters, I don't think Hibernate can map a
List<String>
. However, it can map a list of other entities.So if your code was something like this:
And the
EntityObject
has aString
-property calledstr
, the criteria could look like this: