简体   繁体   English

Hibernate L2 缓存用于多对一关联

[英]Hibernate L2 Caching for many-to-one associations

I am somewhat new to hibernate and am attempting to implement second level caching using ehCache.我对 hibernate 有点陌生,我正在尝试使用 ehCache 实现二级缓存。 I am running into a problem when attempting to use the L2 cache to retrieve a many-to-one association.尝试使用 L2 缓存检索多对一关联时遇到问题。 The association mapping in my foo.hbm.xml file looks like:我的 foo.hbm.xml 文件中的关联映射如下所示:

<hibernate-mapping>
    <class name="com.test.Foo" table="FOO" >
        <id name="id" type="long">
            <column name="FOO_ID" precision="11" scale="0" />
            <generator class="assigned" />
        </id>

        <many-to-one name="bar" class="com.test.Bar" >
            <cache usage="read-only" />
            <column name="BAR_TY" not-null="true" />
            <column name="BAR_VAL" length="4" not-null="true" />
        </many-to-one>

    </class>
</hibernate-mapping>

(This is a very shortened/modified version of my actual hbm.xml file, hopefully no errors in it) (这是我的实际 hbm.xml 文件的一个非常缩短/修改的版本,希望其中没有错误)

Note that I am only caching "bar", not "foo".请注意,我只缓存“bar”,而不是“foo”。 However when I run a unit test with this mapping I get the following error:但是,当我使用此映射运行单元测试时,出现以下错误:

SEVERE: Error parsing XML: XML InputStream(40) The content of element type "many-to-one" must match "(meta*,(column|formula)*)".严重:解析 XML 时出错:XML InputStream(40) 元素类型“多对一”的内容必须匹配“(meta*,(column|formula)*)”。

When I take out it works fine, just no L2 caching.当我取出它时,它工作正常,只是没有 L2 缓存。 Is it simply that the many-to-one relationship doesn't support caching?仅仅是多对一的关系不支持缓存吗? And if so, any suggestions on how to get around this?如果是这样,关于如何解决这个问题的任何建议?

FYI I have another unit test that reads "bar" directly (ie not through an association) and it works correctly - the 2nd time I get a "bar" it gets it from the L2 cache, so I'm fairly confident I have the rest of the configuration done correctly.仅供参考我有另一个单元测试直接读取“bar”(即不通过关联)并且它工作正常 - 我第二次获得“bar”它从 L2 缓存中获取它,所以我相当有信心我有rest 的配置正确完成。 And so sorry, still in the stone ages at work so no annotations etc.很抱歉,仍然在石器时代工作,所以没有注释等。

Your XML is malformed.您的 XML 格式不正确。 It is illegal node <cache> inside <many-to-one> .它是<many-to-one>内的非法节点<cache> > 。
See hibernate-mapping-3.0.dtd .请参阅hibernate-mapping-3.0.dtd Declaration on many-to-one element:关于many-to-one元素的声明:

<!ELEMENT many-to-one (meta*,(column|formula)*)>
    <!ATTLIST many-to-one name CDATA #REQUIRED>
    <!ATTLIST many-to-one access CDATA #IMPLIED>
    <!ATTLIST many-to-one class CDATA #IMPLIED>
    <!ATTLIST many-to-one entity-name CDATA #IMPLIED>
    <!ATTLIST many-to-one column CDATA #IMPLIED>
    <!ATTLIST many-to-one not-null (true|false) #IMPLIED>
    <!ATTLIST many-to-one unique (true|false) "false">
    <!ATTLIST many-to-one unique-key CDATA #IMPLIED>
    <!ATTLIST many-to-one index CDATA #IMPLIED>
    <!ATTLIST many-to-one cascade CDATA #IMPLIED>
    <!ATTLIST many-to-one outer-join (true|false|auto) #IMPLIED>
    <!ATTLIST many-to-one fetch (join|select) #IMPLIED>
    <!ATTLIST many-to-one update (true|false) "true">
    <!ATTLIST many-to-one insert (true|false) "true">
    <!ATTLIST many-to-one optimistic-lock (true|false) "true">  
    <!ATTLIST many-to-one foreign-key CDATA #IMPLIED>
    <!ATTLIST many-to-one property-ref CDATA #IMPLIED>
    <!ATTLIST many-to-one formula CDATA #IMPLIED>
    <!ATTLIST many-to-one lazy (false|proxy|no-proxy) #IMPLIED>
    <!ATTLIST many-to-one not-found (exception|ignore) "exception">
    <!ATTLIST many-to-one node CDATA #IMPLIED>
    <!ATTLIST many-to-one embed-xml (true|false) "true">  

Cache element can be placed on class level:缓存元素可以放在 class 级别:

<hibernate-mapping>
    <class name="com.test.Foo" table="FOO" >
        <cache usage="read-only" />
        <id name="id" type="long">

or for collections like set , map , bag etc.或用于 collections 等setmapbag等。

Have you tried moving definition of bar into another, separate .hbm.xml and only referencing it from foo ?您是否尝试过将bar的定义移动到另一个单独的.hbm.xml并且仅从foo引用它?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM