From
chapter 11.6
Quote:
11.6. Polymorphic queries
11.6. Polymorphic queries
A query like:
from eg.Cat as cat
returns instances not only of Cat, but also of subclasses like DomesticCat. Hibernate queries may name any Java class or interface in the from clause. The query will return instances of all persistent classes that extend that class or implement the interface. The following query would return all persistent objects:
from java.lang.Object o
The interface Named might be implemented by various persistent classes:
from eg.Named n, eg.Named m where n.name = m.name
Note that these last two queries will require more than one SQL SELECT. This means that the order by clause does not correctly order the whole result set. (It also means you can't call these queries using Query.scroll().)
The advantage of the criteria is that you can add all those ors that you were after.
As far as straight HQL (polymorphic queries) You should get a list of all elements in that table that are in the FileResourceImpl class hierarchy.
As far as I use them, criteria queries are for loading collections of top-level persistent entities (Parent objects with lazy-loaded children collections for the most part), and HQL is for Report Queries, for loading projected data, as in
"select employee.name, employee.eyeColor, address.id" -- straight RDBMS projection of fields, most often used with inner and/or outer joins.
In the list you will get rows of Object[] arrays, each element of which you will have to cast to the field type expected (String, Long, Date, etc).
Since you seem to be loading objects in the one hierarchy, I thought Criteria queries might be useful. You can iterate through the returned list using getClass(), instanceof, isassignablefrom (see spring's ClassUtils.getShortName() for a nice tool ) - etc, tests to see if it's one of the FileResourceImpl or one of the AudioFileResourceImpl types.
net.sf.hibernate.expression.Expression allows you to use disjunctions (all those "ors" I saw).
Though I don't know if the hibernate folks approve - from the mapping I may assume you're using table-per-class-hierarchy instead of joined-subclass, you can actually filter on the discriminator to be sure you only get the particular types of subclasses you want.
Code:
.add( Expression.eq("metadata_type", "com.bamboo.web.db.resources.AudioFileResourceImpl")
Do you have your showSql = true? I really learned a lot from Hibernate by watching the queries being generated by all the HQL and criteria queries I was using.