tags:

views:

81

answers:

1

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!

A: 

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.

meriton
Just did, it fails with the message ERROR org.hibernate.util.JDBCExceptionReporter - Parameter #1 is not set
Jason Novak
I see. Edited to include that - and I found a related question that hopefully helps.
meriton
That's bad news. But thanks. At least now I know not to keep pursuing this path until hibernate gets this functionality.
Jason Novak