Given the following mappings:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
default-cascade="save-update" auto-import="true"
assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.NH1234" >
<class name="Bar" table="Bar" optimistic-lock="version">
<id name="Id" column="ID" type="Int32">
<generator class="identity" />
</id>
<version name="Timestamp" type="binary" generated="always" unsaved-
value="null" />
<property name="AField" not-null="true" />
<many-to-one name="Foo" column="FooID" cascade="all" class="Foo" not-
null="true" />
</class>
<class name="Foo" table="Foo" optimistic-lock="version">
<id name="Id" column="ID" type="Int32">
<generator class="identity" />
</id>
<version name="Timestamp" type="binary" generated="always" unsaved-
value="null" />
<property name="AField" not-null="true" />
<set name="Bars" lazy="true" cascade="all"
access="nosetter.camelcase">
<key column="FooID" />
<one-to-many class="Bar" />
</set>
</class>
</hibernate-mapping>
And using SQL Server 2005 the following test always fails using NH
2.0.1GA:
using (ISession session = OpenSession())
{
Bar retrievedBar = null;
Bar bar = new Bar();
bar.AField = 24;
Foo foo = new Foo();
foo.AField = 42;
foo.AddBar(bar);
session.Save(foo);
session.Flush();
session.Evict(bar);
session.Evict(foo);
retrievedBar = session.Get<Bar>(bar.Id);
// At this point the assumption is that bar and
retrievedBar should have
// identical values, but represent two different
POCOs. The asserts below
// are intended to verify this. Currently this test
fails on the comparison
// of the SQL Server timestamp (i.e. binary(8)) fields
because
// NHibernate does not retrieve the new timestamp
after the last update.
Assert.AreNotSame(bar, retrievedBar);
Assert.AreEqual(bar.Id, retrievedBar.Id);
Assert.AreEqual(bar.AField, retrievedBar.AField);
Assert.IsTrue(TimestampEquals(bar.Timestamp,
retrievedBar.Timestamp),
"Timestamps are different!");
}
As of yet, I have not been able to get this test running in the
NHibernate.Test project due to a failure while compling the mappings.
I have started another thread concerning this, so I don't want to
discuss that here; however, my assumption is that if I save an object,
evict the object from the cache and then retrieve another copy of the
object then the values should be identical. When you have a database
generated version field (BTW, I have also verified that this test
fails with an Int64 version instead of a SQL server timestamp version)
NHibernate, for whatever reason, doesn't retrieve the timestamp after
the last update when the above code is executed. If instead of failing
the insert, I tried to update and save the original Bar object above,
NHibernate throws an exception (as it should) whenever it detects the
versions are no longer in sync.
Maybe someone can answer this question for me, is it not typical that
NHibernate is used with database designs that generate versioning at
the database level?
> > > - Show quoted text -- Hide quoted text -