sh_*_*alh 3 nhibernate session
我注意到,通过使用log4net,在调用ISession.Update时,它会更新所有已更改的对象.
例如:
// Change 2 instances
user1.IsDeleted = true;
user2.UserName = "Xyz";
// Call session.Update to update the 2 users
using (ITransaction transaction = session.BeginTransaction())
{
Session.Update(user1); // This updates both user1 & user2
transaction.Commit();
}
using (ITransaction transaction = session.BeginTransaction())
{
Session.Update(user2); // Now there is no need for this
transaction.Commit();
}
Run Code Online (Sandbox Code Playgroud)
这是NHibernate的默认行为还是与我的映射文件有关?
我可以逐个进行NHibernate更新吗?
reb*_*ard 12
It's the normal and default behavior:
Hibernate维护已插入,更新或删除的对象的缓存.它还维护从数据库中查询的对象的缓存.只要用于获取它们的EntityManager仍处于活动状态,这些对象就称为持久对象.这意味着在事务提交时,事务边界内对这些对象的任何更改都会自动保留.这些更新隐含在事务的边界内,您不必显式调用任何方法来保存值.
问)我还需要在交易中进行保存和更新吗?
Save() is only needed for objects that are not persistent (such as new objects). You can use Update to bring an object that has been evicted back into a session.
From NHibernate's automatic (dirty checking) update behaviour:
I've just discovered that if I get an object from an NHibernate session and change a property on object, NHibernate will automatically update the object on commit without me calling Session.Update(myObj)!
答:您可以将Session.FlushMode设置为FlushMode.Never.这将使您的操作显式,即:在tx.Commit()或session.Flush()上.当然,这仍然会在提交/刷新时更新数据库.如果你不想要这种行为,那么调用session.Evict(yourObj)然后它会变成瞬态的,NHibernate不会为它发出任何db命令.