NHibernate 根据父类 ID 缓存对象
我对动物和狗类型有以下定义。请注意,对象的 ID 是 AnimalID:
<class name="Animal" table="Animals">
<id name="Id" type="System.Int32" column="AnimalID">
<generator class="identity" />
</id>
<property name="IsBig" column="IsBig" type="System.Bool" not-null="true" />
</class>
<joined-subclass name="Dog" table="Dogs" extends="Animal">
<key column="AnimalID" />
<property name="OwnerID" column="OwnerID" type="System.Int32" non-null="true" />
<property name="IsStrong" column="IsStrong" type="System.Bool" non-null="true" />
</joined-subclass>
假设我的数据库中有以下信息:
in table Animals:
AnimalID IsBig
-------- -----
10 True
in table Dogs:
AnimalID OwnerID IsStrong
-------- ------- --------
10 1 True
10 2 False
首先,我查询 OwnerID = 1 的 Dog。在同一个会话中,我查询 OwnerID = 2 的 Dog。因为在 NHibernate 的 Session 缓存中,第二个查询返回一个 Dog 对象,其中 OwnerID = 1 且 IsStrong = True,它应该返回一个 Dog 对象,其中 OwnerID = 2 且 IsStrong = False。
NHibernate 按 ID(主键)列自动缓存对象,因此第二次请求 Dog 最终会检索具有相同键的对象。我可以通过在对象上调用 ISession.Evict() 来解决这个问题,但这似乎是一种黑客行为。
还有更好的建议吗?
I have the following definitions for the Animal and Dog types. Note that the ID for the object is the AnimalID:
<class name="Animal" table="Animals">
<id name="Id" type="System.Int32" column="AnimalID">
<generator class="identity" />
</id>
<property name="IsBig" column="IsBig" type="System.Bool" not-null="true" />
</class>
<joined-subclass name="Dog" table="Dogs" extends="Animal">
<key column="AnimalID" />
<property name="OwnerID" column="OwnerID" type="System.Int32" non-null="true" />
<property name="IsStrong" column="IsStrong" type="System.Bool" non-null="true" />
</joined-subclass>
Let's say I have the following information in my database:
in table Animals:
AnimalID IsBig
-------- -----
10 True
in table Dogs:
AnimalID OwnerID IsStrong
-------- ------- --------
10 1 True
10 2 False
First, I query for the Dog where OwnerID = 1. In the same session, I query for the Dog where OwnerID = 2. Because of NHibernate's Session cache, the second query returns a Dog object where OwnerID = 1 and IsStrong = True, where it should return a Dog object where OwnerID = 2 and IsStrong = False.
NHibernate automatically caches objects by their ID (primary key) column, so requesting the Dog the second time ends up retrieving an object with the same key. I can solve this problem by calling ISession.Evict() on the object, but that seems like a hack.
Any better suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须确保对不同的实例使用不同的密钥。在您的情况下,您实际上违反了此规则:Dogs 表公开了两个实例的部分共享相同的密钥。
因此 Dogs.AnimalID 必须标记为主键,但在您的情况下并非如此。如果被标记为PK的话,你根本就得不到这样的内容。
You must ensure you're using different keys for different instances. In your case you're actually violating this rule: Dogs table exposes parts of two instances sharing the same key.
So Dogs.AnimalID must be marked as primary key, but in your case it isn't. If it would be marked as PK, you couldn't get such content at all.
恕我直言,您应该问的问题是“如何正确建模?”
您的 Dogs 表显示狗 10 属于所有者 1,并且很强大,但是同时,狗 10 属于所有者 2,并且并不强大。
不知怎的,我不认为这就是你的意思。
如果您能更详细地解释您想要建模的内容,也许我们可以提出一些建议。
With respect, the question you should be asking is "how do I model this correctly?"
Your Dogs table says that dog 10 is owned by owner 1 and is strong, but, at the same time, dog 10 is owned by owner 2 and is NOT strong.
Somehow, I don't think that's what you meant.
If you'll explain in more detail what you're trying to model, perhaps we can make some suggestions.
将 AnimalID 和 OwnerID 设为复合主键。
Make AnimalID and OwnerID a composite primary key.