spring3.1与hibernate4集成,applicationContext关于annotation的配置错误

yjpfj1203 发布于 2012/08/07 08:58
阅读 12K+
收藏 0

下面这个配置是用hibernate4以前的版本,没有问题

<!-- 配置Hibernate和数据库 -->
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="configurationClass">
   <value>org.hibernate.cfg.AnnotationConfiguration</value>
  </property>
  <property name="packagesToScan">
   <value>liuhan.test.domain</value>
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">${db.dialect}</prop>
    <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>

   </props>
  </property>
  <property name="namingStrategy">
   <ref bean="appNameStrategy" />
  </property>

但是在hibernate4的时候就不能用了,下面的我的写法,不知道哪儿有问题,请大师指点下

<!-- 配置Hibernate和数据库 -->
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
<!--  <property name="configuration">-->
<!--   <value>org.hibernate.cfg.Configuration</value>-->
<!--  </property>-->
  <property name="packagesToScan">
   <value>com.liuhan.myframework.domain</value>
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">${db.dialect}</prop>
    <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
    <prop key="hibernate.search.default.directory_provider">org.hibernate.search.store.FSDirectoryProvider</prop>
    <prop key="hibernate.search.default.indexBase">${indexBase}</prop>
   </props>
  </property>
  <property name="namingStrategy">
   <ref bean="appNameStrategy" />
  </property>
 </bean>

被我注释掉的部分是因为,这个类里有这个属性,但没有这个set方法,是类似

<property name="configurationClass">
   <value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>

这个写的,不知道对不对。

现在的提示是

严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/engine/SessionFactoryImplementor
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
 at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
 at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)
 at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)
 at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
 at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4723)
 at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5226)
 at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5221)
 at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
 at java.util.concurrent.FutureTask.run(FutureTask.java:138)
 at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
 at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NoClassDefFoundError: org/hibernate/engine/SessionFactoryImplementor
 at org.springframework.orm.hibernate3.SessionFactoryUtils.getDataSource(SessionFactoryUtils.java:123)
 at org.springframework.orm.hibernate3.HibernateTransactionManager.afterPropertiesSet(HibernateTransactionManager.java:411)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
 ... 20 more
Caused by: java.lang.ClassNotFoundException: org.hibernate.engine.SessionFactoryImplementor
 at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
 at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
 at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
 ... 24 more


 

加载中
0
无忌
无忌

Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/engine/SessionFactoryImplementor

看错误提示找不到类,是不是少了jar包或版本不匹配啊。

看这个答案是不是你想要的:

http://stackoverflow.com/questions/9908452/java-lang-classnotfoundexception-org-hibernate-engine-sessionfactoryimplementor

<bean id="transactionManager"  class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="dataSource" ref="dataSource" />
        <property name="sessionFactory" ref="sessionFactory" />
</bean>


0
幽梦新影
幽梦新影

org.hibernate.engine.SessionFactoryImplementor

我的4.0看不到有这个类,可以把后面一些属性删除了再试试了,提示调用初始化失败,

0
Eriloan
Eriloan

http://blog.csdn.net/shibingli/article/details/7296987

 

看看这里,也许对你有帮助。

0
shelltea
shelltea
<bean id="transactionManager"
	class="org.springframework.orm.hibernate4.HibernateTransactionManager">
	<property name="sessionFactory" ref="sessionFactory" />
</bean>


0
yjpfj1203
yjpfj1203
谢谢了,我知道什么错了,不是sessionFactory错了,是事务配置错了,不好意思哈,我的事务配置还是用的hibernate3. HibernateTransactionManager,现在改成hibernate4.HibernateTransactionManager就不报错了,我一直以为是我的annotation配置错误。第一次试着写个框架,见笑了。
清雨倾城
清雨倾城
其实报的异常里面已经指明是事务配置的问题了。要是看不懂,可以搞个翻译器翻译看一下就知道了,在网上找答案太费时间了。还不一定找得到。
0
yjpfj1203
yjpfj1203

怎么才能弄积分呀,现在没积分,有帮助点不了。

返回顶部
顶部