• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate + Spring... mapping files not loading

 
Dave Turkel
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to use Hibernate and Spring together. I'm trying to run some tests through JUnit.

I'll be glad to post more details, but I'm hoping this is simple enough of a problem that someone can give this noob an answer.

With my given configuration and classpath, my hibernate.cfg.xml appears to be loaded properly in the ApplicationContext, but the actual class mappings located in the same directory has hibernate.cfg.xml are not being loaded (via the mapping tag). I know hibernate.cfg.xml is found because when I change it's name, I get an exception saying it can't be found.

From hibernate.cfg.xml:
<mapping resource="EeActivity.hbm.xml"/>
<mapping resource="EeAction.hbm.xml"/>
<mapping resource="EeAlert.hbm.xml"/>
<mapping resource="EeAppDetailLngView.hbm.xml"/>

Here is my sessionFactory bean configuration from my Spring ApplicationContext.xml config:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"><ref local="dataSource"/></property>
<property name="mappingResources">
<value>hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="eventListeners">
<map>
<entry key="merge">
<bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
</entry>
</map>
</property>
</bean>

Is there an initialization step I may be missing, or something else?

My bean extends HibernateDaoSupport (this is where I pull the application context).

This is the JUnit Test method:
public void testRoleFindById(){
EeRole role = new EeRole();
role.setRoleId(new Integer(1));

RoleDAO roleDao = (RoleDAO)this.applicationContext.getBean("role");

try {
role = (EeRole)roleDao.find(role);
System.out.println("RoleName: " + role.getRoleName());
} catch (DaoException e) {
fail(e.toString());
}

}

Thanks,

David
 
Dave Turkel
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Update:

I found that the sessionFactory bean was not getting a call to initialize itself, as in: sessionFactory = new Configuration().configure().buildSessionFactory();

I assumed that this was something that would be done "automagically" for me between Hibernate and Spring.

Where is the right place to ensure that the sessionFactory is initialized, and how do I do it?

Thanks,

David
 
Dave Turkel
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A little more progress...

I had to change my sessionFactory bean definition to use the configLocation property, rather than the "mappingResources" property:

<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"><ref local="dataSource"/></property>
<property name="configLocation">
<value>hibernate.cfg.xml</value>
</property>
<!--
<property name="mappingResources">
<value>hibernate.cfg.xml</value>
</property>
-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="eventListeners">
<map>
<entry key="merge">
<bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
</entry>
</map>
</property>
</bean>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic