-->
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.  [ 6 posts ] 
Author Message
 Post subject: How to use property name instead of column name in HQL?
PostPosted: Wed Aug 24, 2005 5:18 pm 
Beginner
Beginner

Joined: Mon Aug 15, 2005 11:38 am
Posts: 28
Hey guys,

I recently started using NHibernate and in the meantime my application goes beyond retrieving either all objects of a type or objects by their ID, meaning I'm using HQL queries.

Lets look at this query: "from User where FirstName=? and LastName=?". I have a domain class "User", which is mapped to "ReportUser" in a database. Furthermore, the User class has a "FirstName" property of type string, which is mapped to the column "FirstName1" in the DB (note the 1 in the end).
So here the expression "User" is referring to the name of my domain class and FirstName is supposed to be referring to the name of my domain class' property.

However, the query fails saying:
NHibernate.ADOException : Could not execute query
----> System.Data.SqlClient.SqlException : Invalid column name 'FirstName'.

When I replace FirstName with FirstName1 everything works. So obviously here I have a dependency between my business logic and the database. Furthermore it seems to me inconsistent that with User I'm referring to a mapped domain class and with FirstName1 I have to refer to a DB object.

Either I'm doing something wrong (could someone tell me how to use FirstName instead of FirstName1) or this is a bug.

What do you guys think? BTW: I just updated to the newest NHibernate, 0.9.1.

Thanks, Christoph


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 24, 2005 5:20 pm 
Beginner
Beginner

Joined: Mon Aug 15, 2005 11:38 am
Posts: 28
Forgot to mention one thing. This is the SQL that was generated (extracted from SQL Profiler):
exec sp_executesql N'select user0_.ID as ID, user0_.FirstName1 as FirstName1, user0_.RoleID as RoleID, user0_.LastName as LastName, user0_.CoordinatorOpID as Coordina6_, user0_.DivisionID as DivisionID from ReportUser user0_ where (FirstName=@p0)and(LastName=@p1)', N'@p0 nvarchar(4000),@p1 nvarchar(4000)', @p0 = N'Admin', @p1 = N''


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 6:20 am 
Senior
Senior

Joined: Thu Jun 02, 2005 5:03 pm
Posts: 135
Location: Paris
Try using an alias to reference your type:

Code:
from User u where u.FirstName=? and u.LastName=?


Cheers,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 7:38 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
Note that entities/properties in HQL are ALWAYS refering to the Domain Model (== the classes and their properties).

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 3:07 pm 
Beginner
Beginner

Joined: Mon Aug 15, 2005 11:38 am
Posts: 28
Hey Symon, thanks for your hint. It worked beautifully :D

KPixel, sorry but from all I can tell you're wrong. If I use the ISession.Find method with these two queries I get two different results:

1) "from User where FirstName=?"
vs
2) "from User u where u.FirstName=?"

The first one generates a SQL statement using FirstName directly as column name, whereas the second one uses FirstName to lookup the column mapping and in my case produces a SQL statement with FirstName1 as column name.

This seems to me like a bug so I'll see that I can create a little test app that I can submit.

Regards, Christoph


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 26, 2005 6:13 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
I never used the 1) :oops:

Anyway, this is probably a design choice because it would be very hard (in 1) to map 'FirstName' as NHibernate will have to guess which class it belongs to (is not straightforward with complex HQL)...

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


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