HQL 中的 Collection.contains(Enum.Value) ?
我对如何在 HQL 中做某事有点困惑。
假设我有一个 Foo 类,我在休眠状态下坚持使用它。它包含一组枚举值,如下所示:
public class Foo
{
@CollectionOfElements
private Set<Bar> barSet = new HashSet<Bar>();
//getters and setters here ...
}
是否
public enum Bar
{
A,
B
}
有一个 HQL 语句我可以用来仅获取 barSet 包含 Bar.B 的 Foo 实例?
List foos = session.createQuery("from Foo as foo " +
"where foo.barSet.contains.Bar.B").list();
或者我是否一直在获取所有 Foo 实例并在 DAO 级别将它们过滤掉?
List foos = session.createQuery("from Foo as foo").list();
List results = new ArrayList();
for(Foo f : foos)
{
if(f.barSet.contains(Bar.B))
results.add(f);
}
谢谢!
I'm a little confused about how to do something in HQL.
So let's say I have a class Foo that I'm persisting in hibernate. It contains a set of enum values, like so:
public class Foo
{
@CollectionOfElements
private Set<Bar> barSet = new HashSet<Bar>();
//getters and setters here ...
}
and
public enum Bar
{
A,
B
}
Is there an HQL statement I can use to fetch only Foo instances who'se barSet containst Bar.B?
List foos = session.createQuery("from Foo as foo " +
"where foo.barSet.contains.Bar.B").list();
Or am I stuck fetching all Foo instances and filtering them out at the DAO level?
List foos = session.createQuery("from Foo as foo").list();
List results = new ArrayList();
for(Foo f : foos)
{
if(f.barSet.contains(Bar.B))
results.add(f);
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该按如下方式进行映射
,并且您的 HQL 看起来像
这里可以看到如何映射
问候,
You should map as follows
And your HQL looks like
Here you can see how to map
regards,
您可以执行此操作
“from Foo as foo where :selectedBar member of foo.barSet”
You can do this
"from Foo as foo where :selectedBar member of foo.barSet"
从 Cat 中选择 mother 作为 mother,Cat 作为 kit
元素中的套件(foo.kittens)
docs.jboss.org
select mother from Cat as mother, Cat as kit
where kit in elements(foo.kittens)
docs.jboss.org
我通常更喜欢将枚举集作为位集存储在数据库中。它的速度非常快,并且需要一个(!)单列。我不知道 HQL 如何处理位操作,但你可以注册自己的。
I usually prefer storing enum sets as bitsets in the database. It's blazing fast and requires one (!) single column. I don't know how HQL handles bit operations but you can register your own.