dimitrod
03-15-2007, 03:06 PM
Hello,
I have obtained the latest nightly build of the Spring.NHibernate package and created a simple application to test it. It is a console application which consists of the following classes:
public class Order
{
private int _id;
public virtual int Id
{
get { return _id; }
set { _id = value; }
}
private string _name;
public virtual string Name
{
get { return _name; }
set { _name = value; }
}
}
public class User
{
private int _id;
public virtual int Id
{
get { return _id; }
set { _id = value; }
}
private string _name;
public virtual string Name
{
get { return _name; }
set { _name = value; }
}
private IList<Order> _orders;
public virtual IList<Order> Orders
{
get { return _orders; }
set { _orders = value; }
}
}
public class UserDao : HibernateDaoSupport, IUserDao
{
public User GetUser(int id)
{
return HibernateTemplate.Get<User>(id);
}
public void SaveUser(User user)
{
}
}
Note that the SaveUser method is not implemented for the moment.
Here is my context definition:
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"
xmlns:db="http://www.springframework.net/database">
<object type="Spring.Objects.Factory.Config.PropertyPlaceholderC onfigurer, Spring.Core">
<property name="ConfigSections" value="databaseSettings"/>
</object>
<db:dbProvider id="DbProvider"
provider="SqlServer-1.1"
connectionString="Data Source=${db.datasource};Database=${db.database};Us er ID=${db.user};Password=${db.password};Trusted_Conn ection=False"/>
<object id="sessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate12">
<property name="DbProvider" ref="DbProvider"/>
<property name="MappingAssemblies">
<list>
<value>HibernateTest</value>
</list>
</property>
<property name="HibernateProperties">
<dictionary>
<entry key="hibernate.connection.provider"
value="NHibernate.Connection.DriverConnectionProvider"/>
<entry key="hibernate.dialect"
value="NHibernate.Dialect.MsSql2000Dialect"/>
<entry key="hibernate.connection.driver_class"
value="NHibernate.Driver.SqlClientDriver"/>
</dictionary>
</property>
</object>
<object id="HibernateTransactionManager"
type="Spring.Data.NHibernate.HibernateTransactionManager , Spring.Data.NHibernate12">
<property name="DbProvider" ref="DbProvider"/>
<property name="SessionFactory" ref="sessionFactory"/>
</object>
<object id="TxProxyConfigurationTemplate"
abstract="true"
type="Spring.Transaction.Interceptor.TransactionProxyFac toryObject, Spring.Data">
<property name="PlatformTransactionManager" ref="HibernateTransactionManager"/>
<property name="TransactionAttributes">
<name-values>
<add key="Save*" value="PROPAGATION_REQUIRED"/>
</name-values>
</property>
</object>
<object id="userDao" parent="TxProxyConfigurationTemplate">
<property name="Target">
<object type="HibernateTest.Services.UserDao, HibernateTest">
<property name="SessionFactory" ref="sessionFactory"/>
</object>
</property>
</object>
</objects>
And finally the Main method:
static void Main(string[] args)
{
IUserDao dao = ContextRegistry.GetContext().GetObject("userDao") as IUserDao;
using (SessionScope scope = new SessionScope())
{
User user = dao.GetUser(1);
user.Orders.Clear();
dao.SaveUser(user);
}
}
where SessionScope is defined like that:
class SessionScope : IDisposable
{
private OpenSessionInViewModule _osiv;
public SessionScope() : this(new OpenSessionInViewModule()) { }
public SessionScope(OpenSessionInViewModule osiv)
{
_osiv = osiv;
_osiv.Open();
}
public void Dispose()
{
_osiv.Close();
}
}
I use the OpenSessionInViewModule to avoid LazyInitializationExceptions =>
the Orders collection in the User class is defined as lazy in my hibernate mapping.
When I run this code I get the following exception:
System.NullReferenceException: Object reference not set to an instance of an object.
�* Spring.Transaction.Support.AbstractPlatformTransac tionManager.ProcessCommit(DefaultTransactionStatus status)
�* Spring.Transaction.Support.AbstractPlatformTransac tionManager.Commit(ITransactionStatus transactionStatus)
�* Spring.Transaction.Interceptor.TransactionAspectSu pport.DoCommitTransactionAfterReturning(Transactio nInfo transacti
onInfo)
�* Spring.Transaction.Interceptor.TransactionIntercep tor.Invoke(IMethodInvocation invocation)
�* Spring.Aop.Framework.AbstractMethodInvocation.Proc eed()
�* Spring.Aop.Framework.DynamicProxy.AdvisedProxy.Inv oke(Object proxy, Object target, Type targetType, MethodInfo targ
etMethod, Object[] args, IList interceptors)
�* CompositionAopProxy_9a3f1938296c4314bb81c3de12dc53 00.SaveUser(User user)
�* HibernateTest.Program.Main(String[] args) dans D:\work\HibernateTest\Program.cs:line 20
The problem comes from the fact that I modify the Orders collection before I call the SaveUser method:
user.Orders.Clear();
Do you have any idea where this error might come from?
I have obtained the latest nightly build of the Spring.NHibernate package and created a simple application to test it. It is a console application which consists of the following classes:
public class Order
{
private int _id;
public virtual int Id
{
get { return _id; }
set { _id = value; }
}
private string _name;
public virtual string Name
{
get { return _name; }
set { _name = value; }
}
}
public class User
{
private int _id;
public virtual int Id
{
get { return _id; }
set { _id = value; }
}
private string _name;
public virtual string Name
{
get { return _name; }
set { _name = value; }
}
private IList<Order> _orders;
public virtual IList<Order> Orders
{
get { return _orders; }
set { _orders = value; }
}
}
public class UserDao : HibernateDaoSupport, IUserDao
{
public User GetUser(int id)
{
return HibernateTemplate.Get<User>(id);
}
public void SaveUser(User user)
{
}
}
Note that the SaveUser method is not implemented for the moment.
Here is my context definition:
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"
xmlns:db="http://www.springframework.net/database">
<object type="Spring.Objects.Factory.Config.PropertyPlaceholderC onfigurer, Spring.Core">
<property name="ConfigSections" value="databaseSettings"/>
</object>
<db:dbProvider id="DbProvider"
provider="SqlServer-1.1"
connectionString="Data Source=${db.datasource};Database=${db.database};Us er ID=${db.user};Password=${db.password};Trusted_Conn ection=False"/>
<object id="sessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate12">
<property name="DbProvider" ref="DbProvider"/>
<property name="MappingAssemblies">
<list>
<value>HibernateTest</value>
</list>
</property>
<property name="HibernateProperties">
<dictionary>
<entry key="hibernate.connection.provider"
value="NHibernate.Connection.DriverConnectionProvider"/>
<entry key="hibernate.dialect"
value="NHibernate.Dialect.MsSql2000Dialect"/>
<entry key="hibernate.connection.driver_class"
value="NHibernate.Driver.SqlClientDriver"/>
</dictionary>
</property>
</object>
<object id="HibernateTransactionManager"
type="Spring.Data.NHibernate.HibernateTransactionManager , Spring.Data.NHibernate12">
<property name="DbProvider" ref="DbProvider"/>
<property name="SessionFactory" ref="sessionFactory"/>
</object>
<object id="TxProxyConfigurationTemplate"
abstract="true"
type="Spring.Transaction.Interceptor.TransactionProxyFac toryObject, Spring.Data">
<property name="PlatformTransactionManager" ref="HibernateTransactionManager"/>
<property name="TransactionAttributes">
<name-values>
<add key="Save*" value="PROPAGATION_REQUIRED"/>
</name-values>
</property>
</object>
<object id="userDao" parent="TxProxyConfigurationTemplate">
<property name="Target">
<object type="HibernateTest.Services.UserDao, HibernateTest">
<property name="SessionFactory" ref="sessionFactory"/>
</object>
</property>
</object>
</objects>
And finally the Main method:
static void Main(string[] args)
{
IUserDao dao = ContextRegistry.GetContext().GetObject("userDao") as IUserDao;
using (SessionScope scope = new SessionScope())
{
User user = dao.GetUser(1);
user.Orders.Clear();
dao.SaveUser(user);
}
}
where SessionScope is defined like that:
class SessionScope : IDisposable
{
private OpenSessionInViewModule _osiv;
public SessionScope() : this(new OpenSessionInViewModule()) { }
public SessionScope(OpenSessionInViewModule osiv)
{
_osiv = osiv;
_osiv.Open();
}
public void Dispose()
{
_osiv.Close();
}
}
I use the OpenSessionInViewModule to avoid LazyInitializationExceptions =>
the Orders collection in the User class is defined as lazy in my hibernate mapping.
When I run this code I get the following exception:
System.NullReferenceException: Object reference not set to an instance of an object.
�* Spring.Transaction.Support.AbstractPlatformTransac tionManager.ProcessCommit(DefaultTransactionStatus status)
�* Spring.Transaction.Support.AbstractPlatformTransac tionManager.Commit(ITransactionStatus transactionStatus)
�* Spring.Transaction.Interceptor.TransactionAspectSu pport.DoCommitTransactionAfterReturning(Transactio nInfo transacti
onInfo)
�* Spring.Transaction.Interceptor.TransactionIntercep tor.Invoke(IMethodInvocation invocation)
�* Spring.Aop.Framework.AbstractMethodInvocation.Proc eed()
�* Spring.Aop.Framework.DynamicProxy.AdvisedProxy.Inv oke(Object proxy, Object target, Type targetType, MethodInfo targ
etMethod, Object[] args, IList interceptors)
�* CompositionAopProxy_9a3f1938296c4314bb81c3de12dc53 00.SaveUser(User user)
�* HibernateTest.Program.Main(String[] args) dans D:\work\HibernateTest\Program.cs:line 20
The problem comes from the fact that I modify the Orders collection before I call the SaveUser method:
user.Orders.Clear();
Do you have any idea where this error might come from?