I am currently trying out NHibernate 2.0 and am having trouble understanding a query. I am using this for a login, I have a Member object that has a FirstName and a Password property. the password property gets hashed before it gets stored in to the database, and on the login, the password is hashed before it is compared to the value in the database. (FirstName will be replaced with UserName, im just experimenting for now)
I am monitoring the SQL that gets sent to the database with NHibernate Profiler (an awesome tool). My confusion right now is that I can set up a query with hql and Criteria API; and only the hql query works; the uniqueresult() and List() method of the Icriteria object both return null objects.
I look at NHibernate Profiler and look at the sql generated by both hql and criteria and then I go in to SQL Server Management Studio and both queries will run just fine and display the row that I need.. which is why I'm confused.
firstName and password are strings being passed in to the method
hql query
Code:
string hql = "from Member as mem where mem.FirstName = '"
+ firstName + "'" + " and mem.Password = '"
+ password + "'";
IQuery q = session.CreateQuery(hql);
IList<Member> memList = q.List<Member>();
(I know i should probably be using parameters for this, but its not in its final version)
Criteria
Code:
ICriteria crit = session.CreateCriteria(typeof(Member))
.Add(Expression.Eq("FirstName", firstName))
.Add(Expression.Eq("Password", password));
IList<Member> memList = crit.List<Member>();
(this is only giving me a problem on the password expression, if i comment it out, the query behaves the way it should, but if i comment out firstName and leave just the password expression it also returns null, but again, the sql being spit out is just fine)
hql sql
Code:
select member0_.MEMBER_ID as MEMBER1_5_,
member0_.MEMBER_FNAME as MEMBER2_5_,
member0_.MEMBER_LNAME as MEMBER3_5_,
member0_.MEMBER_USERNAME as MEMBER4_5_,
member0_.MEMBER_EMAIL as MEMBER5_5_,
member0_.MEMBER_PASSWORD as MEMBER6_5_,
member0_.MEMBER_CHALLENGE_QUESTION as MEMBER7_5_,
member0_.MEMBER_CHALLENGE_RESPONSE as MEMBER8_5_,
member0_.ROLE_ID as ROLE9_5_
from MEMBER member0_
where ( member0_.MEMBER_FNAME = 'hasher' )
and ( member0_.MEMBER_PASSWORD = '�J��̱��Lsӑ釘/��' )
criteria sql
Code:
SELECT this_.MEMBER_ID as MEMBER1_5_0_,
this_.MEMBER_FNAME as MEMBER2_5_0_,
this_.MEMBER_LNAME as MEMBER3_5_0_,
this_.MEMBER_USERNAME as MEMBER4_5_0_,
this_.MEMBER_EMAIL as MEMBER5_5_0_,
this_.MEMBER_PASSWORD as MEMBER6_5_0_,
this_.MEMBER_CHALLENGE_QUESTION as MEMBER7_5_0_,
this_.MEMBER_CHALLENGE_RESPONSE as MEMBER8_5_0_,
this_.ROLE_ID as ROLE9_5_0_
FROM MEMBER this_
WHERE this_.MEMBER_FNAME = 'hasher' /* @p0 */
and this_.MEMBER_PASSWORD = '�J��̱��Lsӑ釘/��' /* @p1 */
If I go in to sql server management studio and run those queries manually, they both return the same row, so it doesn't seem like it's the sql that's the problem.
Any ideas/explanations?