Collection.contains(Enum.Value) in HQL?

Posted by Seth on Stack Overflow See other posts from Stack Overflow or by Seth
Published on 2010-03-08T17:11:32Z Indexed on 2010/03/08 17:51 UTC
Read the original article Hit count: 544

Filed under:
|
|
|

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!

© Stack Overflow or respective owner

Related posts about java

Related posts about hibernate