NHibernate ISession.Update [英] NHibernate ISession.Update

查看:118
本文介绍了NHibernate ISession.Update的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经注意到,通过使用log4net,在调用ISession.Update时,它将更新所有更改的对象.
例如:

I have noticed, by using log4net, that when calling ISession.Update, it updates all the changed objects.
For example:


// 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();
}

这是NHibernate的默认行为还是与映射文件有关?
我可以让NHibernate一次一次更新吗?

Is this the default behavior of NHibernate or has something to do with my mapping file?
Can I make NHibernate update one by one?

推荐答案

它是

Hibernate维护对象的缓存 已插入,更新或 已删除.它还维护了 已从中查询的对象 数据库.这些对象是 称为持久对象 只要是 用来获取它们的状态仍然有效. 这意味着对 这些对象在一个范围内 交易是自动的 交易完成后持续存在 坚定的.这些更新是隐式的 在交易范围内 而且您不必显式地打电话 任何保留值的方法.

Hibernate maintains a cache of Objects that have been inserted, updated or deleted. It also maintains a cache of Objects that have been queried from the database. These Objects are referred to as persistent Objects as long as the EntityManager that was used to fetch them is still active. What this means is that any changes to these Objects within the bounds of a transaction are automatically persisted when the transaction is committed. These updates are implicit within the boundary of the transaction and you don’t have to explicitly call any method to persist the values.

来自 休眠陷阱第2部分 :

Q)我仍然需要保存并 更新内部交易?

Q) Do I still have to do Save and Update inside transactions?

Save()仅适用于具有以下特征的对象: 不是持久性的(例如新的 对象).您可以使用Update带来 被逐出的物体 进入会话.

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.

来自 NHibernate的自动(脏检查)更新行为 :

我刚刚发现,如果我得到一个 来自NHibernate会话的对象,以及 更改对象的属性, NHibernate将自动更新 没有我提交的对象 调用Session.Update(myObj)!

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.从不.这会让你 明确的操作,即:在tx.Commit()或session.Flush()上. 当然,这仍然会更新 提交/刷新后的数据库.如果你这样做 不想这种行为,然后打电话 session.Evict(yourObj),它将 然后变成瞬态和NHibernate 不会为此发出任何数据库命令.

Answer: You can set Session.FlushMode to FlushMode.Never. This will make your operations explicit ie: on tx.Commit() or session.Flush(). Of course this will still update the database upon commit/flush. If you do not want this behavior, then call session.Evict(yourObj) and it will then become transient and NHibernate will not issue any db commands for it.

这篇关于NHibernate ISession.Update的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆