整合SSH三大框架用注解时报An AnnotationConfiguration instance is required to use

时间:2023-12-31 14:40:02

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/classes/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="cn.net.xsoft.po.User"/>
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)

    at java.security.AccessController.doPrivileged(Native Method)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)

    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)

    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)

    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)

    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)

    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)

    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:269)

    ... 46 more

Caused by: org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="cn.net.xsoft.po.User"/>

    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1619)

    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1574)

    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1553)

    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1527)

    at org.hibernate.cfg.Configuration.configure(Configuration.java:1462)

    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:601)

    at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)

... 56 more

解决方案:

将spring 的配置文件 装配 sessionFactory的地方的class

org.springframework.orm.hibernate3.LocalSessionFactoryBean

改成

org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean

检查Spring中sessionFactory中是否引进了hibernate文件

	<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>