-->
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.  [ 7 posts ] 
Author Message
 Post subject: implicit polymorphic query
PostPosted: Tue Jan 09, 2007 3:13 pm 
Beginner
Beginner

Joined: Wed Jul 05, 2006 12:45 pm
Posts: 21
Hibernate version:
1.2.0 beta 2

Hello,

I have read in NHibernate documentation that querying for a base class or implemented interface should return mapped subclasses and implementing classes:

http://www.hibernate.org/hib_docs/nhibe ... tance.html
(half way down... implicit polymorphism)

This works when i try something like "from System.Object", but not when I try to use one of my own interfaces - "from IResearchItemDatumParent" - which has two mapped implementing classes.

I get the following error:

QueryException occurred: in expected: <end of text>

I have tried specifying that interface using the fully qualified namespace, and name space + assembly (wrapped in single quotes) - this produces different errors as i'm sure the syntax is incorrect.

Am I missing something about implicit polymorphic support in NHibernate?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 09, 2007 4:17 pm 
Contributor
Contributor

Joined: Sat Sep 24, 2005 11:25 am
Posts: 198
Using CriteriaQuery for this should work
I don't think that HQL will do polymorphism on types that it doesn't know.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 09, 2007 5:48 pm 
Beginner
Beginner

Joined: Wed Jul 05, 2006 12:45 pm
Posts: 21
Ayende,

Thanks for your reply. I tried the Criteria API, and this indeed worked:

IList list = session.CreateCriteria(typeof(IResearchItemDatumParent)).List();


I originally tried using HQL instead of the Criteria API because the docs specifically stated that implicit polymorphic Get/Load was only supported through a "query". Please see the table at the bottom of:

http://www.hibernate.org/hib_docs/nhibe ... tance.html

Can anyone comment on the correct way to do an IMPLICIT (not mapped) polymorphic Get/Load? What did I do wrong in my HQL?

Thanks very much.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 09, 2007 6:45 pm 
Contributor
Contributor

Joined: Sat Sep 24, 2005 11:25 am
Posts: 198
Nothing is a wrong with your mapping.
Get/Load will not do a polymoprhic load because that would mean issuing more than a single query when you explicitly asked for a single query by the use of a PK.
HQL will not do the auto search for children that Criteria will do, at least not on interfaces.
Both HQL and Criteria are considered queries, as far as NHibernate is concerned


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 10, 2007 4:03 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
You should import the interface for this to work. In one of your mapping files put:
Code:
<import class="Namespace.IResearchItemDatumParent, Assembly" />

This will make the interface usable in HQL.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 10, 2007 4:50 pm 
Beginner
Beginner

Joined: Wed Jul 05, 2006 12:45 pm
Posts: 21
Sergey,

Thanks, the import statement allowed me to use the interface in an HQL query. I'd like to suggest that this be added to the docs somewhere.

For anyone who encounters this use case in the future: i simulated an implicit polymorphic Get with a query. This is really common sense, but thought i'd post it anyway for any beginners stumbling across the issue:

String id = 'xxx';

IResearchItemDatumParent parent =
session.CreateCriteria(typeof(IResearchItemDatumParent))
.Add(Expression.Eq("ID", id))
.UniqueResult<IResearchItemDatumParent>();

Thanks for all your help.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 25, 2007 2:59 am 
Beginner
Beginner

Joined: Wed Nov 29, 2006 10:32 am
Posts: 34
Just as a cautionary note: The following ...

whoissasha wrote:
Sergey,

For anyone who encounters this use case in the future: i simulated an implicit polymorphic Get with a query. This is really common sense, but thought i'd post it anyway for any beginners stumbling across the issue:

String id = 'xxx';

IResearchItemDatumParent parent =
session.CreateCriteria(typeof(IResearchItemDatumParent))
.Add(Expression.Eq("ID", id))
.UniqueResult<IResearchItemDatumParent>();

Thanks for all your help.


... is not that common sense: In contrast to a standard Load ("Get"), it will always go to the database (even if you have a second level cache in place!). It might or might not be the case that the acceptable ...

Regards
Harald


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