在nhibernate中,Update(objectobj, Object id)和Update(string enti

本文关键字:Update enti string id objectobj nhibernate Object | 更新日期: 2023-09-27 18:10:16

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

我不知道Update(Object obj, Object id)和Update(string entity, Object obj)在nhibernate中的区别是什么?

有人知道吗?或者另一种方法来更新元素到db?

在nhibernate中,Update(objectobj, Object id)和Update(string enti

如session .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);

第一个案例是关于用obj的状态更新具有提供ID的行。它的ID并不那么重要

第二个,期望对象没有被这个会话加载。entityName帮助找到正确的持久性(实体相关的hbm.xml映射)

最后,简单的Update(obj)几乎与第一个相同,但id是从传递的obj中解析出来的

一般来说,如果我们用会话加载任何实体,您不必调用Update() -只需Flush()。Update有助于显式地说明:may实例已存在。最常用的方法是调用SaveOrUdpate(obj)(参见 )。

老。更新分离对象