0

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.

4

1 回答 1

3

这不是因为二级缓存,而是因为您试图从会话 2 中保存实体,而该实体已经在会话 1 中加载(实际上是导致此问题的一级缓存)。

您的问题的答案是使用(在会话 1 中)session.Evict(car),但这并不是最好的方法 - 我宁愿建议使用 session.Merge(car) 来更新会话 1 中的持久对象,而无需抛出关于另一个具有相同 id 的对象的异常。

于 2012-05-22T08:15:51.970 回答