Results 1 to 4 of 4

Thread: Error in saving an object to database using NHibernate

  1. #1
    Join Date
    Mar 2008
    Posts
    2

    Default Error in saving an object to database using NHibernate

    Hi,
    I am a new buddy using NHibernate. When i am trying to save the object, it showing me the fallowing exception.

    "could not insert: [Users.User][SQL: INSERT INTO [dbo].[users] (UserName, Password, EmailAddress) VALUES (?, ?, ?)]"} System.Exception {NHibernate.ADOException}

    the inner exception is :

    {"Cannot insert the value NULL into column 'LoginID', table 'EHM.dbo.users'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated."} System.Exception {System.Data.SqlClient.SqlException}

    my mapping file is as below

    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    namespace="Users"
    assembly="Users">
    <class name="Users.User" table="[dbo].[users]">
    <id name="Id" column="LoginID" >
    <generator class="native" />
    </id>

    <property name="UserName" column="UserName" />
    <property name="Password" column="Password" />
    <property name="EmailAddress" column="EmailAddress" />

    </class>
    </hibernate-mapping>


    Please any one help me in fixing the exception.
    Thank u very much in advance.
    Bye,
    srikanth.

  2. #2
    Join Date
    Mar 2008
    Posts
    2

    Default sorry i have forgot to add remaining code

    Here is my actual code

    try
    {

    cfg.AddAssembly("Users");
    ISessionFactory factory = cfg.BuildSessionFactory();
    ISession session = factory.OpenSession();
    ITransaction transaction = session.BeginTransaction();
    //Users.User newuser = new Users.User();
    User newuser = new User();
    newuser.Id = "sree";
    newuser.UserName = "Srikanth";
    newuser.Password = "kanth";
    newuser.EmailAddress = "sree@yahoo.com";
    session.Save(newuser); /* Here i am getting the problem*/
    transaction.Commit();
    session.Close();
    }

    Here is my persistant class
    public class User
    {
    private string id;
    private string userName;
    private string password;
    private string emailAddress;
    public User()
    {
    }

    public virtual string Id
    {
    get { return id; }
    set { id = value; }
    }

    public virtual string UserName
    {
    get { return userName; }
    set { userName = value; }
    }

    public virtual string Password
    {
    get { return password; }
    set { password = value; }
    }

    public virtual string EmailAddress
    {
    get { return emailAddress; }
    set { emailAddress = value; }
    }
    }

  3. #3
    Join Date
    Jul 2007
    Posts
    23

    Default NH auto-generated columns.

    Hi,

    I believe that NHibernate forums (on Hibernate site) might be a better place for such issues, which are related only to pure NHibernate usage.

    Anyway, I think your problem is that you are using
    Code:
    <generator class="native" />
    which assumes an auto generated column (e.g. an Identity column in SQL Server) in the table. So in this case, you cannot say -
    Code:
    newuser.Id = "sree";
    Since this is an auto-generated column, the database will itself populate its appropriate value.

    If you don't want to use auto-generated columns, then there are other ways to achieve that, so please refer NH documentation.

    HIH.
    Thanks and regards,
    Manoj.

  4. #4
    Join Date
    Oct 2006
    Location
    Bergen, Norway
    Posts
    365

    Default

    And I also think that the generated column should be of type int...

    Code:
    ...
    public int PrimKey
    {
        get { return primKey; }
        set { primKey = value; }
    }
    ...
    hbml.xml extract:
    PHP Code:
    ...
        <
    id name="PrimKey" column="CurveInfoId">
          <
    generator class="native" />
        </
    id>
    ... 
    This works for both MSSQL and MySQL.

    Cheers,
    Steinar.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •