Question

What would cause a change to not generate an update statement for an existing entity? I have a command that updates a status enum on an entity. It changes it from Active to Deleted. I use the same command (generically) to update the status bit on other entities without a problem. I can update a single string on the entity in question without changing anything else and the update processes fine. I can observe all this going down in NHProf...

I guess my questions then are:

  • What would cause NHibernate to not update an entity when an enum value (and nothing else ... or other things to?) is/are changed?
  • What is the best way to track down issues like these?

Thanks for your help!

A clue. The property on the base type is:

public virtual EntityStatus Status { get; protected set; }

If I make the setter public and assign to it directly, NHibernate picks it up just fine. When it's protected, I call a method on the entity and update the Status there. Does that provide any clues?

Was it helpful?

Solution

So, this was a fun one and a total pain in the ass to track down. It turns out that using Lazy loading inside of an Any mapping somehow screws the ol' pooch somewhere. So calling a virtual method to update a protected property does not work when I do this:

Any(o => o.Thing, typeof(Guid), m =>
                        {
                            m.IdType<Guid>();
                            m.MetaType<string>();
                            m.MetaValue("Squirmy", typeof(SquirmyThing));
                            m.MetaValue("Odoriferous", typeof(OdoriferousThing));
                            m.Columns(id => id.Name("ThingId"), cr => cr.Name("ThingType"));
                            m.Lazy(true);
                            m.Cascade(Cascade.Persist);
                        });

But it DOES work when I do this:

Any(o => o.Thing, typeof(Guid), m =>
                        {
                            m.IdType<Guid>();
                            m.MetaType<string>();
                            m.MetaValue("Squirmy", typeof(SquirmyThing));
                            m.MetaValue("Odoriferous", typeof(OdoriferousThing));
                            m.Columns(id => id.Name("ThingId"), cr => cr.Name("ThingType"));
                            m.Lazy(false);
                            m.Cascade(Cascade.Persist);
                        });

Anyway, I really hope this helps someone else crazy enough to use an Any mapping.

OTHER TIPS

This can happen if the entity you are updating is not in the session object. Ensure you haven't closed the session that you queried the original object in or attached it to.

Also ensure that you are actually changing the value to something different than what it was before. If you have dynamic-update enabled for this entity and you aren't actually changing the value it won't generate an update.

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

I would enable NHibernate logging also and take a look at what it's spitting out you may find some useful information in the logs.

Honestly I would also post the offending code in your question. It's always easier to tell where things might go wrong by being able to look at the code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top