一个简单的方法是做session.Refresh(实体),但是这会导致额外的查询。
在Interceptor中工作,看起来很重。
http://www.nhforge.org/doc/nh/en/index.html#objectstate-interceptors(11.1)
你想要的东西,如:
public override bool OnFlushDirty(object entity,
object id,
object[] currentState,
object[] previousState,
string[] propertyNames,
IType[] types)
{
if(entity is DomainObjectICareAbout)
for (int i=0; i < propertyNames.Length; i++) {
if (currentState[i] is DateTime) {
DateTime dt = (DateTime)currentState[i];
dt = dt.AddMilliseconds(-1 * dt.Millisecond);
return true;
}
}
}
return false;
}
同类的事情的OnSave()。
在Custom IUserType中这样做可能会更好,但我不确定用户类型是否暴露了这样做的能力?
像一个IUserType:
public void NullSafeSet(System.Data.IDbCommand cmd, object value, int index)
{
DateTime dt = (DateTime)value;
dt = dt.AddMilliseconds(-1 * dt.Millisecond);
NHibernateUtil.DateTime.NullSafeSet(cmd, dt, index);
}
我不知道......我不知道最好的答案。只是给出想法。可能有更好的方法!
我想我现在要使用session.Refresh,即使它再次击中数据库。这已经证明是有用的:我发现我没有在连接字符串中设置字符集,所以我丢失了Unicode字符;)。 – Andrew 2009-07-21 14:17:53
嗯,重新读你的问题(有关保持同一会话的部分),它看起来像Session.Refresh()是正确的答案。 – anonymous 2009-07-21 15:15:30