使用 nHibernate 二级缓存进行多对一引用

发布于 2025-01-04 06:56:34 字数 1621 浏览 1 评论 0原文

我试图让 nHibernate 使用具有多对一关系的二级缓存,但是我找不到任何关于如何正确设置它的明确解释。我发现这个 如何让 nhibernate 缓存通过多对一引用的表 - 我的配置正确吗?,但是 sJHonny 提供的示例适用于一对多,它对我不起作用当我采用它时。还有其他帖子讨论了这个主题,但没有一个足够具体。

我提供的 XML 配置有效(我必须进行大量编辑,因此“希望”有效),但查找对象仅在查询 DataObject 时检索时才被缓存。我想知道 1) 在哪里/如何预加载 LookupObject 集合? 2)如果我将此集合分配给一个区域并设置过期时间,那么我在哪里/如何再次重新加载缓存? 3)如何更改DataObject的hbm.xml,以便nHibernate不会生成与LOOKUP表的连接,即查找对象始终来自二级缓存,只从数据库获取DATA.LOOKUP_ID,而不是LOOKUP。姓名?

LookupObject.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="BusinessObjects" assembly="BusinessObjects">
  <class name="LookupObject" table="LOOKUP" mutable="false" batch-size="20">
    <cache usage="read-only" />   
    <id name="Id" column="ID" />
    <property name="Name" column="NAME" />
  </class>
</hibernate-mapping>

DataObject.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="BusinessObjects" assembly="BusinessObjects">
  <class name="DataObject" 
         table="DATA" mutable="false">
    <composite-id>
      <key-property name="Id" column="ID"/>
      <key-property name="Date" column="DATE" type="Date"/>
    </composite-id>
    <many-to-one name="LookupObject" not-null="true" column="LOOKUP_ID" fetch="join">
  </class>
</hibernate-mapping>

I'm trying to get nHibernate to use second-level cache with a many-to-one relationship, however I can't find any clear explanation on how to set it up correctly. I found this How to get nhibernate to cache tables referenced via many-to-one - is my config correct?, but the example sJHonny provided is for one-to-many and it's not working for me when I adopt it. There are other posts going over this subject, but none of them are specific enough.

The XML config I provide works (I had to edit dramatically, so "hopefully" works), but lookup objects are being cached only when they are retrieved as DataObject is queried. I'd like to know 1) where/how to preload the LookupObject collection? 2) what if I assign this collection to a region and set expiration, then where/how do I reload the cache again? 3) how to change the DataObject's hbm.xml such that nHibernate doesn't generate a join with the LOOKUP table, i.e. such that lookup objects always come from the secondary cache, only getting the DATA.LOOKUP_ID from the db, not the LOOKUP.NAME?

LookupObject.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="BusinessObjects" assembly="BusinessObjects">
  <class name="LookupObject" table="LOOKUP" mutable="false" batch-size="20">
    <cache usage="read-only" />   
    <id name="Id" column="ID" />
    <property name="Name" column="NAME" />
  </class>
</hibernate-mapping>

DataObject.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="BusinessObjects" assembly="BusinessObjects">
  <class name="DataObject" 
         table="DATA" mutable="false">
    <composite-id>
      <key-property name="Id" column="ID"/>
      <key-property name="Date" column="DATE" type="Date"/>
    </composite-id>
    <many-to-one name="LookupObject" not-null="true" column="LOOKUP_ID" fetch="join">
  </class>
</hibernate-mapping>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

‖放下 2025-01-11 06:56:34

我相信我回答了我自己的问题。 1) 为了预加载整个集合,我只需要更改代码,以便始终从数据库中提取整个查找列表并进行缓存,然后使用 LINQ 通过 ID 获取单个对象。 2)和以前一样。 3)我还没有对此进行测试,但我需要从多对一元素中删除 fetch="join",因为否则仍会生成 SQL 连接并且仍会返回数据。然后通过从缓存中获取查找对象来设置不使用 nHibernate 的查找对象。

I believe I answered my own questions. 1) For preloading the entire collection, I just needed to change my code such that the entire list of lookups is always pulled from the DB and cached, then I get the individual object by ID with LINQ. 2) Same as before. 3) I haven't tested this yet, but I need to remove fetch="join" from many-to-one element because otherwise the SQL join will still be generated and data will be still returned. Then set the lookup object without nHibernate by getting it from cache.

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