引起:org.hibernate.MappingException:无法在配置中加载声明为<mapping class =“xxxxx”>的类:

时间:2016-06-13 11:46:46

标签: java spring hibernate

在我的春季网络应用程序中,我在启动弹簧应用程序时收到此错误

SEVERE: 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 ServletContext resource [/WEB-INF/spring/data.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 ServletContext resource [/WEB-INF/spring/data.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Unable to load class declared as <mapping class="reshetyk.alexey.diary.domain.DiaryUser"/> in the configuration:

此外,我将此作为第二个错误

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring/data.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Unable to load class declared as <mapping class="reshetyk.alexey.diary.domain.DiaryUser"/> in the configuration:

类DiaryUser在包中定义,并具有以下属性,假设getter和setter定义良好

这是我的DiaryUser class.java实体类的完整代码

    @Entity
@Table(name = "USERS")
public class DiaryUser implements Serializable {

    @Id
    @Column(name = "ID_USER")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Column(name = "LOGIN", unique = true, nullable = false)
    private String login;

    @Column(name = "PASSWORD", nullable = false)
    private String password;

    public DiaryUser() {
    }
    public DiaryUser(Integer id) {
        this.id = id;
    }

    public DiaryUser(String login) {
        this.login = login;
    }

    public DiaryUser(Integer id, String login, String password) {
        this.id = id;
        this.login = login;
        this.password = password;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}

我在hibernate.cfg.xml文件中同样定义了这个

<hibernate-configuration>
    <session-factory>
        <mapping class="reshetyk.alexey.diary.domain.DiaryUser" />
        <mapping class="reshetyk.alexey.diary.domain.DiaryCategory" />
        <mapping class="reshetyk.alexey.diary.domain.DiaryRecord" />
    </session-factory>
</hibernate-configuration>

这是我的data.xml文件,其中包含hibernate映射的数据库定义配置

<tx:annotation-driven transaction-manager="transactionManager" />


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


    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>


    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="/WEB-INF/jdbc.properties" />


    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        p:driverClassName="${jdbc.driverClassName}" 
        p:url="${jdbc.databaseurl}"
        p:username="${jdbc.username}" 
        p:password="${jdbc.password}" />


    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
        <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.connection.charSet">UTF-8</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>

我有点困惑,因为我不知道为什么会收到这个错误?

2 个答案:

答案 0 :(得分:1)

不要使用LocalSessionFactoryBean,而是尝试使用AnnotationSessionFactoryBean,并指定映射类,如下所示(查看链接:AnnotationSessionFactoryBean

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
   <property name="dataSource" ref="dataSource"/>
   <property name="annotatedClasses">
     <list>
       <value>test.package.Foo</value>
       <value>test.package.Bar</value>
     </list>
   </property>
 </bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
   <property name="dataSource" ref="dataSource"/>
   <property name="packagesToScan" value="test.package"/>
 </bean>

而不是在hibernate.cfg.xml下定义映射。

您可以修改data.xml文件中的sessionFactory详细信息,如下所示:

<bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!--<property name="configLocation" value="classpath:hibernate.cfg.xml"/>-->
        <property name="annotatedClasses">
         <list>
           <value>reshetyk.alexey.diary.domain.DiaryUser</value>
           <value>reshetyk.alexey.diary.domain.DiaryCategory</value>
           <value>reshetyk.alexey.diary.domain.DiaryRecord</value>
         </list>
       </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.connection.charSet">UTF-8</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>

答案 1 :(得分:0)

请确保正确定义了映射资源。