hibernate中 org.hibernate.MappingException

hibernate中的 org.hibernate.MappingException 解决办法:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="com.hibernate3.model.Student"/>

Caused by: org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="com.hibernate3.model.Student"/>

把配置文件中:applicationContext.xml

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

改成:

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

---------------------------------------

因为,hibernate中注解你用的是import javax.persistence.Entity;

so。。。。


还要注意的是:Configuration只支持xml配置,不支持注解。如果数据库的表用注解写的,同时又用了Configuration,它也会报上面这个错。

解决办法:AnnotationConfiguration扩展了Configuration类,同时支持xml配置和注解方式。

你可能感兴趣的:(java,exception,Hibernate)