使用Hibernate、Hibernate注释和Ehcache需要哪些Maven依赖项?

18

我想用最新的hibernate、hibernate-annotations和ehcache依赖更新我的Maven pom.xml文件。

我阅读了hibernate下载页面和ehcache下载页面。我尝试理解它们,但都失败了。有人能写出应该放入pom.xml文件中的片段吗?

神秘的hibernate下载页面: http://hibernate.org/downloads

神秘的ehcache下载页面: http://www.terracotta.org/dl/ehcache-oss-download-catalog

以下是我目前拥有的:

   <properties>
       <hibernate.version>3.6.0.Final</hibernate.version>
   </properties>
   <dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
        <exclusions>
            <exclusion>
                <groupId>javax.transaction</groupId>
                <artifactId>jta</artifactId>
            </exclusion>
            <exclusion>
                <groupId>net.sf.ehcache</groupId>
                <artifactId>ehcache</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-ehcache</artifactId>
        <version>${hibernate.version}</version>
        <exclusions>
            <exclusion>
                <groupId>net.sf.ehcache</groupId>
                <artifactId>ehcache</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache</artifactId>
        <version>2.2.0</version>
        <type>pom</type>
    </dependency>
 </dependencies>

    <repositories>
        <repository>
            <id>codehaus-repository</id>
            <name>Codehaus</name>
            <url>https://nexus.codehaus.org/content/repositories/releases/</url>
            <layout>default</layout>
        </repository>
        <repository>
            <id>java.net</id>
            <url>http://download.java.net/maven/1</url>
            <layout>legacy</layout>
        </repository>
        <repository>
            <id>central</id>
            <name>Maven Repository Switchboard</name>
            <layout>default</layout>
            <url>http://repo1.maven.org/maven2</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>sonatype-ehcache</id>
            <url>http://oss.sonatype.org/content/repositories/sourceforge-releases</url>
            <layout>default</layout>
        </repository>
        <repository>
            <id>JBoss Repository</id>
            <url>https://repository.jboss.org/nexus/content/groups/public</url>
            <layout>default</layout>
        </repository>
    </repositories>

我知道,这看起来像一团乱麻,因为我似乎无法确定所有依赖项的存储位置。

上述内容失败的原因是:

Invocation of init method failed; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)

更新: Pascal,你的回复非常清晰并且解释得很好。但是,Hibernate仍然在启动时失败。

  1. sl4j报错,通过添加以下内容解决:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.6.1</version>
</dependency>
  • 有许多这种警告的实例:

  • g in the two-phase commit!
    1797 [main] WARN  net.sf.ehcache.hibernate.AbstractEhcacheRegionFactory  - No Tr
    ansactionManagerLookup found in Hibernate config, XA Caches will be participatin
    
  • 此外,这个错误会导致一切失败:

    相关原因:

  • org.springframework.beans.factory.BeanCreationException: Error creating bean wit
    h name 'financialDAO' defined in ServletContext resource [/WEB-INF/config/applic
    ationContext-database.xml]: Cannot resolve reference to bean 'transactionManager
    ' while setting bean property 'transactionManager'; nested exception is org.spri
    ngframework.beans.factory.BeanCreationException: Error creating bean with name '
    transactionManager' defined in ServletContext resource [/WEB-INF/config/applicat
    ionContext-database.xml]: Cannot resolve reference to bean 'sessionFactory' whil
    e setting bean property 'sessionFactory'; nested exception is org.springframewor
    k.beans.factory.BeanCreationException: Error creating bean with name 'sessionFac
    tory' defined in ServletContext resource [/WEB-INF/config/applicationContext-dat
    abase.xml]: Invocation of init method failed; nested exception is java.lang.Null
    PointerException
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver
    .resolveReference(BeanDefinitionValueResolver.java:328)
    
    确认一下,这是 Spring 的配置:

    <!-- Enable EHCache -->
    <prop key="hibernate.cache.use_second_level_cache">true</prop>
    <prop key="hibernate.cache.use_query_cache">false</prop>
    <prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</prop>
    <prop key="net.sf.ehcache.configurationResourceName">/ehcache.xml</prop>
    

    更新: 这似乎是Hibernate陷入困境的最新异常:

    Caused by: java.lang.NullPointerException
        at net.sf.ehcache.hibernate.HibernateUtil.loadAndCorrectConfiguration(HibernateUtil.java:48)
        at net.sf.ehcache.hibernate.EhCacheRegionFactory.start(EhCacheRegionFactory.java:79)
        at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:236)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1842)
        at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:855)
        at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:774)
        at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1469)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1409)
    

    暂时尝试不使用您的ehcache.xml文件(即从spring配置中删除它)。 - Pascal Thivent
    当我在hibernateproperties中删除所有的ehcache条目时,应用程序可以正常启动。但是,当我仅删除configurationResourceName条目时,它会显示正在使用故障转移配置。 - Thomas
    3个回答

    22

    如果你真的需要的是 Hibernate 注解的最终版本(而不是 Hibernate 实体管理器)和 Ehcache,则需要以下内容:

    <project>
      ...
      <dependencies>
        <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-core</artifactId>
          <version>3.6.0.Final</version>
        </dependency>
        <dependency>
           <groupId>net.sf.ehcache</groupId>
           <artifactId>ehcache-core</artifactId>
           <version>2.2.0</version>
       </dependency>
       ...
      </dependencies>
    
      <repositories>
        <repository>
          <id>repository.jboss.org-public</id>
          <name>JBoss repository</name>
          <url>https://repository.jboss.org/nexus/content/groups/public</url>
        </repository>
      </repositories>
      ...
    </project>
    
    自 Hibernate 3.6 起,不再支持 JDK 1.4,并且已将 Hibernate 注释合并回 Core 中。如果您指的是 Hibernate EntityManager,则在上面的代码片段中将 hibernate-core 替换为 hibernate-entitymanager

    关于 EHCache,ehcache-core 应该提供你所需的一切(包括实现“新”Hibernate 3.3/3.5 缓存 SPI和它的 CacheRegionFactory)。如文档所述:

    将 Ehcache 设置为 Hibernate 二级缓存

    Hibernate 3.3 及更高版本

    注意 Hibernate 3.2 用户

    请注意属性名和值的更改。

    使用:

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

    例如创建实例,或者

    <property name="hibernate.cache.region.factory_class">
              net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory</property>
    

    强制Hibernate使用Ehcache CacheManager的单例。

    跟进:回答您的更新:

    我无法猜测您正在使用哪个日志框架,因为选择绑定留给了读者自己决定 :)

    当在非JTA环境中运行时,我只会收到一个WARNING。如果您也是在非JTA环境下运行,则可以将WARNING视为正常现象。如果不是这种情况,请查看有关JTA的文档。

    此外,这个错误导致了一切都失败了。

    这其实是一个不同的问题(但看起来像是SessionFactory未能正确初始化,启用日志记录以查看原因),我建议发表一个新的Spring特定问题。


    评论框中的格式很糟糕,将在上面更新问题。 - Thomas
    Pascal,再次感谢您的帮助。您似乎非常聪明。也许最好为配置问题开始一个新的问题。 - Thomas

    4
    如果您要将Spring 3与Hibernate 3集成,那么您可以按照这个pom.xml进行操作。
    <properties>
        <org.springframework.version>3.0.5.RELEASE</org.springframework.version>
    </properties>
    
    <dependencies>
        <!-- Core utilities used by other modules. Define this if you use Spring 
            Utility APIs (org.springframework.core.*/org.springframework.util.*) -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
    
        <!-- Expression Language (depends on spring-core) Define this if you use 
            Spring Expression APIs (org.springframework.expression.*) -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
    
        <!-- Bean Factory and JavaBeans utilities (depends on spring-core) Define 
            this if you use Spring Bean APIs (org.springframework.beans.*) -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
    
        <!-- Aspect Oriented Programming (AOP) Framework (depends on spring-core, 
            spring-beans) Define this if you use Spring AOP APIs (org.springframework.aop.*) -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
    
        <!-- Application Context (depends on spring-core, spring-expression, spring-aop, 
            spring-beans) This is the central artifact for Spring's Dependency Injection 
            Container and is generally always defined -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
    
        <!-- Various Application Context utilities, including EhCache, JavaMail, 
            Quartz, and Freemarker integration Define this if you need any of these integrations -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
    
        <!-- Transaction Management Abstraction (depends on spring-core, spring-beans, 
            spring-aop, spring-context) Define this if you use Spring Transactions or 
            DAO Exception Hierarchy (org.springframework.transaction.*/org.springframework.dao.*) -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
    
        <!-- JDBC Data Access Library (depends on spring-core, spring-beans, spring-context, 
            spring-tx) Define this if you use Spring's JdbcTemplate API (org.springframework.jdbc.*) -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
    
        <!-- Object-to-Relation-Mapping (ORM) integration with Hibernate, JPA, 
            and iBatis. (depends on spring-core, spring-beans, spring-context, spring-tx) 
            Define this if you need ORM (org.springframework.orm.*) -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
    
        <!-- Object-to-XML Mapping (OXM) abstraction and integration with JAXB, 
            JiBX, Castor, XStream, and XML Beans. (depends on spring-core, spring-beans, 
            spring-context) Define this if you need OXM (org.springframework.oxm.*) -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
    
        <!-- Web application development utilities applicable to both Servlet and 
            Portlet Environments (depends on spring-core, spring-beans, spring-context) 
            Define this if you use Spring MVC, or wish to use Struts, JSF, or another 
            web framework with Spring (org.springframework.web.*) -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
    
        <!-- Spring MVC for Servlet Environments (depends on spring-core, spring-beans, 
            spring-context, spring-web) Define this if you use Spring MVC with a Servlet 
            Container such as Apache Tomcat (org.springframework.web.servlet.*) -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
    
        <!-- Spring MVC for Portlet Environments (depends on spring-core, spring-beans, 
            spring-context, spring-web) Define this if you use Spring MVC with a Portlet 
            Container (org.springframework.web.portlet.*) -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc-portlet</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
    
        <!-- Support for testing Spring applications with tools such as JUnit and 
            TestNG This artifact is generally always defined with a 'test' scope for 
            the integration testing framework and unit testing stubs -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${org.springframework.version}</version>
            <scope>test</scope>
        </dependency>
    
    
        <!-- Hibernate resources -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>3.6.7.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.3.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>3.3.0.ga</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.3.1.GA</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.3.2.GA</version>
        </dependency>
        <!-- EhCache -->
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache-core</artifactId>
            <version>2.2.0</version>
        </dependency>
        <dependency>
            <groupId>antlr</groupId>
            <artifactId>antlr</artifactId>
            <version>2.7.7</version>
        </dependency>
    
    
        <!-- Taglibs -->
    
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.1.2</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>20030825.184428</version>
        </dependency>
        <dependency>
            <groupId>commons-pool</groupId>
            <artifactId>commons-pool</artifactId>
            <version>20030825.183949</version>
        </dependency>
    
        <!-- Log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.14</version>
            <type>jar</type>
            <exclusions>
                <exclusion>
                    <groupId>javax.mail</groupId>
                    <artifactId>mail</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.5.6</version>
        </dependency>
    
    
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.4</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    

    如果你想要不同版本的hibernate,可以在这里查看。


    0
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>5.2.2.Final</version>
    </dependency>
    

    请添加更多信息。不鼓励仅包含代码和“试试这个”答案,因为它们没有可搜索的内容,并且没有解释为什么有人应该“试试这个”。 - abarisone

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接