views:

56

answers:

1

Hi,

I've been banging my head against a brick wall for nearly two days on this so any help would be great.

The scenario I've got is basically this.

I have an object called Campaign which I'm creating from scratch in a web client. The Campaign has a reference to another object, Portal. The Portal has been pulled from the database through NHibernate.

When I come to call CreateCampaign(Campaign cmp) NH barfs saying that the reference to Portal is transient and must be saved first. If i create EVERYTHING from scratch then it works fine.

So in short I have a new Campaign instance which references a detached instance of Portal. The cascades are set to 'save-update, merge'.

Do I need to reload all of the detached instances back into the session before I can call session.Save or is it something else I'm not aware of.

Sorry if this seems a little vague and for the lack of any code,..NDAs stop me from posting and code.

Thanks in advance.

+1  A: 

You can use ISession.Lock to attach your transient object to the same session that is being used to persist Campaign:

session.Lock(myPortal, LockMode.None);
myCampaign.Portal = myPortal;
session.Save(myCampaign);
Jamie Ide
doesn't this mean I've got to reconstruct the campaign though? I've got lots of detached instances further down the model so doing this would make most operations very involved. I was hoping that i could just pass the new campaign and let NH do the rest,..or am i asking too much?
Stimul8d
Jamie Ide
I actually ended up changing the cascade settings and using session.refresh and the session.merge. You on the right track though,.thanks.
Stimul8d