NHibernate Session.Flush() Sending Update Queries When No Update Has Occurred

前端 未结 3 1643
终归单人心
终归单人心 2021-02-01 14:59

I have an NHibernate session. In this session, I am performing exactly 1 operation, which is to run this code to get a list:

public IList GetCust         


        
相关标签:
3条回答
  • 2021-02-01 15:32

    I also experienced this problem in NH 2.0.1 when trying to hide the inverse ends of many-to-many bags using access="noop" (hint: this doesn't work).

    Converting them to access="field" + adding a field on the class fixed the problem. Pretty hard to track them down though.

    0 讨论(0)
  • 2021-02-01 15:49

    Always be careful with NULLable fields whenever you deal with NHibernate. If your field is NULLable in DB, make sure corresponding .NET class uses Nullable type too. Otherwise, all kinds of weird things will happen. The symptom is usually will be that NHibernate will try to update the record in DB, even though you have not changed any fields since you read the entity from the database.

    The following sequence explains why this happens:

    1. NHibernate retrieves raw entity's data from DB using ADO.NET
    2. NHibernate constructs the entity and sets its properties
    3. If DB field contained NULL the property will be set to the defaul value for its type:
      • properties of reference types will be set to null
      • properties of integer and floating point types will be set to 0
      • properties of boolean type will be set to false
      • properties of DateTime type will be set to DateTime.MinValue
      • etc.
    4. Now, when transaction is committed, NHibernate compares the value of the property to the original field value it read form DB, and since the field contained NULL but the property contains a non-null value, NHibernate considers the property dirty, and forces an update of the enity.

    Not only this hurts performance (you get extra round-trip to DB and extra update every time you retrieve the entity) but it also may cause hard to troubleshoot errors with DateTime columns. Indeed, when DateTime property is initialized to its default value it's set to 1/1/0001. When this value is saved to DB, ADO.NET's SqlClient can't convert it to a valid SqlDateTime value since the smallest possible SqlDateTime is 1/1/1753!!!

    The easiest fix is to make the class property use Nullable type, in this case "DateTime?". Alternatively, you could implement a custom type mapper by implementing IUserType with its Equals method properly comparing DbNull.Value with whatever default value of your value type. In our case Equals would need to return true when comparing 1/1/0001 with DbNull.Value. Implementing a full-functional IUserType is not really that hard but it does require knowledge of NHibernate trivia so prepare to do some substantial googling if you choose to go that way.

    0 讨论(0)
  • 2021-02-01 15:52

    I have seen this once before when one of my models was not mapped correctly (wasn't using nullable types correctly). May you please paste your model and mapping?

    0 讨论(0)
提交回复
热议问题