-->
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.  [ 3 posts ] 
Author Message
 Post subject: U
PostPosted: Wed Mar 05, 2008 2:09 am 
Newbie

Joined: Wed Mar 05, 2008 2:03 am
Posts: 3
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>

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; }
}
}



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


Top
 Profile  
 
 Post subject: Id column problem
PostPosted: Wed Mar 05, 2008 3:35 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi Srikanth,

I think <generator class="native" /> means (on SQL-Server at least) that the Id column must be an identity column.

If you want to set the Id property yourself, you might want to try <generator class="assigned" />.

http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/mapping.html#mapping-declaration-id-generator

Regards,
Richard


Top
 Profile  
 
 Post subject: Thank you very much
PostPosted: Wed Mar 05, 2008 3:44 am 
Newbie

Joined: Wed Mar 05, 2008 2:03 am
Posts: 3
Thank you very much..........

I have changed "native" to "assigned" . Its working...


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