如何强制 Maven 将 Ehcache 2.2.0 与 Hibernate 3.3.2GA 一起使用?

Posted

技术标签:

【中文标题】如何强制 Maven 将 Ehcache 2.2.0 与 Hibernate 3.3.2GA 一起使用?【英文标题】:How can I force Maven to use Ehcache 2.2.0 with Hibernate 3.3.2GA? 【发布时间】:2011-04-03 10:31:46 【问题描述】:

假设两者兼容,如何强制 Maven 2 使用 Ehcache 2.2.0 而不是 Ehcache 1.2.3 和 Hibernate 3.3.2.GA?

本质上,我希望替换令人费解且实际循环的依赖链

Hibernate Ehcache Integration 3.3.2.GA => Ehcache 1.2.3 => Hibernate 3.2.0.cr3 => Ehcache 1.2

Hibernate Ehcache Integration 3.3.2.GA => Ehcache 2.2.0

更新:

我了解到hibernate-commons-annotations-3.3.0.ga也依赖于工件ehcache-1.2.3

[INFO] +- org.hibernate:hibernate-commons-annotations:jar:3.3.0.ga:compile
[INFO] |  +- org.hibernate:hibernate:jar:3.2.1.ga:compile
[INFO] |  |  +- net.sf.ehcache:ehcache:jar:1.2.3:compile
[INFO] |  |  +- asm:asm-attrs:jar:1.5.3:compile
[INFO] |  |  +- cglib:cglib:jar:2.1_3:compile
[INFO] |  |  \- asm:asm:jar:1.5.3:compile
[INFO] |  \- javax.persistence:persistence-api:jar:1.0:compile

hibernate-commons-annotations-3.3.0.ga 的目的是什么?如果 Hibernate 使用hibernate-annotations-3.2.1-ga,它是否需要这个工件?这个不包括 Ehcache 的工件有替代品吗?我应该尝试将其从构建中排除吗?

【问题讨论】:

似乎 hibernate-commons-annotations-3.3.0.ga.jar 依赖于此时将 ehcache 作为依赖项的整体 hibernate-3.2.1.ga.jar。我不使用这些依赖,我只依赖hibernate-entitymanager-3.4.0.GA.jar。我将展示我的 pom 和依赖树。 但是无论你使用什么依赖,你总是可以排除它。 【参考方案1】:

假设两者兼容,如何强制 Maven 2 将 Hibernate 3.3.2.GA 与 Ehcache 2.2.0 一起使用?根据各自的 Maven POM 文件:

我已经根据个人需求调查了这个问题,现在我有了具体的答案。所有需要的信息都可以在线获得,我只是发布一个非常简短的版本如何在 Hibernate 3.3+ 中使用 Ehcache 2.x

首先,您需要声明对 ehcache 工件的依赖关系。

<dependency>
  <groupId>net.sf.ehcache</groupId>
  <artifactId>ehcache</artifactId>
  <version>2.2.0</version>
  <type>pom</type>
</dependency>

然后,为二级缓存配置Hibernate,并指定二级缓存提供者:

<property key="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</property>

需要注意的重要事项:

我们正在为新的 Hibernate 3.3/3.5 SPI(Ehcache 2.0+ 支持)使用该属性 hibernate.cache.region.factory_class 我们正在使用 Echache 提供的缓存提供程序 net.sf.ehcache.hibernate.EhCacheRegionFactory(而不是o.h.c.EhCacheProvider

所以你实际上只是不需要 hibernate-ehcache 工件 - 这解决了整个问题 :) 以下是我使用的确切(相关)依赖项:

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-entitymanager</artifactId>
  <version>3.4.0.GA</version>
</dependency>
<dependency>
  <groupId>net.sf.ehcache</groupId>
  <artifactId>ehcache</artifactId>
  <version>2.2.0</version>
  <type>pom</type>
</dependency>

还有树:

[信息] +- org.hibernate:hibernate-entitymanager:jar:3.4.0.GA:compile [信息] | +- org.hibernate:ejb3-persistence:jar:1.0.2.GA:compile [信息] | +- org.hibernate:hibernate-commons-annotations:jar:3.1.0.GA:compile [信息] | +- org.hibernate:hibernate-annotations:jar:3.4.0.GA:compile [信息] | +- org.hibernate:hibernate-core:jar:3.3.0.SP1:compile [信息] | | +- antlr:antlr:jar:2.7.6:compile [信息] | | \- commons-collections:commons-collections:jar:3.1:compile [信息] | +- org.slf4j:slf4j-api:jar:1.5.10:compile [信息] | +- dom4j:dom4j:jar:1.6.1:编译 [信息] | | \- xml-apis:xml-apis:jar:1.0.b2:compile [信息] | +- javax.transaction:jta:jar:1.1:compile [信息] | \- javassist:javassist:jar:3.4.GA:compile [信息] +- ch.qos.logback:logback-classic:jar:0.9.18:compile [信息] | \- ch.qos.logback:logback-core:jar:0.9.18:compile [信息] \- net.sf.ehcache:ehcache:pom:2.2.0:compile [信息] +- net.sf.ehcache:ehcache-core:jar:2.2.0:compile [信息] +- net.sf.ehcache:ehcache-terracotta:jar:2.2.0:compile [信息] \- org.terracotta:terracotta-toolkit-1.0-runtime:jar:1.0.0:compile

更多详情,ehcache配置示例,官方文档,参考以下链接。

资源

Ehcache 2.0 supports new Hibernate 3.3 caching provider Configure Ehcache as a Second Level Cache Hibernate Second Level Cache Upgrading From Ehcache versions prior to 2.0

【讨论】:

感谢您的详细解答! 我明白了!试一试,让你知道我的结果。这是否也适用于 Hibernate 3.5+? 我重新阅读了您的回答并注意到您的第一个注释表明此解决方案也适用于 Hibernate 3.5+。 @Derek 是的,正如第一个注释中所述:) 帕斯卡,我发现工件 hibernate-commons-annotations-3.3.0.ga 也引入了 ehcache-1.2.3(请参阅我的问题中的更新)。【参考方案2】:

您可以使用“exclude”元素排除 Ehcache 1.2.3 依赖项,请参阅 http://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html依赖项排除部分。但我不知道它是否有效。我不知道 Ehcache 是否与 1.2.3 兼容。版本号表明它不是。但是通过“排除”,您可以轻松删除 Ehcache 1.2.3 和 1.5.0 的依赖关系。

【讨论】:

我使用元素 &lt;exclusions/&gt;ehcache-1.2.3hibernate-commons-annotations-3.3.0.ga 的依赖项列表中排除,因为我认为 Hibernate 注释需要这个工件。

以上是关于如何强制 Maven 将 Ehcache 2.2.0 与 Hibernate 3.3.2GA 一起使用?的主要内容,如果未能解决你的问题,请参考以下文章

使用 hibernate、hibernate 注释和 ehcache 的 Maven 依赖项是啥?

Ehcache基础知识学习

SpringMVC mybatis or hibernate ehcache二级缓存maven非和maven版本

企业框架源码 SpringMVC mybatis or hibernate ehcache二级缓存maven非和maven版本

企业框架源码 SpringMVC mybatis or hibernate ehcache二级缓存maven非和maven版本 websocket即时通讯

企业框架源码 SpringMVC mybatis or hibernate ehcache二级缓存maven非和maven版本websocket即时通讯