java 正确版本的 Spring 和 Hibernate 以及所需的依赖项...由于依赖项而导致的异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5326051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Correct version of Spring and Hibernate and required dependencies...Exception due to dependencies
提问by Anupam Gupta
Since three days i am trying to get my Spring-Hibernate programs run. i had really tough time finding the involved dependencies due to version difference between hibernate2 and hibernate3. Finally i was able to run program with following set of dependencies
从三天开始,我就试图让我的 Spring-Hibernate 程序运行。由于 hibernate2 和 hibernate3 之间的版本差异,我很难找到相关的依赖项。最后我能够运行具有以下依赖项集的程序
- cglib-nodep-2.1_3.jar
- commons-collections.jar
- commons-dbcp.jar
- commons-pool.jar
- commons-logging.jar
- dom4j-1.4.jar
- ehcache-1.6.0-beta1.jar
- hibernate-3.1.3.jar
- hsqldb.jar
- jta.jar log4j-1.2.9.jar
- mysql-connector-java-5.0.8-bin.jar
- org.springframework.orm-3.1.0.M1.jar
- org.springframework.transaction-3.1.0.M1.jar
- spring-2.5.6.jar
- spring-beans-2.0.4.jar
- cglib-nodep-2.1_3.jar
- commons-collections.jar
- commons-dbcp.jar
- 公共池.jar
- commons-logging.jar
- dom4j-1.4.jar
- ehcache-1.6.0-beta1.jar
- hibernate-3.1.3.jar
- 数据库文件
- jta.jar log4j-1.2.9.jar
- mysql-connector-java-5.0.8-bin.jar
- org.springframework.orm-3.1.0.M1.jar
- org.springframework.transaction-3.1.0.M1.jar
- spring-2.5.6.jar
- spring-beans-2.0.4.jar
now after two days of efforts when i was able to manage the above mentioned dependencies i tried building similar program but it is throwing following error.I tried online for solution but the solution which i found there is not correct version of spring and hibernate ...Can any one tell me correct cause of exception and also correct version of the Spring and hibernate
现在经过两天的努力,当我能够管理上述依赖项时,我尝试构建类似的程序,但它抛出以下错误。 .谁能告诉我异常的正确原因以及Spring和hibernate的正确版本
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myRecordDAO' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'hibernateTemplate' while setting bean property 'hibernateTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' defined in class path resource [applicationContext.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 class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.reflect.MalformedParameterizedTypeException
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:275)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1245)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
I am also adding my application context.xml
我还添加了我的应用程序 context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/subhash"/>
<property name="username" value="root"/>
<property name="password" value=""></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"><ref local="dataSource"/></property>
<property name="mappingResources">
<list>
<value>MyRecord.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
<bean id="myRecordDAO" class="com.shoesobjects.MyRecordDAOHibernateWithSpring">
<property name="hibernateTemplate"><ref local="hibernateTemplate"/></property>
</bean>
</beans>
采纳答案by Sean Patrick Floyd
a)
一个)
Note
As of Spring 3.0, Spring requires Hibernate 3.2 or later.
注意
从 Spring 3.0 开始,Spring 需要Hibernate 3.2 或更高版本。
Source:
来源:
b)
b)
- org.springframework.orm-3.1.0.M1.jar
- org.springframework.transaction-3.1.0.M1.jar
- spring-2.5.6.jar
- spring-beans-2.0.4.jar
- org.springframework.orm- 3.1.0.M1.jar
- org.springframework.transaction- 3.1.0.M1.jar
- 弹簧- 2.5.6.jar
- spring-beans- 2.0.4.jar
Do you really think mixing current pre-release versions (3.1.x) with ancient versions (2.0.4 was released in 2007) is a good idea?
您真的认为将当前的预发布版本 (3.1.x) 与旧版本(2.0.4 于 2007 年发布)混合在一起是个好主意吗?
As matt says: use a dependency managements system like Maven, what you are dealing with is jar hell. Here's an article about referencing Spring 3.x artifacts from maven:
正如马特所说:使用像 Maven 这样的依赖管理系统,你正在处理的是 jar 地狱。这是一篇关于从 maven 引用 Spring 3.x 工件的文章:
回答by matt b
I suggest using a dependency management tool like Mavenor Apache Ivyso you don't have to handle sorting through the dependencies and required versions on your own.
我建议使用像Maven或Apache Ivy这样的依赖管理工具,这样你就不必自己处理依赖和所需版本的排序。
回答by Anupam Gupta
Got my problem resolved ...As pointed by Sean it was due to mixing different versions of spring and hibernate. Message for other newbies is please use latest version of spring and hibernate.As of Spring 3.0, Spring requires Hibernate 3.2 or later.one should never mix old and new version.
解决了我的问题......正如肖恩所指出的那样,这是由于混合了不同版本的 spring 和 hibernate。给其他新手的消息是请使用最新版本的spring和hibernate。从Spring 3.0开始,Spring需要Hibernate 3.2或更高版本。不要将新旧版本混用。
回答by Abhijeet Kashnia
Here's a simple file that worked for me, I used transaction manager and not template.
这是一个对我有用的简单文件,我使用了事务管理器而不是模板。
<bean id="mySqlDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/stackoverflow" />
<property name="user" value="root" />
<property name="password" value="******" />
<property name="maxPoolSize" value="50" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="mySqlDataSource" />
<property name="mappingResources">
<list>
<value>Post.hbm.xml</value>
<value>Tag.hbm.xml</value>
<value>User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
</value>
<!--
hibernate.connection.provider_class =
org.hibernate.connection.C3P0ConnectionProvider
hibernate.hbm2ddl.auto=update
hibernate.current_session_context_class=thread
-->
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
<!--
<property name="dataSource" ref="mySqlDataSource"/>
-->
</bean>
Also, I think the hibernate download package comes with separate folders that indicate what its specific dependencies are. As for the interrelation between hibernate and spring, I think you will have to use some dependency management tool as others have suggested. I do not think maven is a convenient tool. Just don't ever depart from maven's expected project structure and you should be fine.
另外,我认为 hibernate 下载包带有单独的文件夹,表明它的特定依赖项是什么。至于 hibernate 和 spring 之间的相互关系,我认为您将不得不像其他人建议的那样使用一些依赖项管理工具。我不认为 maven 是一个方便的工具。永远不要偏离 maven 预期的项目结构,你应该没问题。