Reputation: 689
I Have a Nhibernate object called Car, this Car object have a PersietentBag IList collection called Doors, all in lazy loading.
if i do (IN SESSION 1)
int singleDoor = Car.Doors[0];
the lazyest collections loads from db and the related objects becomes added to the first level cache, the i will have in the 1rst level cache N objects Car and N Doors loaded from db.
From another hand (at other part of the code IN SESSION 2) i loads the same Car objects and do the same assigment
int singleDoor = Car,Doors[0];
and i Evicts Car and all the Door(s) objects from SESSION2
i modify the state of this objects and want to attach the doscontected objects tu the SESSION1 for save nd do
mySession.Update(Car);
But when i try to update the Door(s) obejcts obviously i have the (an other obejct with the same id, etc) exception is thrown because there's yet another object with the same id.
Bot it's dificult to find the old object to evict, how can i EVICT the old objects or clear the 1rst level cache (only by type and id) or discard old objects from cache and update what i want?.
thanks in advance.
Upvotes: 0
Views: 710
Reputation: 5679
This isn't because of the second level cache, it's because you're trying to save the entity from session 2 when it's already been loaded in session 1 (in fact it's the first level cache that's causing this).
The answer to your question is to use (in session 1), session.Evict(car), however that's not really the best approach - I would rather suggest using session.Merge(car) which will update the persistent object in session 1 without throwing the exception about another object with the same id.
Upvotes: 3