Grook
Grook

Reputation: 405

Hibernate + Spring - xml mapping not found

I have simple application with following folder structure:

here is the part of beans.xml Spring config file:

<bean id="propertyConfigurer"  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:./META-INF/jdbc.properties" />
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:./META-INF/hibernate.cfg.xml" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
        </props>
    </property>

    <property name="mappingResources">
<list>
      <value>classpath:./META-INF/EntityMapping.hbm.xml</value>
</list>
 </property>
</bean>

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

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

when i start my unit tests i getting following exception:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'wrapperClass' defined in class path resource [META-INF/beans.xml]: Cannot resolve reference to bean 'wrapperClassField' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xmlBooksource' defined in class path resource [META-INF/beans.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 class path resource [META-INF/beans.xml]: Invocation of init method failed; nested exception is java.io.FileNotFoundException: class path resource [classpath:/META-INF/EntityMapping.hbm.xml] cannot be opened because it does not exist

The same exception is thrown when i type

<property name="mappingResources">
<list>
      <value>EntityMapping.hbm.xml</value>
</list>
 </property>

Why spring cant find this file and how i must fill its location to make this code work?

Thanks in advance.

Upvotes: 1

Views: 11099

Answers (3)

iltaf khalid
iltaf khalid

Reputation: 10318

I have successfully configure Hibernate 4 with Spring 3.1. My applicationContext.xml file is inside web-inf folder and has the following hibernate cofiguration:

<!-- Session Factory Declaration -->
 <bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  <property name="dataSource" ref="DataSource" />
  <!--  
  <property name="annotatedClasses">
   <list>
    <value>iltaf.models.Levels</value>
   </list>
  </property>
  -->

    <property name="mappingLocations" value="classpath:iltaf/models/*.hbm.xml" />


    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>

  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
    <prop key="hibernate.show_sql">true</prop>
   </props>
  </property>
 </bean>

 <!-- Enable the configuration of transactional behavior based on annotations -->
    <tx:annotation-driven transaction-manager="txManager"/>

 <!-- Transaction Manager is defined -->
    <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
       <property name="sessionFactory" ref="SessionFactory"/>
    </bean>

</beans>

and I have separate hibernate.cfg.xml file inside my src folder. I am using Eclipse Juno Java EE version.

Upvotes: 0

Gray
Gray

Reputation: 116888

Have you tried removing the classpath: prefix? In looking at the Hibernate code, the mappingResources setter expects passes the strings to new ClassPathResource(String). This expects classpath resources already. The string then gets passed to ClassLoader.getResourceAsStream(String). None of this code would strip the "classpath:" prefix from the front of the resource string.

Upvotes: 3

minodudd
minodudd

Reputation: 40

I'm not sure the error message is consistent with the beans.xml content you posted. In the error you have

[classpath:/META-INF/EntityMapping.hbm.xml]

which isn't the same as

 classpath:./META-INF/EntityMapping.hbm.xml

Notice the missing "." at the beginning in the error.

The second beans.xml configuration, should probably produce a different error message with:

[classpath:EntityMapping.hbm.xml]

This would be searching for the file in the root of your compiled application (jar, war, exploded, what have you).

Upvotes: 0

Related Questions