Digressing ever so little from the thread's subject an issue i see with the criteria API is regarding polymorphic queries for association classes. Among the criteria enthusiasts I hope someone has workarounds for this.
Here are the demo classes and their members.
Code:
Parent
|-- id
Child
|-- id
|-- name
SubChild extends Child
|-- location
the relationships are as
Code:
<class name="test.Parent">
<id name="id" unsaved-value="-1">
<generator class="native"/>
</id>
<bag name="children" lazy="false" cascade="all">
<key column="parent_id"/>
<one-to-many class="test.Child"/>
</bag>
</class>
<class name="test.Child" discriminator-value="super">
<id name="id" unsaved-value="-1">
<generator class="native"/>
</id>
<discriminator column="subclass" type="string"/>
<property name="name"/>
<subclass name="test.SubChild" discriminator-value="subchild">
<property name="location" type="string"/>
</subclass>
</class>
now the ONLY way i can find all children of Parent which match a certain value for 'location' seems to be as follows (note that 'location' is not on the Child class but in the subclass SubChild)
Code:
Query c = s.createQuery("from test.Child where location='location1'");
and all of the following attempts (which i think are very valid usage patterns), do not work
Code:
// this blows as it tries to find the 'location' field on Child and not SubChild
Criteria c = s.createCriteria(Parent.class)
.createCriteria("children")
.add(Expression.eq("location", "location1"));
// same thing here, but will work if we us SubChild.class instead
Criteria c = s.createCriteria(Child.class)
.add(Expression.eq("location", "location1"));
// again the same, 'location' is not found on child
Query c = s.createQuery("select parent from test.Parent parent " +
"left outer join parent.children as child " +
"where child.location='location1'");
// this is just a step away from working, removing the alias 'child' works
Query c = s.createQuery("from test.Child child where child.location='location1'");
Eventhough this can be somehow made to work in HQL but the extensibility I need for various types of queries that are formed in my app, is only available through the criteria API.
There has to be way for each createCriteria(associatoinPath) to retrieve not just the associated class but also all of it's subclasses.
If anyone has already got around to hacking at CriteriaImpl please let us know. Gavin if you already have a prototype that's not yet part of the product, we'll be glad to use it :-)
thanks - for bearing with me till here
ravi