Hi guys - the user class originally inherits from an Entity class which provides the Save functionality. Ive stipped this out into the User class (please note I dont normally use the session in this way but it does replicate the error):
User class:
using System;
using System.Collections.Generic;
using System.Text;
using NHibernate.Cfg;
using NHibernate;
namespace PersistanceUpdate
{
public class User
{
private int _id;
private string _name;
private IList<Country> _userCountries = new List<Country>();
private Configuration cfg = new Configuration().Configure();
private ISessionFactory _factory = null;
public User()
{
_factory = cfg.BuildSessionFactory();
}
public int Id
{
get { return this._id; }
set { this._id = value; }
}
public string Name
{
get { return this._name; }
set { this._name = value; }
}
public IList<Country> UserCountries
{
get { return this._userCountries; }
set { this._userCountries = value; }
}
public void Save()
{
ISession session = _factory.OpenSession();
ITransaction trans = session.BeginTransaction();
try
{
session.Save(this);
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
finally
{
if(session != null)
session.Close();
}
}
}
}
And the user mapping class is as follows:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-cascade="none" default-lazy="false" schema="Test.dbo">
<class
name="PersistanceUpdate.User,PersistanceUpdate"
table="Users">
<id name="Id" column="UserId" type="int" length="4" unsaved-value="0">
<generator class="identity" />
</id>
<property name="Name" column="UserName"/>
<bag name="UserCountries" table="UserCountries" generic="true">
<key column="UserId"/>
<many-to-many class="PersistanceUpdate.Country,PersistanceUpdate" column="CountryId" />
</bag>
</class>
</hibernate-mapping>
I get the error when I run the following test:
User newUser = new User();
newUser.Name = "User Name";
newUser.Save();
Note - if I remove the bag from the mapping file the user saves correctly
The Country mapping file is as follows
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-cascade="none" default-lazy="false" schema="Test.dbo">
<class
name="PersistanceUpdate.Country,PersistanceUpdate"
table="Countries" >
<id name="Id" column="CountryId" type="int" length="4" unsaved-value="0">
<generator class="identity" />
</id>
<property name="Name" column="CountryName"/>
</class>
</hibernate-mapping>
My hibernate.cfg.xml file looks like:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory >
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">{My Connection String}</property>
<property name="show_sql">false</property>
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
<property name="use_outer_join">true</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
</session-factory>
</hibernate-configuration>
As a sanity check I left eveything the same but changed the NHibernate dll back to v1.2.0.3001 and then save worked correctly
Any help would be much appreciated
many thanks
W
|