These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Inefficient criteria query with polymorph association
PostPosted: Wed Jun 28, 2006 8:37 am 
Beginner
Beginner

Joined: Fri Apr 09, 2004 12:47 pm
Posts: 36
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.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 28, 2006 9:19 am 
Beginner
Beginner

Joined: Thu Sep 01, 2005 7:43 am
Posts: 31
Location: León (Spain)
I never use Criteria queries, but what about doing a subselect over the concrete subclass to do the filtering? Something like:

Criteria c = session.createCriteria(psrClass, "psr").createCriteria(Presentation.class, "pres").add(Property.forName("pres.powerSystemResource").in(Property.forName("power.id"))).list();

Hope this helps.
Bye.

Pd: don't try to copy & paste, it's just an idea :P

_________________
Please rate...

Expert on Hibernate errors... I've had them all... :P


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 28, 2006 9:48 am 
Beginner
Beginner

Joined: Fri Apr 09, 2004 12:47 pm
Posts: 36
Heh, already tried that approach too, but the criteria you specified returns PowerSystemResource objects, not Presentations... (to my astonishment, since the last criteria created in the chain is on Presentation objects).


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.