My code is as below:
Configuration cfg = new Configuration();
cfg.AddAssembly("NHibernate.Examples");
ISessionFactory factory = cfg.BuildSessionFactory();
ISession session = factory.OpenSession();
ITransaction transaction = session.BeginTransaction();
User newUser = new User();
newUser.Id = "Joseph_Cool";
newUser.UserName = "Joseph Cool";
newUser.Password = "abc123";
newUser.EmailAddress = "
[email protected]";
newUser.LastLogon = DateTime.Now;
// Tell NHibernate that this object should be saved
session.Save(newUser);
// commit all of the changes to the DB and close the ISession
transaction.Commit();
session.Close();
I get error at session.save(newUser)..
mapping is as below:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="NHibernate.Examples.QuickStart.User, NHibernate.Examples"
table="users">
<!--table="[NHibernate].[dbo].[users]">-->
<id name="Id" column="LogonId" type="String" length="20">
<generator class="assigned"/>
</id>
<property name="UserName" column="Name" type="String" length="40"/>
<property name="Password" type="String" length="20"/>
<property name="EmailAddress" type="String" length="40"/>
<property name="LastLogon" type="DateTime"/>
</class>
</hibernate-mapping>
i am getting error in the folloing method at bold line:
public object GetIdentifier(object entity)
{
object id;
if (entityMetamodel.IdentifierProperty.IsEmbedded)
{
id = entity;
}
else
{
if (idGetter == null)
{
if (identifierMapperType == null)
{
throw new HibernateException("The class has no identifier property: " + EntityName);
}
else
{
ComponentType copier = (ComponentType)entityMetamodel.IdentifierProperty.Type;
id = copier.Instantiate(EntityMode);
copier.SetPropertyValues(id, identifierMapperType.GetPropertyValues(entity, EntityMode), EntityMode);
}
}
else
{
id = idGetter.Get(entity);
} }
return id;
}
Please help me out.
I am totally newbie in NHibernate.