NHibernate 审计和继承类的问题
我有一个实现 IDomainObject
的 Animal
类。还有另一个类 Cat
,它继承自 Animal
。我对 NHibernate 映射使用 Table Per Subclass 继承策略,并将 CreatedDate
和 LastModified
属性映射到 Animal
表中的列以及Cat
表中的列。
我还使用 PreUpdate 事件处理程序为 LastModified 分配一个值。
当我更新 Cat 对象中的属性时,两个 UPDATES 会发送到数据库:一个用于 Animal
,另一个用于 Cat
。但是,Animal
的 UPDATE 会获取新的 LastModified 值,而 Cat
的 UPDATE 会保留旧值。
当在另一个会话中再次检索 Cat 实体时,这会导致一些问题。似乎 LastModified 被新值(来自 Animal)覆盖,有效地标记实体已脏,并触发另一个 UPDATE。
我的整体设计似乎有问题,但我不确定它是什么。有什么更好的方法来解决这个问题?
public class Animal : IDomainObject {
// ... various properties
public virtual DateTime CreatedDate { get; set; }
public virtual DateTime LastModified { get; set; }
public virtual int Version { get; protected set; }
}
public class Cat : Animal {
// ... various properties
public override DateTime CreatedDate { get; set; }
public override DateTime LastModified { get; set; }
public override int Version { get; protected set; }
}
public interface IDomainObject {
DateTime CreatedDate { get; set; }
DateTime LastModified { get; set; }
int Version { get; }
}
public bool OnPreUpdate(PreUpdateEvent eventItem) {
if (!(eventItem.Entity is IDomainObject)) {
return false;
}
var entity = eventItem.Entity as IDomainObject;
if (entity == null) {
return false;
}
var lastModified = DateTime.Now;
PersistState(eventItem.Persister, eventItem.State, "LastModified", lastModified);
entity.LastModified = lastModified;
return false;
}
private void PersistState(IEntityPersister persister, object[] state, string propertyName, object value) {
var index = Array.IndexOf(persister.PropertyNames, propertyName);
if (index == -1) {
return;
}
state[index] = value;
}
I have an Animal
class that implements IDomainObject
. There is another class, Cat
, that inherits from Animal
. I'm using a Table Per Subclass inheritance strategy for my NHibernate mappings and map the CreatedDate
and LastModified
properties to columns in the Animal
table and to columns in the Cat
table.
I also use a PreUpdate event handler to assign LastModified with a value.
When I update a property in a Cat object, two UPDATES get sent to the database: one for Animal
and one for Cat
. However, the UPDATE for Animal
is the one that gets the new LastModified value, and the UPDATE for Cat
keeps the old value.
This causes some issues when the Cat entity is retrieved again in another Session. It seems that LastModified gets overwritten with the new value (from Animal), effectively marking the entity has dirty, and triggering another UPDATE.
There seems to be something wrong with my overall design, but I'm not sure what it is. What is a better way to go about this?
public class Animal : IDomainObject {
// ... various properties
public virtual DateTime CreatedDate { get; set; }
public virtual DateTime LastModified { get; set; }
public virtual int Version { get; protected set; }
}
public class Cat : Animal {
// ... various properties
public override DateTime CreatedDate { get; set; }
public override DateTime LastModified { get; set; }
public override int Version { get; protected set; }
}
public interface IDomainObject {
DateTime CreatedDate { get; set; }
DateTime LastModified { get; set; }
int Version { get; }
}
public bool OnPreUpdate(PreUpdateEvent eventItem) {
if (!(eventItem.Entity is IDomainObject)) {
return false;
}
var entity = eventItem.Entity as IDomainObject;
if (entity == null) {
return false;
}
var lastModified = DateTime.Now;
PersistState(eventItem.Persister, eventItem.State, "LastModified", lastModified);
entity.LastModified = lastModified;
return false;
}
private void PersistState(IEntityPersister persister, object[] state, string propertyName, object value) {
var index = Array.IndexOf(persister.PropertyNames, propertyName);
if (index == -1) {
return;
}
state[index] = value;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不明白两件事:
为什么要重写 Cat 类中的 CreatedDate、LastModified 和 Version - 当它们没有新功能时?
为什么两个表中都有这三列?
在基类中拥有三列/属性应该足够了 - 其他任何东西都会带来麻烦。
Two things I don't understand:
Why do you override CreatedDate, LastModified and Version in your Cat-class - when they have no new functionality?
Why do you have those three columns in both tables?
It should be enough to have the three columns/properties in the baseclass - anything else is asking for trouble.