-->
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: Equals versus Is Null in generated SQL
PostPosted: Thu Feb 09, 2006 3:07 pm 
Newbie

Joined: Thu Feb 09, 2006 3:03 pm
Posts: 6
Hi, all,

I am using the latest release of NHibernate in .NET Framework 1.1 and SQL Server 2000.

I wrote an inline query and I specifically used "is null" to test a condition in my where clause. I monitored the query in SqlProfiler. NHibernate generated "= null". This seems patently wrong. I tried changing my query to "= null" and back to "is null", but NHibernate always generated "= null".

Has anyone else experienced this? It is worrisome.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 09, 2006 6:22 pm 
Senior
Senior

Joined: Wed Jun 15, 2005 4:17 am
Posts: 156
can you post the query? or at least a minimal test case.

cheers,
radu


Top
 Profile  
 
 Post subject: The query
PostPosted: Thu Feb 09, 2006 6:37 pm 
Newbie

Joined: Thu Feb 09, 2006 3:03 pm
Posts: 6
Here's the gist of it. I have a class that models something like a table of tax rates. So let's say I'm trying to compare two tax rate tables, call them A and B. A is the standard table of rates for different types of items, and it has a row for every known type of item. Table A, being the standard table, has a known Id of 0. B is a different table, and B might only have a handful of tax rates that override specific rates for items in A. When we do the actual calculations, if there's no entry in B, we'll use the rate found in A. There's no direct (modeled) relationship between two tables or the rates they contain.

What I'm trying (and failing) to do is a theta-style join to return ALL the rates in A joined to any rates in B that also have rates in A. I am trying to get Nulls where there is no rate in B that corresponds to an item in A.

I should mention I'm new to NHibernate, and what I really want is a left join, but I'm not sure if I can do that when there is no modeled relationship between the things I'm trying to join, hence theta-style.

I'm psuedocoding to protect the innocent, so bear with me if I screw something up.

Code:
TaxRateTables
-------------
Id - Primary Key
TableName - varchar(100)

TaxRates
--------
Id - Primary Key
TableId - Foreign Key to TaxRateTables
ItemType - int (FK to another table, call it 'ItemTypes', would likely be modeled as a class 'Item')
Rate - currency

Dim query As New StringBuilder
query.Append("from TaxRateTables A, TaxRateTables B")
query.Append("where (A.Item = B.Item or B.Item is null) ")
query.Append("and A.Id = 0 ")
query.Append("and B.Id = ? ")

Dim myQuery As IQuery = myNHSession.CreateQuery(query.ToString)
myQuery.SetInt32(0, tableIdForB)
Dim myList As IList = myQuery.List()


Top
 Profile  
 
 Post subject: oooh weird
PostPosted: Thu Feb 09, 2006 6:49 pm 
Newbie

Joined: Thu Feb 09, 2006 3:03 pm
Posts: 6
Is there some kind of weird caching going on here?

I removed an Order By that I have in my original query (and which I didn't put in the example), recompiled the VS.NET solution and ran it, and SQL Profiler showed me the same old query with an Order By sitting in there.

So I did a forced rebuild of the entire solution, bounced my web server, and now I'm getting the correct query with "is null" and no order by.

That's very very weird.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 09, 2006 6:59 pm 
Senior
Senior

Joined: Wed Jun 15, 2005 4:17 am
Posts: 156
to me it looks that there is a relationship between TaxRateTable and TaxRates: a one-to-one relationship (which alse includes the zero case). You can model it with a foreign key or even by sharing the primary key value. By declaring the relationship you can use the join clause and have more efficient queries.

cheers,
radu


Top
 Profile  
 
 Post subject: well, yes
PostPosted: Thu Feb 09, 2006 7:01 pm 
Newbie

Joined: Thu Feb 09, 2006 3:03 pm
Posts: 6
There is a relationship between TaxRates and TaxRateTables, exactly as you said. I am trying to compare all the rates in one table to all the rates in another, and I need to display them in a grid, showing the overridden rates where rates are overridden, and the fallback rates where they are not.

I know exacly how I would do this in straight SQL, but I'm banging my head figuring out how to easily do the same with NHibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 09, 2006 7:15 pm 
Senior
Senior

Joined: Wed Jun 15, 2005 4:17 am
Posts: 156
well, you have to switch your mindeset to objects and operate only with objects. In the object world I would have a property for that calculated tax rate which in the getter will check if the associated object exists, and return the tax rate from the related object otherwise will return its own value. Of course this calculated property will not be mapped to any exisiting column in the db. I would map a one-to-one relationship between the two objects (check the docs regarding this topic). You could also expose the base tax rates in both objects.

HTH,
radu


Top
 Profile  
 
 Post subject: perhaps
PostPosted: Thu Feb 09, 2006 7:27 pm 
Newbie

Joined: Thu Feb 09, 2006 3:03 pm
Posts: 6
I hear you on the object point. I haven't even revealed my third level of rate tables, those that are overridden on a customer-by-customer basis. So I have:
Standard Rate Table < Override Rate Tables < Customer-specific Override Rate Tables.

So for every Override rate, I could have a one-to-one property that maps to the corresponding Standard Rate. And for every Customer rate I could have two one-to-one properties, one that maps to the rate in the Override table that the customer may be using, and one that maps to the Standard rate.

I'm frightened, however, of how this might actually perform. I foresee NHibernate executing dozens and dozens of queries when I know I could get everything I need in a single SQL query. I've not tried it, so I don't really know how it will perform, but I guess I'm not optimistic and I fear wasting my time going down that road.


Top
 Profile  
 
 Post subject: Re: perhaps
PostPosted: Thu Feb 09, 2006 7:37 pm 
Senior
Senior

Joined: Wed Jun 15, 2005 4:17 am
Posts: 156
cerulean47 wrote:
I'm frightened, however, of how this might actually perform. I foresee NHibernate executing dozens and dozens of queries when I know I could get everything I need in a single SQL query. I've not tried it, so I don't really know how it will perform, but I guess I'm not optimistic and I fear wasting my time going down that road.


well, hibernate is not the silver bullet for everything. In this case indeed, plain SQL is best. you can also use hibernate with plain sql.

cheers,
radu


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.