Hibernate L2 Caching for many-to-one associations
I am somewhat new to hibernate and am attempting to implement second level caching using ehCache. I am running into a problem when attempting to use the L2 cache to retrieve a many-to-one association. The association mapping in my foo.hbm.xml file looks like:
<hibernate-mapping>
<class name="com.test.Foo" table="FOO" >
<id name="id" type="long">
<column name="FOO_ID" precisio开发者_StackOverflown="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)
Note that I am only caching "bar", not "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)*)".
When I take out it works fine, just no L2 caching. 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 (i.e. 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. And so sorry, still in the stone ages at work so no annotations etc.
Have you tried moving definition of bar
into another, separate .hbm.xml
and only referencing it from foo
?
Your XML is malformed. It is illegal node <cache>
inside <many-to-one>
.
See hibernate-mapping-3.0.dtd. Declaration on many-to-one
element:
<!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:
<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.
精彩评论