Hibernate は拡張のための多くの場所を提供します。その中には Session がありIInterceptor
ます。多くの詳細が記載されたドキュメントがあります。
http://nhibernate.info/doc/nh/en/index.html#objectstate-interceptors
この場合、エンティティ (例: Client ) と毎回更新する必要があるプロパティ (例: Code ) を監視するカスタム エンティティを作成できます。したがって、実装は次のようになります。
public class MyInterceptor : EmptyInterceptor
{
public override int[] FindDirty(object entity, object id, object[] currentState, object[] previousState, string[] propertyNames, NHibernate.Type.IType[] types)
{
var result = new List<int>();
// we do not care about other entities here
if(!(entity is Client))
{
return null;
}
var length = propertyNames.Length;
// iterate all properties
for(var i = 0; i < length; i++)
{
var areEqual = currentState[i].Equals(previousState[i]);
var isResettingProperty = propertyNames[i] == "Code";
if (!areEqual || isResettingProperty)
{
result.Add(i); // the index of "Code" property will be added always
}
}
return result.ToArray();
}
}
注: これは単なる例です。ダーティ プロパティをチェックするための独自のロジックを適用します。
そして、次のようにラップする必要がありSession
ます。
var interceptor = new MyInterceptor()
_configuration.SetInterceptor(interceptor);
で、これです。クライアントが動的更新としてマークされている間、プロパティCodeは常にダーティとして設定されます
<class name="Client" dynamic-update="true" ...