tags:

views:

254

answers:

2

Okay, simple question.. :)

In my webportal I got users, who obviously need to log on.

I got a table with users in a MSSQL-DB:

  • PK - GUID - UserId
  • CK - nvarchar - Email
  • CK - nvarchar - PhoneNumber

Now, I want users to be able to log on to my site using either their phonenumber or email, so I need to somehow fetch a user using either the phonenumber or email (both of which are guarenteed to be unique).

In NHibernate, the ISession only allows me to load an entity using the primary key (which is basically a surrogate key in my case). Is there anyway to load entities using candidate keys?

I'm very rusty in database-design, so this might be poor design, but I really think it's somewhat common to have a scheme like this - but if it is poor design, please let me know :)

Thanks in advance

A: 

Oh my.. Found the answer myself :)

Posting it here in case anyone runs into the same problem.

What we need is NHibernate Criterias or NQL. I found the information I needed here: http://www.beansoftware.com/asp.net-tutorials/nhibernate-log4net.aspx

-- under "Queries and Criteria" - Scroll about 4/5ths down.

(Don't think Im allowed to quote the info here, so I better not)

cwap
+2  A: 

Haven't used NHibernate, but in Java, using Session::executeQuery with some simple HQL would fetch the correct object. First, I assume that Hibernate's "Session" is equivilent to NHibernates "ISession". Second, I assume HQL is the same for both. If both of those are true, you'd probably use something like:

user = isession.executeQuery('from Users where Email = ?');

or

user = isession.executeQuery('from Users where PhoneNumber = ?');

Again, because I haven't worked with NHibernate, there may be some differences in syntax. Hope it helps!

Todd R
Ye, that's basically what I found out myself. HQL really improves the flexibility of Hibernate - can't believe I didn't find it before now :) Thanks a lot
cwap