本文介绍了使用hibernate,hibernate注释和ehcache的Maven依赖关系是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想使用最新的hibernate,hibernate-annotations和ehcache依赖来更新我的Maven pom.xml。 我阅读了hibernate的下载页面和ehcache donwload页面。我所有的尝试解释它似乎都失败了。有人可以写出应该进入pom.xml的片段吗? Cryptic hibernate下载页面: http://hibernate.org/downloads Cryptic ehcache下载页面: http://www.terracotta.org/dl/ehcache-oss-download-catalog 这是我到目前为止所有的: ;性状> < hibernate.version> 3.6.0.Final< / hibernate.version> < / properties> <依赖关系> <依赖关系> < groupId> org.hibernate< / groupId> < artifactId> hibernate-core< / artifactId> < version> $ {hibernate.version}< / version> <排除> < exclude> < groupId> javax.transaction< / groupId> < artifactId> jta< / artifactId> < / exclusion> < exclude> < groupId> net.sf.ehcache< / groupId> < artifactId> ehcache< / artifactId> < / exclusion> < / exclusions> < / dependency> <依赖关系> < groupId> org.hibernate< / groupId> < artifactId> hibernate-ehcache< / artifactId> < version> $ {hibernate.version}< / version> <排除> < exclude> < groupId> net.sf.ehcache< / groupId> < artifactId> ehcache< / artifactId> < / exclusion> < / exclusions> < / 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> 我知道,它看起来像一个大混乱,因为我似乎没有弄清楚,所有的依赖项都存储起来。 以上失败: 调用init方法失败;嵌套异常是java.lang.NullPointerException 在org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328) 更新:帕斯卡尔,你的回答很清楚,很好解释。但是,hibernate在启动时仍然失败。 sl4j抛出错误,后者添加: <依赖关系> < groupId> org.slf4j< / groupId> < artifactId> slf4j-log4j12< / artifactId> < version> 1.6.1< / version> < / dependency> 这个警告有很多例子: g在两阶段提交! 1797 [main] WARN net.sf.ehcache.hibernate.AbstractEhcacheRegionFactory - No Tr ansactionManagerLookup在Hibernate配置中找到,XA缓存将被参与 此外,此错误使一切失败: 相关原因: org.springframework.beans.factory.BeanCreationException:在ServletContext资源中定义的h名称'financialDAO'创建bean时出错[/ WEB-INF / config / applic ationContext-database.xml]:设置bean属性transactionManager时,无法解析引用beantransactionManager ;嵌套异常是org.spri ngframework.beans.factory.BeanCreationException:在ServletContext资源[/ WEB-INF / config / applicat ionContext-database中定义的名称为' transactionManager' .xml]:无法解析引用bean'sessionFactory'whil e设置bean属性'sessionFactory';嵌套异常是org.springframewor k.beans.factory.BeanCreationException:在ServletContext资源[/ WEB-INF / config / applicationContext-dat 中定义的名称为'sessionFac tory' abase.xml]:调用init方法失败;嵌套异常是java.lang.Null PointerException 在org.springframework.beans.factory.support.BeanDefinitionValueResolver .resolveReference(BeanDefinitionValueResolver.java:328) 要确认,这是弹簧配置: <! - 启用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被卡住的最新异常: p> 导致:java.lang.NullPointerException 在net.sf.ehcache.hibernate.HibernateUtil.loadAndCorrectConfiguration(HibernateUtil.java :48) at net.sf.ehcache.hibernate.EhCacheRegionFactory.start(EhCacheRegionFactory.java:79)在org.hibernate.impl.SessionFactoryImpl。< init>(SessionFactoryImpl.java:236) 在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1842)在org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:855)在org .springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:774)在org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)在org.springframework.beans .factory.support.Abstrac tAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1469)在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1409) 解决方案如果你真的意味着hibernate-annotations(而不是hibernate-entitymanager)和ehcache的终极版本,那么你将需要以下: < project> ... <依赖关系> <依赖关系> < groupId> org.hibernate< / groupId> < artifactId> hibernate-core< / artifactId> < version> 3.6.0.Final< / version> < / 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存储库< / 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 )。因此,如记录的: 后续行动:要回答您的更新: 嗯,我不能猜测你正在使用什么样的日志框架,提供你选择的绑定作为读者的一个练习:)我只得到一个 警告 在非JTA环境中运行。如果是这种情况(非JTA环境),我会考虑 WARNING 正常。如果不是这样,请查看有关 JTA 的文档。这是一个不同的问题(但它看起来像 SessionFactory 无法正确初始化,激活日志记录以了解原因),我建议发布一个新的春季特定问题。 I would like to update my Maven pom.xml with the latest hibernate, hibernate-annotations, and ehcache dependencies.I read the hibernate download page and the ehcache donwload page. All my tries at interpreting it seem to fail. Can someone write out the snippet that should go into pom.xml?Cryptic hibernate download page:http://hibernate.org/downloadsCryptic ehcache download page:http://www.terracotta.org/dl/ehcache-oss-download-catalogHere is what I have so far: <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>I know, it looks like a big mess, because I don't seem to figure out, where all the dependencies are stored.The above fails with:Invocation of init method failed; nested exception is java.lang.NullPointerException at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)Update:Pascal, your reply is very clear and well explained. However, hibernate still fails on startup.sl4j threw errors, which went away by adding:<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.1</version></dependency>There are many instances of this warning:g in the two-phase commit!1797 [main] WARN net.sf.ehcache.hibernate.AbstractEhcacheRegionFactory - No TransactionManagerLookup found in Hibernate config, XA Caches will be participatinAdditionally, this error makes everything fail:Related cause:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'financialDAO' defined in ServletContext resource [/WEB-INF/config/applicationContext-database.xml]: Cannot resolve reference to bean 'transactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in ServletContext resource [/WEB-INF/config/applicationContext-database.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/config/applicationContext-database.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)To confirm, this is the spring configuration:<!-- 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>Update:This seems to be the latest exception that hibernate gets stuck on: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) 解决方案 If you really mean the ultimate versions of hibernate-annotations (and not hibernate-entitymanager) and ehcache, then you'll need the following:<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>As of Hibernate 3.6, JDK 1.4 support has been dropped and the Hibernate Annotations have been merged back into Core.I you meant Hibernate EntityManager, then replace hibernate-core by hibernate-entitymanager in the above snippet.Regarding EHCache, ehcache-core should provide everything you need (including implementations of the "new" Hibernate 3.3/3.5 caching SPI with its CacheRegionFactory). So, as documented:Follow-up: To answer your update:Well, I can't guess what logging framework you're using, providing the binding of your choice was left as an exercise for the reader :)I get only one WARNING when running in a non JTA environment. If that's your case (a non JTA environment), I would consider the WARNING as normal. If that's not your case, have a look at the documentation about JTA.This is somehow a different question (but it looks like the SessionFactory fails to initialize properly, activate logging to see why) and I suggest to post a new spring specific question. 这篇关于使用hibernate,hibernate注释和ehcache的Maven依赖关系是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-19 14:15