0

Let's say Student is the parent class in an inheritance hierarchy that includes ComputerScienceStudent and ITStudent. Student defines a field called favoriteColors, which is a value-typed collection of Strings. Using the criteria API I'd like to query for all students who list "blue" as one of their favoriteColors.

Here is the relevant java snippet

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
abstract class Student {

    @CollectionOfElements
    Set<String> favoriteColors = new TreeSet<String>();

I looked at the hibernate documentation found at http://docs.jboss.org/hibernate/core/3.3/reference/en/html/querycriteria.html and it looks like something like this might be a start

List students = sess.createCriteria(Student.class)
.createCriteria("favoriteColors")
    .add( Restrictions.eq(??????, "blue") )
.list();

but I don't know what to fill in as the name of the property since String is not a class I defined (thus the ??????)

I'd really like to avoid using HQL unless there is absolutely no way to do this using criteria. I'd also like to avoid adding sqlRestrictions. I don't think the example API will work because Student is the abstract parent class in an inheritance hierarchy and I can't create a concrete Student to pass in as an example.

Is this possible? Thanks for your time!

Jason Novak
  • 1,081
  • 3
  • 13
  • 26

1 Answers1

0

Have you tried writing

List students = sess.createCriteria(Student.class)
.add(Restrictions.eq("favoriteColors", "blue") )
.list();

Edit: Ok, that doesn't work. According to this related question, refering to elements of value-type collections is currently impossible using the Criteria API.

Community
  • 1
  • 1
meriton
  • 68,356
  • 14
  • 108
  • 175