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.  [ 9 posts ] 
Author Message
 Post subject: Problem using .class in WHERE clause
PostPosted: Tue Apr 04, 2006 9:00 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
NHibernate version: 1.0.2

I have a query using the .class special property where I'm trying to get objects based on their subclass:

Eg:

Code:
SELECT j FROM Jobs j WHERE j.class = Namespace.GovernmentJob


and

Code:
SELECT j FROM Jobs j WHERE j.class = Namespace.PrivateSectorJob


The classes are mapped using the table per subclass approach.

The SQL generated is almost correct, however the discriminator value being generated to compare against the case statement in the where clause of the SQL is always 0, regardless of the name of the class embedded in the HQL.

I can confirm that the class name (including the namespace) I'm using is correct according to the mapping files.

Is this a bug, or is there likely some problem in my mappings?

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 05, 2006 3:29 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Discriminators are only supported for table-per-hierarchy, not table-per-subclass.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 05, 2006 4:38 am 
Senior
Senior

Joined: Wed Jun 15, 2005 4:17 am
Posts: 156
I have encountered the same bug myself.

Sergey can you detail, why is not supported if the generated SQL takes into account the class discriminator and generates (almost) correct statements for it?

cheers,
Radu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 05, 2006 4:40 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
Really? The HQL seems to have allocated values to the different subclasses using a case statement, so it must have some idea of what each class represents.

The only issue here is that when the type name is included in the HQL statment (alias.class = fully_qualified.typename) the generated SQL has not coverted the typename to the value that matches the other values allocated in the SQL case statement.

For example, the HQL statement...

Code:
FROM InputProfile p WHERE p.class = Framework.FtpInputProfile


...produces the following SQL:

Code:
select inputprofi0_.InputProfileID as InputPro1_, case when [inputprofi0__1_].FtpInputProfileID is not null then 1 when [inputprofi0__2_].CftInputProfileID is not null then 2 when [inputprofi0__3_].SmtpInputProfileID is not null then 3 when [inputprofi0__4_].WebsiteInputProfileID is not null then 4 when [inputprofi0__5_].OcrInputProfileID is not null then 5 when inputprofi0_.InputProfileID is not null then 0 end as clazz_, inputprofi0_.Version as Version19_, inputprofi0_.Deleted as Deleted19_, inputprofi0_.Created as Created19_, inputprofi0_.InputInterface as Input6_19_, inputprofi0_.ParentUserID as Paren7_19_, inputprofi0_.Modified as Modif4_19_, [inputprofi0__1_].AutoForward as AutoF2_20_, [inputprofi0__1_].AutoForwardHost as AutoF3_20_, [inputprofi0__1_].AutoForwardPort as AutoF4_20_, [inputprofi0__1_].AutoForwardUsername as AutoF5_20_, [inputprofi0__1_].AutoForwardPassword as AutoF6_20_, [inputprofi0__1_].WelcomeMessage as Welco7_20_, [inputprofi0__1_].StoragePath as Stora8_20_, [inputprofi0__5_].ImmediateInject as Immed2_30_, [inputprofi0__5_].ConfirmationFax as Confi3_30_, [inputprofi0__5_].AlertFax as Alert4_30_, [inputprofi0__5_].IMediaProductionAccountID as IMedi5_30_, [inputprofi0__5_].ConfirmationTemplateDocID as Confi6_30_, [inputprofi0__5_].AlertTemplateDocID as Alert7_30_ from Framework.dbo.InputProfiles inputprofi0_ left outer join Framework.dbo.FtpInputProfiles [inputprofi0__1_] on inputprofi0_.InputProfileID=[inputprofi0__1_].FtpInputProfileID left outer join Framework.dbo.CftInputProfiles [inputprofi0__2_] on inputprofi0_.InputProfileID=[inputprofi0__2_].CftInputProfileID left outer join Framework.dbo.SmtpInputProfiles [inputprofi0__3_] on inputprofi0_.InputProfileID=[inputprofi0__3_].SmtpInputProfileID left outer join Framework.dbo.WebsiteInputProfiles [inputprofi0__4_] on inputprofi0_.InputProfileID=[inputprofi0__4_].WebsiteInputProfileID left outer join Framework.dbo.OcrInputProfiles [inputprofi0__5_] on inputprofi0_.InputProfileID=[inputprofi0__5_].OcrInputProfileID where (case when [inputprofi0__1_].FtpInputProfileID is not null then 1 when [inputprofi0__2_].CftInputProfileID is not null then 2 when [inputprofi0__3_].SmtpInputProfileID is not null then 3 when [inputprofi0__4_].WebsiteInputProfileID is not null then 4 when [inputprofi0__5_].OcrInputProfileID is not null then 5 when inputprofi0_.InputProfileID is not null then 0 end=0)


and the HQL...

Code:
FROM InputProfile p WHERE p.class = Framework.SmtpInputProfile


...produces the following SQL:

Code:
select inputprofi0_.InputProfileID as InputPro1_, case when [inputprofi0__1_].FtpInputProfileID is not null then 1 when [inputprofi0__2_].CftInputProfileID is not null then 2 when [inputprofi0__3_].SmtpInputProfileID is not null then 3 when [inputprofi0__4_].WebsiteInputProfileID is not null then 4 when [inputprofi0__5_].OcrInputProfileID is not null then 5 when inputprofi0_.InputProfileID is not null then 0 end as clazz_, inputprofi0_.Version as Version19_, inputprofi0_.Deleted as Deleted19_, inputprofi0_.Created as Created19_, inputprofi0_.InputInterface as Input6_19_, inputprofi0_.ParentUserID as Paren7_19_, inputprofi0_.Modified as Modif4_19_, [inputprofi0__1_].AutoForward as AutoF2_20_, [inputprofi0__1_].AutoForwardHost as AutoF3_20_, [inputprofi0__1_].AutoForwardPort as AutoF4_20_, [inputprofi0__1_].AutoForwardUsername as AutoF5_20_, [inputprofi0__1_].AutoForwardPassword as AutoF6_20_, [inputprofi0__1_].WelcomeMessage as Welco7_20_, [inputprofi0__1_].StoragePath as Stora8_20_, [inputprofi0__5_].ImmediateInject as Immed2_30_, [inputprofi0__5_].ConfirmationFax as Confi3_30_, [inputprofi0__5_].AlertFax as Alert4_30_, [inputprofi0__5_].IMediaProductionAccountID as IMedi5_30_, [inputprofi0__5_].ConfirmationTemplateDocID as Confi6_30_, [inputprofi0__5_].AlertTemplateDocID as Alert7_30_ from Framework.dbo.InputProfiles inputprofi0_ left outer join Framework.dbo.FtpInputProfiles [inputprofi0__1_] on inputprofi0_.InputProfileID=[inputprofi0__1_].FtpInputProfileID left outer join Framework.dbo.CftInputProfiles [inputprofi0__2_] on inputprofi0_.InputProfileID=[inputprofi0__2_].CftInputProfileID left outer join Framework.dbo.SmtpInputProfiles [inputprofi0__3_] on inputprofi0_.InputProfileID=[inputprofi0__3_].SmtpInputProfileID left outer join Framework.dbo.WebsiteInputProfiles [inputprofi0__4_] on inputprofi0_.InputProfileID=[inputprofi0__4_].WebsiteInputProfileID left outer join Framework.dbo.OcrInputProfiles [inputprofi0__5_] on inputprofi0_.InputProfileID=[inputprofi0__5_].OcrInputProfileID where (case when [inputprofi0__1_].FtpInputProfileID is not null then 1 when [inputprofi0__2_].CftInputProfileID is not null then 2 when [inputprofi0__3_].SmtpInputProfileID is not null then 3 when [inputprofi0__4_].WebsiteInputProfileID is not null then 4 when [inputprofi0__5_].OcrInputProfileID is not null then 5 when inputprofi0_.InputProfileID is not null then 0 end=0)


You will note that the discriminator value at the very end of the SQL statement is 0 in both cases, however in the case statement it's being evaluated against has allocated the discriminator value 1 to FtpInputProfile and 3 to SmtpInputProfile.

If I manually edit the SQL statement to set the discriminator value to 1 rather than 0 the DB returns all the rows that represent FtpInputProfiles, so the rest of the SQL seems to be correct, just the conversion of the string representing the type name is not being correctly transformed into the matching descriminator.

To my thinking this must be a bug...

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 05, 2006 10:03 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Yes, looks like you're right and it's a bug.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 05, 2006 3:11 pm 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
Jira issue created: http://jira.nhibernate.org/browse/NH-571

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 06, 2006 4:36 am 
Senior
Senior

Joined: Wed Jun 15, 2005 4:17 am
Posts: 156
merge_s.rottem wrote:


Everybody interested in using the class discriminator with table per subclass mapping please vote for the bug.

Anyway, the fix should be trivial.

radu


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 18, 2006 10:14 am 
Newbie

Joined: Tue Jul 18, 2006 10:12 am
Posts: 6
Voted!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 27, 2007 8:51 am 
Newbie

Joined: Tue Nov 27, 2007 8:44 am
Posts: 1
merge_s.rottem Thanks for code

_________________
Mp3 music
generic cialis online
generic levitra online


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