-->
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.  [ 4 posts ] 
Author Message
 Post subject: Querying sub-class properties
PostPosted: Mon Oct 15, 2007 6:37 pm 
Newbie

Joined: Mon Oct 15, 2007 6:07 pm
Posts: 4
This question is purely about querying although the solution may require changes to the class model we have created.

In our application we two sets of classes mapped together at the highest level, so we have the abstract class 'Request' which contains a property of type 'Parameters'. The relationship is mapped as a many-to-one in Request.hbm.xml because a single Parameters instance can be referenced by many Requests.
N.B We have followed the table-per-subclass method.

We then have concrete classes derived from Request and Parameters, so for example a PriorityRequest has a corresponding PriorityParameters class. However this relationship is never explicitly declared and relies on the mapping declared in the parent classes.

This scenario works perfectly well for all but one scenario. It's simple to return a list of PriorityRequests based on properties of the Parameters class but we have been unable to query based on properties of the PriorityParameters class. We've investigated the various query options and have not found a solution. Is this possible given the way we've defined our classes and mappings? If not is there a simple change we can make to allow it?

Thanks in advance,
Phil


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 16, 2007 2:48 pm 
Regular
Regular

Joined: Fri Jan 20, 2006 7:45 pm
Posts: 97
Location: San Antonio, TX
You should be able to do this with an HQL query (a guess).

Like so:
Code:
"from PriorityRequest as request
   where request.PriorityParameters.Param1 = :param1Value"


In any case, I would enable logging (see log4net/NHibernate) and see what SQL is being generated based on your queries.

The mapping you describe sounds right (you'd need a many-to-one) and the classes sounds simple enough. If you are terribly surprised by the SQL generated, post your mappings and the SQL generated and perhaps someone can give you better help.

I have a couple of objects with the same relationship and mapping. I ran a quick test using a composite object's field as a constraint and it worked fine.

_________________
Dum spiro, spero
-------------------------------
Rate my post if it helps...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 17, 2007 4:38 am 
Newbie

Joined: Mon Oct 15, 2007 6:07 pm
Posts: 4
Thanks for the response. Unfortunately the PriorityRequest object does not have a PriorityParameters property - it has a Parameters property that it inherits from its ancestor Request class.

I've tried doing
"from PriorityRequest pr
join fetch pr.Parameters as PriorityParameters
where pr.Parameters .PrioritySpecificParam = :param1Value"

but I get the error
"unexpected token as[....]"

I realise that 'as' is for naming variables but it's that sort of functionality I am looking for, some sort of cast. In Hibernate (Java) I could use the .class property but I can't find anything similar (e.g. Type or GetType()) in NHibernate. Any thoughts?

I should mention that we are using 1.0.3. I couldn't see anything in the 1.2.0 release note that would help us out with this issue but I could have missed it.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 18, 2007 1:44 pm 
Regular
Regular

Joined: Fri Jan 20, 2006 7:45 pm
Posts: 97
Location: San Antonio, TX
Hmmm, I don't have the code for 1.0.3 on hand, so I can't give a definitive response, but...

the error seems to indicate an exception during parsing the query. Perhaps you could try the query after removing 'as'.
The query would look like this:
Code:
from PriorityRequest request
   where request.PriorityParameters.Param1 = :param1Value


On the other hand, what do you mean when you say you are wanting a type cast? The query above should return a PriorityRequest instance (assuming all the mapping is in place and valid).

_________________
Dum spiro, spero
-------------------------------
Rate my post if it helps...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.