在nhibernate中,Update(Object obj,Object id)和Update(字符串实体,Object obj)之间有什么不同

时间:2014-06-25 09:36:43

标签: c# nhibernate

我是NHibernate的新手。所以,我想在NHibernate框架中使用Isession更新数据库中的单个元素。

我不知道nhibernate中Update(Object obj,Object id)和Update(string entity,Object obj)之间有什么不同?

有谁知道吗?或者将元素更新为db的另一种方法?

1 个答案:

答案 0 :(得分:0)

正如ISession.cs

中所述
/// <summary>
/// Update the persistent state associated with the given identifier.
/// </summary>
/// <remarks>
/// An exception is thrown if there is a persistent instance with the same identifier
/// in the current session.
/// </remarks>
/// <param name="obj">A transient instance containing updated state</param>
/// <param name="id">Identifier of persistent instance</param>
void Update(object obj, object id);

/// <summary>
/// Update the persistent instance with the identifier of the given detached
/// instance.
/// </summary>
/// <param name="entityName">The Entity name.</param>
/// <param name="obj">a detached instance containing updated state </param>
/// <remarks>
/// If there is a persistent instance with the same identifier,
/// an exception is thrown. This operation cascades to associated instances
/// if the association is mapped with <tt>cascade="save-update"</tt>.
/// </remarks>
void Update(string entityName, object obj);

也许Update(obj)

/// <summary>
/// Update the persistent instance with the identifier of the given transient instance.
/// </summary>
/// <remarks>
/// If there is a persistent instance with the same identifier, an exception is thrown. If
/// the given transient instance has a <see langword="null" /> identifier, an exception will be thrown.
/// </remarks>
/// <param name="obj">A transient instance containing updated state</param>
void Update(object obj);

第一种情况是关于使用提供的ID更新具有obj状态的行。它的ID不是那么重要

第二个,期望,该会话未加载该对象。 entityName有助于找出正确的持久性(与实体相关的hbm.xml映射)

最后,简单的Update(obj)几乎与第一个一样,但是ID是从传递的obj

中解析出来的

一般情况下,如果我们在会话中加载任何实体,则不必调用Update() - 只需刷新()。更新有助于明确说:可能实例正在存在。最优选的方法是致电SaveOrUdpate(obj),请参阅

9.4.2. Updating detached objects