tags:

views:

255

answers:

5

I have a situation where I need to load part of an object graph using custom SQL (for performance reasons). So I load up this collection using my custom SQL (ISession.CreateSQLQuery()) and then I assign that list to a property on another entity object.

The problem is that when I assign the list to the property, it makes that entity dirty along with all of the objects in the list, so when I go to save the object, it does hundreds of queries to save the entire list. Is there a way that I can specify that an object is NOT dirty (after I load it up myself)?

(Yeah, I know I could turn off cascade="save-update", but I really don't want to have to do that if I can avoid it.)

+3  A: 

I think there is a functionality to evict an entity.

That means it is not connected to NHibernate anymore.


UPDATED after Jon's various comments:

  • If you want NHibernate to manage the object, ie detect if it is dirty, then keep it managed.
  • If not, Evict() it, it won't be managed. You can still save it manually and so on, it's just that it won't be done automatically for you.

I don't see any middle ground, between automatic and manual...

Note that you can still persist in various ways, like saving manually the parent entity, a Set of child entities and so on... Many things are still possible.

KLE
But then all lazy loading doesn't work anymore on that object, so that won't work.
Jon Kruger
Yeah, there are definitely ways around the problem. It seems like it would be so much easier though if there were a way to just tell NH that something isn't dirty. Thanks for the help.
Jon Kruger
@Jon You're welcome. Thanks for your nice attitude :)-
KLE
+2  A: 

Expanding on KLEs answer, I would:

  1. Evict() the parent entity
  2. Load the child list
  3. Attach the list of children to the parent entity
  4. Merge() the whole thing back into nHibernate

At that point I believe that NHibernate will recognize everything as clean.

Greg Bahrey
Wow, you got your first SO point from answering my question. I feel special!
Jon Kruger
After you Merge() something in, NH still things that it's dirty.
Jon Kruger
+1  A: 

Can you just remove the property you use to store this manually fetched data from NHibernates tracking?

Aaron Fischer
No because I need it to save that property.
Jon Kruger
+1  A: 

Assuming that you are not persisting the property that the list is assigned to, you can remove that property from the NHibernate mapping. I haven't tested this, but my expectation is that assigning to that property would not cause IsDirty() to return true.

EDIT: Ok, try try this. Load the object from an IStatelessSession, call your custom SQL and assign the property. Then Lock the object into a new ISession and continue working with it. I think the Lock will cascade to child objects if your cascade setting is all or all-delete-orphan. If Lock does not cascade then you will have to manually walk the object graph.

Jamie Ide
I need it to save the property.
Jon Kruger
A: 

You could use an interceptor and then override the FindDirty method.

SideFX