3

I want to update just one column of a record in my database. Not the whole record, but just one column (Stav). I am using NHibernate and I access through a 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.

7
  • what do you mean when you say one paramenter? May 24, 2017 at 8:59
  • 1
    You can set Dynamic-Update to true in your mappings. But you have to set it for every class where you want this behaviour.
    – Rabban
    May 24, 2017 at 9:05
  • @Rabban ok, so how then I do the update... something like: rezervaceDao.Update(r.Stav); did not work
    – pajasv
    May 24, 2017 at 9:13
  • @RobertoBenazzato I have class reservation, it has parameters (propertiees) like: date_from, id, state.... and when I close reservation, I would like to change only state. Do you understat now? :)
    – pajasv
    May 24, 2017 at 9:23
  • @pajasv how do you map your entities, HBM, FluentNHibernate, etc...?
    – Rabban
    May 24, 2017 at 9:30

2 Answers 2

5

In your example code, you call Update on an entity. This if for updating detached entities, entities which are not tracked by your current NHibernate session.

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. 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.

If you want to only change one property, you must first load your entity with NHibernate, change the property, then Flush the session.

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.

By default, NHibernate will still update all properties with their previous values and the changed one with its new value. 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.

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.
2
  • OK and where should I do it.. because i did updatein controllet and there is no session ref right?
    – pajasv
    May 24, 2017 at 10:09
  • I do not know how your DAO is implemented, this is why I just write here the basic operations you need to do on NHibernate API. Please consider, when asking, that you should stripe down your trouble to its bare essential, removing parts specific to your application and irrelevant to the question as much as possible. See minimal reproducible example. So yes in your case you have to move that where your application interacts with NHibernate API.
    – Frédéric
    May 24, 2017 at 10:18
1

You have to set Dynamic-Update to true in your mapping. 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

The NHibernate Reference states:

dynamic-update (optional, defaults to false): Specifies that UPDATE SQL should be generated at runtime and contain only those columns whose values have changed.

A example from the NHibernate reference (shortened):

<?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.

2
  • Note that the entity is detached entity.
    – Amit Joshi
    May 24, 2017 at 10:04
  • @AmitJoshi i'm afraid you are right. But he didn't stated it and it is possible that the entity is in the same Session/UnitOfWork but comes from another repository. Together with the answer from @Frédéric he should be able to solve it and understand the whole problem.
    – Rabban
    May 24, 2017 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.