Hi,
in my model I do have a Presentation class that does refer, using
ManyToOne, a PowerSystemResource class, which is the root of an
inheritance hierarchy with 28 subclasses (and it may grow further),
mapped with the table per subclass approach (subclasses do have many attributes).
Now, I'm trying to get out all Presentation objects attached to a particular
type of PowerSystemResource, that is, a particular subclass of it, and have
the query expressed using the criteria API since I may need to add
conditions dynamically.
What I tried first is something like:
Criteria c = session.createCriteria(Presentation.class);
c.createAlias("powerSystemResource", "p").add(Restrictions.eq("p.class", psrClass));
where psrClass is the subclass I want to extract (a Class object)
It works, but the generated query is way too inefficient, it's a join between
29 tables with a decode to get out the only required rows -> most of the
time 20 to 26 tables in the join are totally useless, because they refer to
other parts of the hierarchy. Basically, the query takes twice the time as
the benchmark I have to respect.
Too bad, because theoretically the restriction on the class could be used
by hibernate to reduce the number of joined tables.
The I tried to add a lazy collection (presentations) from
PowerSystemResource just for the sake of traversing the association from
the other side, and tried the following:
Criteria c = session.createCriteria(psrClass);
c.createAlias("presentations", "p");
c.setProjection(Projections.property("p"));
hoping to get out only the presentations... unfortunately it doesn't work
neither, since Hibernate tells me it cannot find the p property in the
psrClass... apparently aliases are used as such only when you have
alias.property, but unfortunately I do need the whole Presentation object.
I'm using Hibernate 3.2.0cr2 and hibernate annotations 3.2.0cr1.
|