tags:

views:

97

answers:

2
A: 

From the docs: inverse (optional - defaults to false): If enabled, Hibernate will not try to insert or update the properties defined by this join.

My suggestion: In some cases to prevent additional updates with 2-way joins BOTH collections may need to be updated:

Contact c;
Address a;    
c.Addresses.Add(a);
a.Contacts.Add(c);

You are probably getting the additional Update statement because both sides have inverse=false and the first entity's relationship list was saved empty. NH is only doing what it thinks it needs to in order to get all relationships updated.

Brendan Kowitz
A: 

Thanks for the response but I believe I've found the problem. I had a repository class for each type of entity which implemented the CRUD operations. Each CRUD operation started it's own session/transaction, called Save/Update/etc then closed the transaction/session.

If I call the session.Save for Entity2 then session.Save for Entity1 (which contains the list of Entity2) NH seems to know that it has already persisted Entity2 in the session and hence doesn't try to update the record.

On the other hand, if I call session.Save for Entity2 then session.Save for Entity1 in a separate session/transaction it wants to update Entity2 again. I'm new to NH so not sure how it tracks which objects require updating, but it must reset between sessions?

Kinda makes my nice DDD repositories a little less useful though! Maybe the repositories should use a singleton session or something similar to avoid this problem?

Thanks, John

John
just remember that a session is not threadsafe. I think most people use 'Session-per-request' in aspnet or 'session-per-thread' in apps
Brendan Kowitz
Yes, looking at other examples on the web I should probably implement a 'unit of work' construct to manage the session which can be instantiated on opening a form (rich client app in my case) and closed (disposing the session) when the form is disposed.
John