Collection.contains (Enum.Value) in HQL?

I am a little confused about how to do something in HQL.

So let's say I have a Foo class that I save in sleep mode. It contains a set of enumeration values, for example:

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 instruction that I can use to retrieve only Foo instances for which barSet contains Bar.B?

 List foos = session.createQuery("from Foo as foo " + "where foo.barSet.contains.Bar.B").list(); 

Or am I stuck all instances of Foo and filters them 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!

+6
java collections hibernate hql
source share
4 answers

You should display as follows

 @CollectionOfElements @Enumerated(EnumType.STRING) @JoinTable( name="BAR_TABLE", joinColumns=@JoinColumn (name="FOO_ID") ) public Set<Bar> getBarSet() { return this.BarSet; } 

And your HQL looks like

 select distinc Foo _foo inner join fetch _foo.barSet bar where bar = :selectedBar query.setParameter("selectedBar", Bar.A); query.list(); 

Here you can see how to match

Yours faithfully,

+5
source share

You can do it

"from foo like foo where: selectedBar member foo.barSet"

+2
source share

choose a mother from Cat as a mother, Cat as kit , where a set of elements (foo.kittens)

docs.jboss.org

+2
source share

I usually prefer storing enums as bits in a database. It flashes quickly and requires one (!) One column. I don't know how HQL handles bitwise operations, but you can register your own.

0
source share

All Articles