Wednesday, September 30, 2015

Exception in thread "main" org.springframework.orm.hibernate3.HibernateSystemException:

Getting following exception while testing an Hibernate Application.

I have GeoLocationDataDao while testing a method getGeoLocationFromZipCode(zipcode) which returns the coordinates for a provided zip code.

I get following exception while running the test class.
Exception in thread "main" org.springframework.orm.hibernate3.HibernateSystemException: Unknown entity: com.testapp.service.geolocation.GeoLocationData; nested exception is org.hibernate.MappingException: Unknown entity: com.testapp.service.geolocation.GeoLocationData at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:690) at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412) at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411) at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374) at org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:512) at org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:506) at com.testapp.service.geolocation.GeoLocationDataDao.getGeoLocationFromZipCode(GeoLocationDataDao.java:35) at org.mg.service.SpringHibernateTest.main(SpringHibernateTest.java:46) Caused by: org.hibernate.MappingException: Unknown entity: com.mg.service.geolocation.GeoLocationData at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:693) at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:92) at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:1090) at org.hibernate.impl.SessionImpl.get(SessionImpl.java:1005) at org.hibernate.impl.SessionImpl.get(SessionImpl.java:998) at org.springframework.orm.hibernate3.HibernateTemplate$1.doInHibernate(HibernateTemplate.java:519) at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406) ... 5 more


Fix :

Error was coming as the Entity object required by Dao could not be instantiated as it could not find its mapping.We have to list our classes in the session factory configuration. We can either have our entities auto-discovered using AnnotationSessionFactoryBean or we can use LocalSessionFactoryBean if have entity xml files.

Following is an example, if we are using EntityManager and annotations.

 <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="annotatedClasses">
        <list>
            <value>com.testapp.service.geolocation.GeoLocationData</value>
        </list>
    </property>
    ....
 </bean>


If we are not using annotation and want to specify the entities in a child xml we can use LocalSessionFactoryBean where we can specify a list of entity beans xmls as shown below.

<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="mgDataSource"/>
    <property name="mappingResources">
        <list>
            <value>geolocationdata.hbm.xml</value>      
            <value>profile.hbm.xml</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <value>hibernate.dialect=org.hibernate.dialect.HSQLDialect</value>
    </property>
</bean>

Here is a sample entity xml geolocationdata.hbm.xml configured above.

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.testapp.service.geolocation.GeoLocationData" table="GeoLocationData">
    <id name="zip_code" column="zip_code">
        <generator class="assigned"/>
    </id>
  <property name="lat"><column name="lat"/></property>
  <property name="lon"><column name="lon"/></property>
  <property name="city"><column name="city"/></property>
  <property name="state_prefix"><column name="state_prefix"/></property>
  <property name="county"><column name="county"/></property>
</class>
</hibernate-mapping>

No comments:

Post a Comment