tags:

views:

28

answers:

2

hello,

I'm using Nhibernate version 2.2 for mapping classes to tables in my project. following is my class file and mapping file

public abstract class BasicUser
{        
    public virtual int RowID { get; set; }

    public virtual string DisplayName { get; set; }

    public BasicUser()
    {
    }
}

<class name="BasicUser" table="UserAccounts"  >
<id column="RowID" type="Int32">
  <generator class="native" />
</id>
<property name="DisplayName" type="String" length="25" />
<!-- More mapping members -->
</class>

the issue Im having now is, whenever I save an object it saves with an automatically generated RowID for the object. But when I query the object from the database in a later time (using some other property in the IQuery) I get the object with RowID always set to 0. Could you guyz please tell me how to retrive an object with its current RowID. Thank you.

A: 

very sorry for the delay guys, I was away to get some sleep :).. heres my code

BasicUser user = null;
using (ISession session = sessionFactory.OpenSession())
{
    IQuery query = session.CreateQuery("From BasicUser user where user.Email=?");
    query.SetString(0, email);
    query.SetMaxResults(1);
    IList<BasicUser> results = query.List<BasicUser>();
    if (results.Count > 0)
        user = results[0];
}
return user;

this code works and I get all the details for BasicUser object exept its row ID.

Nadun
+1  A: 

You should set Name attribute value for the Id like this:

<class name="BasicUser" table="UserAccounts"  >
<id column="RowID" type="Int32" Name="RowID">
  <generator class="native" />
</id>
<property name="DisplayName" type="String" length="25" />
<!-- More mapping members -->
</class>
Sly
@Sly, wow it actually worked :) thank you very much. also thank you DanP,Rafael for your time on this matter :)
Nadun