Blaze Blaze - 5 months ago 123
Java Question

Caused by: org.hibernate.MappingException: Unable to load class declared as <mapping class="xxxxx"/> in the configuration:

In my spring web application I am getting this error on launching my spring application

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:


Also I have this as a second error

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:


The class DiaryUser is defined in the package and have the following properties assuming the getters and setters are well defined

this is the complete code of my entity class for 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;
}

}


I have equally defined this in my hibernate.cfg.xml file

<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>


this is my data.xml file that contains my database definition configuration for hibernate mappings

<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>


I am kind of confused as I dont know why I am getting this error?

S.B S.B
Answer

Instead of using LocalSessionFactoryBean,try using the AnnotationSessionFactoryBean instead and specify the mapping classes as shown below(check the link: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>

or

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

instead of defining the mappings under hibernate.cfg.xml.

Your sessionFactory details in data.xml file can be modified as below:

<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>
Comments