开发者

NHibernate not updating parent object's table when using event listeners

开发者 https://www.devze.com 2023-04-13 06:36 出处:网络
I\'m trying to add audit fields to my domain objects before they are inserted or updated by utilizing NHibernate event listeners. Utilizing the IPreInsert and IPreUpdate event listener interfaces, I a

I'm trying to add audit fields to my domain objects before they are inserted or updated by utilizing NHibernate event listeners. Utilizing the IPreInsert and IPreUpdate event listener interfaces, I am able to set the auditing fields on all the domain objects except for those that have the auditing fields stored in a parent object's table. For example, here are my entity classes:

public class Person : Entity
{
   public string name {get; set;}
}

public class Entity : PersistentEntity
{
   public int EntityTypeId {get; set;}
}

public abstract class PersistentEntity
{
   public virtual Guid Id { get; set;}
   public virtual Guid UpdateUser {get; set;}
   public virtual DateTime UpdateDate {get; set;}
}

Here are my mappings:

public class PersonMap : SubclassMap<Person>
{
   Map(x => x.Name);
}

public class EntityMap : PersistentEntityMap<Entity>
{
   Map(x => x.EntityTypeId);
}

public abstract class PersistentEntityMap<T> : ClassMap<T> where T : PersistentEntityMap
{
    Id(x => x.Id).GeneratedBy.GuidComb();
    Map(x => x.UpdateUser);
    Map(x => x.UpdateDate);
}

And here is how the tables are defined:

Person Table:

  • Id
  • Name

Entity Table:

  • Id
  • EntityTypeId
  • UpdateUser
  • UpdateDate

So given this setup, the IPreInsert event listener works perfectly, inserting a new person into the person table and inserting a new Entity into the Entity table with UpdateUser and UpdateDate correctly populated. However, if I where to update the person's name, the IPreUpdate event listener fires and does change the UpdateDate field on the object, but an update on the entity table is never performed. Taking advise from this page,

http://nhforge.org/wikis/howtonh/changing-values-in-nhibernate-events.a开发者_如何学Cspx

I tried using both IFlushEntity and ISaveOrUpdate event listeners to no effect.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号