Hi all
I'm using NH with WCF, and implementation IPreUpdateListener for
auditing what properties of object are changed. My variant is using
session.SaveOrUpdateCopy, which allows access to @event.OldState in
listener.
But SaveOrUpdateCopy doesn't updates version of object if none of
object's properties has been changed.
How I can tell to NHibernate for updating version in?
My code:
var configuration = new Configuration();
configuration.AddAssembly(Assembly.GetExecutingAssembly());
configuration.Configure();
configuration.EventListeners.PostUpdateEventListeners=new
IPostUpdateEventListener[] {new PostUpdateListener()};
new SchemaExport(configuration).Create(true, true);
var sessionFactory =
configuration.BuildSessionFactory();
var parent=new Parent();
using(var session = sessionFactory.OpenSession())
using(var transaction = session.BeginTransaction())
{
parent = (Parent)
session.SaveOrUpdateCopy(parent);
transaction.Commit();
}
Console.WriteLine(parent.Version);
// SaveOrUpdate: 1. SaveOrUpdateCopy: 1.
parent.Children.Add(new Child(){Name = "1"});
using(var session = sessionFactory.OpenSession())
using(var transaction = session.BeginTransaction())
{
parent =
(Parent)session.SaveOrUpdateCopy(parent);
transaction.Commit();
}
Console.WriteLine(parent.Version);
// SaveOrUpdate: 2. SaveOrUpdateCopy: 2
parent.Children[0].Name="2";
using(var session = sessionFactory.OpenSession())
using(var transaction = session.BeginTransaction())
{
parent =
(Parent)session.SaveOrUpdateCopy(parent);
transaction.Commit();
}
Console.WriteLine(parent.Version);
// SaveOrUpdate: 3. SaveOrUpdateCopy: 2
parent.Children[0].Name="3";
using(var session = sessionFactory.OpenSession())
using(var transaction = session.BeginTransaction())
{
parent =
(Parent)session.SaveOrUpdateCopy(parent);
transaction.Commit();
}
Console.WriteLine(parent.Version);
// SaveOrUpdate: 4. SaveOrUpdateCopy: 2
object model and mappings are trivial:
public class Parent
{
public Parent()
{
Children = new List<Child>();
}
public virtual Guid Id { get; set; }
public virtual int Version { get; set; }
public virtual IList<Child> Children { get; set; }
}
public class Child
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
}
mappings:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="ConsoleApplication1" namespace="ConsoleApplication1">
<class name="Parent" table="Parent" dynamic-insert="true" dynamic-
update="true">
<id name="Id" column="ParentId" unsaved-
value="{00000000-0000-0000-0000-000000000000}">
<generator class="guid.comb" />
</id>
<version name="Version"/>
<bag name="Children" table="Child" cascade="all">
<key column="ParentId" />
<one-to-many class="Child"/>
</bag>
</class>
<class name="Child" table="Child">
<id name="Id" column="ChildId">
<generator class="guid.comb" />
</id>
<property name="Name" />
</class>
</hibernate-mapping>
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=.