I would certainly need a second opinion on that but here is how I ran into this and what the cause was:
If you attach an object to a session (Save, Lock) and the object was previously loaded with the session this exception occurs. If this situation seems unlikely to you, you should consider also this case when you have cascading enabled on some braches.
Eg.
class Country { CountryId, Name }
class City ( CityId, Country, Name }, City has cascade all with Country.
Load the city { 1, {1, "USA"} , "New York" } (note that the country is eagerly loaded) and than evict it.
Create a new session, load another city { 2, {1, "USA}, "Manhattan" }. If you try now to save the "New York" object using the second session you will receive this error because you have a different instance of "USA" already associated with the session and nHibernate tries to attach the country object to the session because of the cascading.
The fun part is that I have the impression that this did not happen in previous versions of NHibernate.
|