java Hibernate EhCache缓存关联/集合/集合的区域?
我是个新来的冬眠者。如果有人能对我提出的以下问题发表评论,那就太好了:
假设我有一个父类,每个父类都有多个子类。因此父类的映射文件类似于:
家长。哈佛商学院。xml
<hibernate-mapping >
<class name="org.demo.parent" table="parent" lazy="true">
<cache usage="read-write" region="org.demo.parent"/>
<id name="id" column="id" type="integer" length="10">
<generator class="native">
</generator>
</id>
<property name="name" column="name" type="string" length="50"/>
<set name="children" lazy="true">
<cache usage="read-write" region="org.demo.parent.children" />
<key column="parent_id"/>
<one-to-many class="org.demo.children"/>
</set>
</class>
</hibernate-mapping>
孩子们。哈佛商学院。xml
<hibernate-mapping >
<class name="org.demo.children" table="children" lazy="true">
<cache usage="read-write" region="org.demo.children"/>
<id name="id" column="id" type="integer" length="10">
<generator class="native">
</generator>
</id>
<property name="name" column="name" type="string" length="50"/>
<many-to-one name="parent_id" column="parent_id" type="integer" length="10" not-null="true"/>
</class>
</hibernate-mapping>
因此,对于集合子项,我们应该指定区域组织。演示。父母亲它应该缓存关联的子对象,或者我们应该使用org的缓存区域。演示。将缓存子对象的子对象
我使用EHCache作为二级缓存提供程序。我试图寻找这个问题的答案,但在这个方向上找不到任何答案。使用org更有意义。演示。孩子们,但我不知道在哪种情况下,应该像上面的例子那样为关联/集合/集合使用单独的缓存区域。请提供您的意见,如果我的问题不清楚,请告诉我
谢谢大家
# 1 楼答案
默认情况下,可选的^{} 设置为类或集合角色名称。因此,对于
set
,默认区域名称将是"org.demo.Parent.children"
(即不同于Child
的默认区域名称,即"org.demo.Child"
)。 这在我看来很有意义,因为您希望能够使某个特定集合或某个区域中的所有集合失效,而不会使集合类型中的所有实体失效但实际上,这里的大问题是:你为什么不使用默认值?您正在引入额外的工作、维护和潜在的问题源,而我无法理解这些好处。作为一个习惯多于配置的爱好者,我使用默认设置(即,我不设置
region
)