【问题标题】:HIbernate 3.5.1 - can I just drop in EHCache 2.0.1?HIbernate 3.5.1 - 我可以加入 EHCache 2.0.1 吗?
【发布时间】:2010-04-24 09:30:10
【问题描述】:

我使用的是捆绑了 EHCache 1.5 的 Hibernate 3.5.1。

如果我想使用最新的 EHCache 版本(2.0.1),是否只是从我的项目中删除 ehcache-1.5.jar 并替换为 ehcache-core-2.0.1.jar 的问题?有什么需要注意的问题吗?

另外 - Hibernate 映射文件中的缓存“区域”是否与 ehcache 配置 xml 中的缓存“名称”相同?我想要做的是定义 2 个命名缓存区域 - 一个用于不会更改的只读引用实体(查找列表等),另一个用于所有其他实体。所以在ehcache中我想定义两个元素;

<cache name="readonly"> ... </cache>
<cache name="mutable"> ... </cache>

然后在我的 Hibernate 映射文件中,我将指定要用于每个实体的缓存:

<hibernate-mapping>
    <class name="lookuplist">
        <cache region="readonly" usage="read-only"/>
        <property> ... </property>
    </class>
</hibernate-mapping>

这行得通吗?一些文档似乎暗示为每个映射类创建一个单独的区域/缓存......

谢谢。

【问题讨论】:

    标签: java hibernate orm ehcache


    【解决方案1】:

    如果我想使用最新的 EHCache 版本(2.0.1),是否只是从我的项目中删除 ehcache-1.5.jar 并替换为 ehcache-core-2.0.1.jar 的问题?有什么需要注意的问题吗?

    根据关于将 Ehcache 用作 Hibernate Second Level Cache 的 Ecache 文档,您确实必须使用 ehache-core.jar,但还要更改 Hibernate's configuration

    另外 - Hibernate 映射文件中的缓存“区域”是否与 ehcache 配置 xml 中的缓存“名称”相同?

    是的。同样,请参阅文档,这在 Configuring ehcache.xml 中进行了解释。

    这行得通吗?一些文档似乎暗示为每个映射类创建一个单独的区域/缓存

    文档没有暗示,Cache mappings 中的黑底白字是默认设置:

    region (optional: defaults to the class or collection role name): specifies the 
    name of the second level cache region 
    

    它会起作用吗?从技术上讲,是的。这是个好主意吗?我不知道。 IMO 最好在 Hibernate 和 Ehcache 级别都有更细粒度的区域(特别是如果您计划使用分布式缓存和失效策略,您当然不想使所有实体失效)。我会使用 Hibernate 的默认值。

    【讨论】:

      【解决方案2】:

      我有同样的问题, 如果您使用的是 Maven。

      最好防止hibernate自己下载ehcache,并提供你的ehcache入口。

              <dependency>
              <groupId>org.hibernate</groupId>
              <artifactId>hibernate</artifactId>
              <version>3.2.6.ga</version>
              <exclusions>
                  <exclusion>
                      <groupId>net.sf.ehcache</groupId>
                      <artifactId>ehcache</artifactId>
                  </exclusion>
              </exclusions>
          </dependency>
      

      这样它就不会下载内部 ehcache 在我的情况下它是版本 1.2.3

      然后放

              <dependency>
              <groupId>net.sf</groupId>
              <artifactId>ehcache-core</artifactId>
              <version>2.4.2</version>
          </dependency>
      

      在您的 POM.xml 中

      它应该可以工作。

      【讨论】:

        猜你喜欢
        • 2011-03-11
        • 2018-05-03
        • 1970-01-01
        • 2013-05-03
        • 2012-01-03
        • 2011-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多