【发布时间】:2016-01-11 06:42:25
【问题描述】:
在我的 spring 项目中,我使用 hibernate 来访问数据库..
我有
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
p:dataSource-ref="dataSource" p:packagesToScan-ref="packagesToScan"
p:hibernateProperties-ref="hibernateProperties" />
<util:map id="hibernateProperties">
<entry key="hibernate.show_sql" value="false" />
<!-- <entry key="hibernate.hbm2ddl.auto" value="update" /> -->
<entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
</util:map>
<util:list id="packagesToScan">
<value>com.preeti.myproject</value>
</util:list>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory" />
在 pom 文件中我有:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.11.Final</version>
</dependency>
但是当我运行该项目时,我遇到了错误:
创建名为“SignupController”的 bean 时出错:
Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field:
private com.preeti.myproject.service.UserService
com.preeti.myproject.controller.SignupController.userService
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'userService':
Injection of autowired dependencies failed;
Could not autowire field:
private org.hibernate.SessionFactory com.preeti.myproject.hibernate.AbstractHibernateDao.sessionFactory;
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'sessionFactory' defined in ServletContext
resource [/WEB-INF/spring-servlet.xml]: Invocation of init method failed;
nested exception is java.lang.ClassCastException:
org.hibernate.annotations.common.reflection.java.JavaReflectionManager
cannot be cast to org.hibernate.annotations.common.reflection.MetadataProviderInjector
我用的时候
hibernate 3.5.0-final,
hibernate-core-3.5.4-Final,
hibernate-entitymanager-3.5.4-Final,
hibernate-commons-annotations-3.3.0.ga
那个项目没有错误..
我必须更新以减少此错误..
【问题讨论】:
-
核心和实体管理器的 jar 需要匹配。确保两者的版本相同。
标签: spring hibernate spring-mvc