Hello,
I'm using NHibernate 1.2.0 and while saving data I'm getting an exception 'NHibernate.ADOException'
and it reads:
could not insert: [NHDemo.User#2][SQL: INSERT INTO USERS (Name, Password, EmailAddress, LastLogon, LoginId) VALUES (?, ?, ?, ?, ?)]
below is the mapping class file:
Code:
using System;
using System.Collections.Generic;
using System.Text;
using NHibernate.Mapping.Attributes;
using Nullables.NHibernate;
using Nullables;
namespace NHDemo
{
[NHibernate.Mapping.Attributes.Class(Table = "USERS")]
public class User
{
private int logonID;
private string userName;
private string password;
private string emailAddress;
private DateTime lastLogon;
public User()
{
}
[NHibernate.Mapping.Attributes.Id(1,Column = "LoginId", Name = "LogonID", TypeType = typeof(Int32))]
[NHibernate.Mapping.Attributes.Generator(2, Class = "assigned")]
[Param(3, Name = "assigned")]
public virtual int LogonID
{
get { return logonID; }
set { logonID = value; }
}
[NHibernate.Mapping.Attributes.Property(Column = "Name", TypeType = typeof(String),Length = 40)]
public virtual string UserName
{
get { return userName; }
set { userName = value; }
}
[NHibernate.Mapping.Attributes.Property(Column = "Password", TypeType = typeof(String), Length = 20)]
public virtual string Password
{
get { return password; }
set { password = value; }
}
[NHibernate.Mapping.Attributes.Property(Column = "EmailAddress", TypeType = typeof(String), Length = 40)]
public virtual string EmailAddress
{
get { return emailAddress; }
set { emailAddress = value; }
}
[NHibernate.Mapping.Attributes.Property(Column = "LastLogon", TypeType = typeof(DateTime))]
public virtual DateTime LastLogon
{
get { return lastLogon; }
set { lastLogon = value; }
}
}
}
Code:
Configuration conf = new Configuration();
conf.AddAssembly("NHDemo");
NHibernate.Mapping.Attributes.HbmSerializer.Default.Serialize("", System.Reflection.Assembly.GetExecutingAssembly());
conf.AddXmlFile("User.hbm.xml");
ISessionFactory factory = conf.BuildSessionFactory();
//cfg.AddXmlFile("User.hbm.xml");
ISession session = factory.OpenSession();
//User joseph = (User)session.Load(typeof(User),1);
ITransaction transaction = session.BeginTransaction();
NHDemo.User newUser = new NHDemo.User();
newUser.LogonID = 2;
newUser.UserName = "Joseph Cool";
newUser.Password = "abc123";
newUser.EmailAddress = "joe@cool.com";
newUser.LastLogon = DateTime.Now;
session.Save(newUser);
// commit all of the changes to the DB and close the ISession
transaction.Commit(); [b]//This is the place where I'm getting Exception[/b]
session.Close();
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelpHibernate version: Mapping documents:Code between sessionFactory.openSession() and session.close():Full stack trace of any exception that occurs:Name and version of the database you are using:The generated SQL (show_sql=true):Debug level Hibernate log excerpt:Problems with Session and transaction handling?
Read this:
http://hibernate.org/42.htmlCode: