0

All,

When loading an entity and its association path eagerly, then calling session.Flush(), causes an Update() to run on the associated entity.

Ex:

Invoice -> Project

Loading single invoice and the associated Project (loading Project eagerly), then calling session.Flush() runs an Update() statement on the Project. NO CHANGES have been done at all between the load and the Flush to any of the entities.

 IStatefulSession sess = PersistentInvoiceHeader.AcquireCurrentSession();
            InvoiceHeader hdr = sess.Session.CreateCriteria<InvoiceHeader>()
                    .Add(Restrictions.Eq(InvoiceHeader.PROP_ENTITYID, 25051))

                    .SetFetchMode(InvoiceHeader.PROP_PROJECT, NHibernate.FetchMode.Eager)
                    .UniqueResult<InvoiceHeader>();

            sess.Session.Flush();

Trace (Job_Info is the Project entity associated w/ the invoice):

NHibernate: SELECT ... FROM InvoiceHeader this_ inner join Job_Info project2_ on this_.ProjectId=project2_.entity_id WHERE this_.entity_id = @p0;@p0 = 25051 [Type: Int32 (0)]
NHibernate: UPDATE Job_Info SET RowVersion = @p0, Created = @p1, LastUpdated = @p2, public_surveyor = @p3, guid = @p4, updated_at = @p5, status = @p6, entry_date = @p7, file_number = @p8, client_reference = @p9, contact = @p10, order_date = @p11, instructions_1 = @p12, instructions_2 = @p13, instructions_3 = @p14, instrument = @p15, quote = @p16, quote_document = @p17, pin = @p18, project_notes = @p19, due_date = @p20, due_date_fw = @p21, lock_status = @p22, plan_attached = @p23, notes_attached = @p24, type_extra = @p25, currency_code = @p26, master_job_id = @p27, manager_id = @p28, contact_id = @p29, office_id = @p30, job_type_id = @p31, client_entity_id = @p32, country_id = @p33, region_id = @p34 WHERE entity_id = @p35 AND RowVersion = @p36;

I am really surprised that Nhibernate is running an Update() statement on something that has not changed! None of the entities are dirty, why would there be an Update?

I am using Nhibernate 3.3, .net 4.0, this is Console app.

ActiveX
  • 1,064
  • 1
  • 17
  • 37
  • 1
    I think sometimes this happens if the data read from the db does not match the types spec'ed by the entity. – mxmissile Aug 28 '14 at 17:37
  • Data types? What's the explanation behind this since to me that sounds like an issue w/ the Nhibernate library. – ActiveX Aug 28 '14 at 18:12
  • 2
    If you map nullable integer to int property, will be updated to zero. If you map long value to int will update too. – Najera Aug 28 '14 at 18:18
  • Najera said it perfectly. – mxmissile Aug 28 '14 at 19:46
  • I am not sure if I understand exactly what you're saying. Can I have an example of this? Does this happen during hydration of the entity? I need a bit more details. Thx. – ActiveX Aug 28 '14 at 20:23
  • 2
    possible duplicate of [NHibernate Session.Flush() Sending Update Queries When No Update Has Occurred](http://stackoverflow.com/questions/34852/nhibernate-session-flush-sending-update-queries-when-no-update-has-occurred) – Najera Aug 28 '14 at 21:13
  • definitely a duplicate. check this question http://stackoverflow.com/questions/2948754/nhibernate-linq-session-becomes-dirty-after-select/2949684#2949684. I links to Jose's, utility, GhostBuster, that will search out all instances where your mapping doesn't reflect your domain object. It will give you a list of the mismatches. – Fran Aug 29 '14 at 18:30

1 Answers1

0

Thanks for the answers,- it turned out to be a bad mapping.

ActiveX
  • 1,064
  • 1
  • 17
  • 37