一尘不染

使用Hibernate,休眠注释和ehcache的Maven依赖项是什么?

hibernate

我想用最新的hibernate,hibernate注释和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>
    

  2. 此警告有很多实例:

    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

  3. 另外,此错误使所有操作失败:

相关原因:

    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)

确认一下,这是弹簧配置:

<!-- 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)

阅读 211

收藏
2020-06-20

共1个答案

一尘不染

如果您真的是说hibernate-annotations(而不是hibernate-
entitymanager)和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 Annotations合并回了Core。

我的意思是说Hibernate EntityManager,然后在上面的代码段中替换hibernate-corehibernate- entitymanager

关于EHCache,ehcache-core应提供您所需的一切(包括“新的” Hibernate 3.3 /
3.5
及其SPI缓存SPI的实现CacheRegionFactory)。因此,如记录所示

将Ehcache设置为hibernate二级缓存

Hibernate 3.3及更高版本

注意海纳特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

强制Hibernate使用单身的Ehcache CacheManager。


后续行动: 要回答您的更新:

sl4j引发错误,通过添加(…)消除了错误

好吧,我无法猜测您使用的是什么日志记录框架,只要您将选择的绑定留给读者练习即可:)

此警告有很多情况(…)

WARNING
在非JTA环境中运行时,我只有一个。如果是这种情况(非JTA环境),我认为这WARNING是正常的。如果不是您这种情况,请查看有关JTA的文档。

此外,此错误会使一切失败

这是一个不同的问题(但似乎SessionFactory无法正确初始化,激活日志记录以查看原因),我建议发布一个新的Spring特定问题。

2020-06-20