NHibernate 根据父类 ID 缓存对象

发布于 2024-08-13 07:31:44 字数 1231 浏览 16 评论 0原文

我对动物和狗类型有以下定义。请注意,对象的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

慕巷 2024-08-20 07:31:44

您必须确保对不同的实例使用不同的密钥。在您的情况下,您实际上违反了此规则: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.

煮酒 2024-08-20 07:31:44

恕我直言,您应该问的问题是“如何正确建模?”

您的 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.

一个人的旅程 2024-08-20 07:31:44

将 AnimalID 和 OwnerID 设为复合主键。

Make AnimalID and OwnerID a composite primary key.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文