简体   繁体   English

如何使用 NHibernate 仅更新记录的一列

[英]How to update just one column of a record with NHibernate

I want to update just one column of a record in my database.我只想更新数据库中记录的一列。 Not the whole record, but just one column (Stav).不是整个记录,而只是一列 (Stav)。 I am using NHibernate and I access through a DAO.我正在使用 NHibernate 并通过 DAO 访问。

Rezervace_dao rezervaceDao = new Rezervace_dao();
r.Stav = 1;
rezervaceDao.Update(r);

The code above update the whole record, but that's not what I want.上面的代码更新了整个记录,但这不是我想要的。

Thank you a lot.非常感谢。

In your example code, you call Update on an entity.在您的示例代码中,您对实体调用Update This if for updating detached entities, entities which are not tracked by your current NHibernate session.这是为了更新分离的实体,即当前 NHibernate 会话未跟踪的实体。

When updating detached entity, you tell NHibernate to take the supplied entity and consider it as being the complete new state of it, for updating it in database.当更新分离的实体时,你告诉 NHibernate 接受提供的实体并将其视为它的完整新状态,以便在数据库中更新它。 So all the properties you have not set will have their default values, and NHibernate will consider it has to update the database with those default values.所以所有你没有设置的属性都会有它们的默认值,NHibernate 会认为它必须用这些默认值更新数据库。

If you want to only change one property, you must first load your entity with NHibernate, change the property, then Flush the session.如果你只想改变一个属性,你必须首先用 NHibernate 加载你的实体,改变属性,然后Flush会话。

var r = session.Load<Rezervace>(id);
r.Stav = 1;
session.Flush();

There are no need to tell to NHibernate which entity are you updating when the entity was loaded from the current session.当从当前会话加载实体时,无需告诉 NHibernate 您要更新哪个实体。

By default, NHibernate will still update all properties with their previous values and the changed one with its new value.默认情况下,NHibernate 仍会使用它们以前的值更新所有属性,并使用其新值更新更改的属性。 As said by Rabban answer , you have to enable dynamic-update on your class mapping for changing this behavior, and have NHibernate update only the changed property.正如Rabban answer所说,您必须在类映射上启用dynamic-update才能更改此行为,并且让 NHibernate 仅更新已更改的属性。

Notes:注意事项:

I now consider I should have not answered but:我现在认为我不应该回答但:

  • Flag the question for closing, either as unclear or duplicate (now done as unclear).将要结束的问题标记为不清楚或重复(现在已完成为不清楚)。
  • Point toward current possible answers in already existing questions.指出现有问题中当前可能的答案。
    This answer to one of those other questions is better than mine by the way, if we are in the detached case.顺便说一句,如果我们处于独立案例中,那么对其他问题之一的答案比我的要好。

You have to set Dynamic-Update to true in your mapping.您必须在映射中将Dynamic-Update设置为true But you have to set it for every class where you want this behaviour.但是你必须为你想要这种行为的每个类设置它。

Please Note: The same can achieved for inserts with Dynamic-Insert请注意:使用Dynamic-Insert也可以实现相同的效果

The NHibernate Reference states:NHibernate 参考说明:

dynamic-update (optional, defaults to false): Specifies that UPDATE SQL should be generated at runtime and contain only those columns whose values have changed.动态更新(可选,默认为 false):指定 UPDATE SQL 应在运行时生成,并且只包含那些值已更改的列。

A example from the NHibernate reference (shortened):来自 NHibernate 参考的示例(已缩短):

<?xml version="1.0"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Eg"
namespace="Eg">
    <class name="Cat" table="CATS" dynamic-update="true">
            <id name="Id" column="uid" type="Int64">
                    <generator class="hilo"/>
            </id>
            <property name="BirthDate" type="Date"/>
            // other properties
    </class>
</hibernate-mapping>

The Disadvantages are discussed here .这里讨论了缺点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM