What is th开发者_运维问答e meaning of Object Identity Problem in NHibernate?
The object identity problem is that there is no perfect (aka 100% exact) way to make sure that an object identity for a plain CLR object has a 1:1 mapping to an object identity in NHibernate.
The reason is that object identity in the one case (CLR) is defined by the object pointer, object identity in the other case by a database ID value.
So it might not be possible to fully reliably map a CLR object to a NHibernate object.
BTW: That is nothing special to NHibernate but does hold true for any object-relational mapper.
The object identity problem deals with the object-relational mismatch and the fact that the equivalence relationship between objects can be defined in different ways.
By default, equivalence between object is defined using the object pointer in memory. This means that two objects loaded using the same PK in two different sessions would be considered different, which is a bit counter intuitive.
So it seems natural to override the equivalence relation to use the PK, in a way to have two such two objects be considered equivalent. But then how do you deal with objects that haven't been saved yet and have not PK assigned already? Now you probably start to see the kinds of problems we bump into...
For more details, you can have a look at what are best practices to implement equality, where I listed various strategies and gotchas related to this problem.
精彩评论