A Jira issue has been created on this bug:
http://jira.nhibernate.org/browse/NH-704
As we all know, there are two means of re-attach a detached object: 1) sess.Update(obj) and 2) sess.Lock(c, LockMode.None);
In 1.2.0 Alpha1, the second meanning does not work. If there is a collection in the entity, Lock() will throw an exception (but now error was found in the log file).
Here are the code and the mapping file, which can also be found in the attachment.
Hibernate version: 1.2.0 alpha1
Mapping documents:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" schema="dbo" assembly="NHSessTest" default-lazy="false">
<class name="NHSessTest.Cat" >
<id name="Id" type="Int32" unsaved-value="0" >
<generator class="identity" />
</id>
<bag name="Children" cascade="all-delete-orphan" lazy="true">
<key column="parentId" />
<one-to-many class="NHSessTest.Cat" />
</bag>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
public class Cat{
private int id;
private IList children = new ArrayList();
//Note: I initialized the collection here, if removed, Lock will work.
public int Id{
get { return id; }
set { id = value; }
}
public IList Children {
get { return children; }
set { children = value; }
}
}
Code:
Cat c = new Cat();
sess.Save(c);
sess.Clear();
sess.Update(c); //Update() works fine
sess.Clear();
sess.Lock(c, LockMode.None); //Exception throw here
Full stack trace of any exception that occurs:NHibernate.HibernateException: reassociated object has dirty collection reference
at NHibernate.Impl.OnLockVisitor.ProcessCollection(Object collection, CollectionType type) in c:\net\nhibernate\nhibernate\src\NHibernate\Impl\OnLockVisitor.cs:line 58
at NHibernate.Impl.AbstractVisitor.ProcessValue(Object value, IType type) in c:\net\nhibernate\nhibernate\src\NHibernate\Impl\AbstractVisitor.cs:line 57
at NHibernate.Impl.AbstractVisitor.ProcessValues(Object[] values, IType[] types) in c:\net\nhibernate\nhibernate\src\NHibernate\Impl\AbstractVisitor.cs:line 30
at NHibernate.Impl.AbstractVisitor.Process(Object obj, IEntityPersister persister) in c:\net\nhibernate\nhibernate\src\NHibernate\Impl\AbstractVisitor.cs:line 82
at NHibernate.Impl.SessionImpl.Reassociate(Object obj, Object id, IEntityPersister persister) in c:\net\nhibernate\nhibernate\src\NHibernate\Impl\SessionImpl.cs:line 1983
at NHibernate.Impl.SessionImpl.Lock(Object obj, LockMode lockMode) in c:\net\nhibernate\nhibernate\src\NHibernate\Impl\SessionImpl.cs:line 2025
at NHSessTest.Tests.Work.ReAttachTest.ReAttachCatTest() in ReAttachTest.cs:line 23
Name and version of the database you are using:SqlServer 2000
[/code]