views:

958

answers:

2

Somewhat-simplified example situation: I have entities A and B which are incredibly "heavy" domain objects. Loading one from the database is a pretty big deal. Then I have an entity C, which is a very simple object that has a label string, one A, and one B -- both lazy.

I'm doing some low-level querying to create huge lists of C, so I know exactly what IDs I need to save for C.A and C.B, but I don't want to load up entire objects and set them to the properties, because the overhead is insane.

Instead, I want to just insert the IDs directly into my C entities, and then let the A and B properties on it be fully loaded later only if needed.

I see the <sql-insert/> tag in the documentation, but the section is really sparse.

Is there any way to do what I want to do inside the NHibernate framework, or should I just do raw SQL? I'm trying to keep database portability if possible, which makes me shy away from the raw option. Seems like there's got to be a better way I'm missing.

+1  A: 

I don't know if NHibernate allows this (separate objects on same base data) but usually I make "digest" objects which can be upconverted into the full-blown objects (even with lazy loading on the full-blown objects). I usually do this with code-gen'd or manual ORM layers.

Large collections are usually of digests, and then if the properties or methods needed aren't exposed by the digest, they are upconverted into complete objects for the call, or to be passed etc.

Cade Roux
I've added a "manual" layer, but I want to know if this can be done without working around NHibernate... so this was definitely helpful, but it doesn't seem like the "right" way.
Dan Fitch
Well, this would not necessarily be working around NHibernate, it would just be two classes, one of which is a read-only digest of the other, based on the same base tables.
Cade Roux
Aha, I get you now. I'm not sure what path to go down, but thanks for the clues... this is all a bit tricksy!
Dan Fitch
+1  A: 

Dan -- not sure if this will work without testing, but its worth a shot. I guess your already have the Ids that you want to add to your C -- so set your associations (many-to-one I'm assuming) to insert="false" and update="false" (maybe cascade="none" would work alternatively as well). Then create an "empty copy" of your A and B entities, add the Ids to them, add them to C and try to save C (Session.SaveOrUpdateCopy(C)). Hopefully NHibernate will not attempt to add a new A and B object into their respective table, but will simply set the correct Ids within the C table -- this way you don't have to load the A and B entities.

The bad part is, if you have to save or update A and B, you'll have to do it directly. The other bad part is that I don't have time to test this, so I don't know if it will work.

Watson
Aha, I see what you're saying. I'm not sure what NHibernate will think if I try to do a non-cascading save with A and B as "dummy objects", but I'll give it a shot.
Dan Fitch