title: Hibernate mapping resource cannot be opened because it does not exist in a Spring + Hibernate Maven project
body: <p>I am doing a Spring/Hibernate application using annotations to autowire the Service and Repository layers and have less configuration. All that seems to be done successfully since when I run the app everything seems initialized appropriately. </p>
  
  <p><strong>Problem</strong></p>
  
  <p>However, when I add the mapping  resources in my applicationContext-datasource.xml I get an Error stating that the SessionFactory could not be properly initialized because it can seem to find my pojo.hbm.xml file. See:</p>
  
  <p>Caused by: java.io.FileNotFoundException: class path resource [com/dariopardo/jfreechartdemo/pojo/Grid.hbm.xml] cannot be opened because it does not exist</p>
  
  <p><strong>Setup</strong></p>
  
  <p><strong>applicationContext-datasource.xml</strong></p>
  
  <p>This file is located under <strong>src/main/resources/META_INF/spring</strong></p>
  
  <pre><code>  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:jdbc="http://www.springframework.org/schema/jdbc"
  xmlns:jee="http://www.springframework.org/schema/jee"
  xmlns:lang="http://www.springframework.org/schema/lang"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xmlns:p="http://www.springframework.org/schema/p"
  xmlns:task="http://www.springframework.org/schema/task"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:util="http://www.springframework.org/schema/util"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
      http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
      http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
      http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
      http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"
      default-autowire="byName"&gt;
  
    &lt;context:annotation-config/&gt;
  
    &lt;tx:annotation-driven/&gt;
  
    &lt;context:component-scan base-package="com.dariopardo.jfreechartdemo.service"&gt;
      &lt;context:include-filter type="annotation"
          expression="org.springframework.stereotype.Service"/&gt;
   &lt;/context:component-scan&gt;
  
   &lt;context:component-scan base-package="com.dariopardo.jfreechartdemo.dao"&gt;
      &lt;context:include-filter type="annotation"
          expression="org.springframework.stereotype.Repository"/&gt;
   &lt;/context:component-scan&gt;
  
   &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt;
      &lt;property name="driverClassName" value="oracle.jdbc.OracleDriver" /&gt;
      &lt;property name="url" value="jdbc:oracle:thin:@myserverhere:1521:mydb" /&gt;
      &lt;property name="username" value="myusername" /&gt;
      &lt;property name="password" value="mypwd" /&gt;
   &lt;/bean&gt;
  
   &lt;!-- Hibernate SessionFactory for k12--&gt;
  &lt;!--&lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt; --&gt;
      &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
      &lt;property name="dataSource"&gt;&lt;ref local="dataSource"/&gt;&lt;/property&gt;
      &lt;property name="mappingResources"&gt;
          &lt;list&gt;                                            
              &lt;value&gt;com/dariopardo/jfreechartdemo/pojo/Grid.hbm.xml&lt;/value&gt;               
          &lt;/list&gt;
      &lt;/property&gt;
      &lt;property name="hibernateProperties"&gt;
           &lt;props&gt;
          &lt;prop key="hibernate.bytecode.use_reflection_optimizer"&gt;false&lt;/prop&gt;
          &lt;prop key="hibernate.cache.provider_class"&gt;org.hibernate.cache.HashtableCacheProvider&lt;/prop&gt;
          &lt;prop key="hibernate.cache.use_second_level_cache"&gt;true&lt;/prop&gt;
          &lt;prop key="hibernate.cache.region_prefix"&gt;hibernate.k12&lt;/prop&gt;
          &lt;prop key="hibernate.cglib.use_reflection_optimizer"&gt;false&lt;/prop&gt;
          &lt;prop key="hibernate.connection.driver_class"&gt;oracle.jdbc.driver.OracleDriver&lt;/prop&gt;
          &lt;prop key="hibernate.connection.pool_size"&gt;1&lt;/prop&gt;
          &lt;!-- &lt;prop key="hibernate.current_session_context_class"&gt;thread&lt;/prop&gt;  --&gt;
          &lt;prop key="hibernate.default_schema"&gt;K12INTEL_DW&lt;/prop&gt;
          &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.Oracle10gDialect&lt;/prop&gt;
          &lt;prop key="hibernate.format_sql"&gt;true&lt;/prop&gt;
          &lt;prop key="hibernate.generate_statistics"&gt;true&lt;/prop&gt;
          &lt;prop key="hibernate.jdbc.batch_versioned_data"&gt;true&lt;/prop&gt;
          &lt;prop key="hibernate.jdbc.use_streams_for_binary"&gt;true&lt;/prop&gt;
          &lt;prop key="hibernate.max_fetch_depth"&gt;1&lt;/prop&gt;
          &lt;prop key="hibernate.proxool.pool_alias"&gt;pool_K12&lt;/prop&gt;
          &lt;prop key="hibernate.query.substitutions"&gt;yes 'Y', no 'N'&lt;/prop&gt;
          &lt;prop key="hibernate.show_sql"&gt;false&lt;/prop&gt;
          &lt;prop key="hibernate.transaction.auto_close_session"&gt;&lt;/prop&gt;
          &lt;prop key="hibernate.transaction.flush_before_completion"&gt;&lt;/prop&gt;
          &lt;prop key="hibernate.use_sql_comments"&gt;true&lt;/prop&gt;
      &lt;/props&gt;
      &lt;/property&gt;
      &lt;property name="eventListeners"&gt;
          &lt;map&gt;
              &lt;entry key="merge"&gt;
                  &lt;bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/&gt;
              &lt;/entry&gt;
          &lt;/map&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  
  &lt;!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) --&gt;
  &lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory"/&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>web.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt;
  
  &lt;!-- The definition of the Root Spring Container shared by all Servlets and Filters --&gt;
  &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;/WEB-INF/spring/root-context.xml
                   classpath*:META-INF/spring/applicationContext*.xml                  
      &lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;!-- Creates the Spring Container shared by all Servlets and Filters --&gt;
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;!-- Processes application requests --&gt;
  &lt;servlet&gt;
      &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/spring/appServlet/servlet-context.xml&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>My POJO</strong></p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;
  &lt;hibernate-mapping package="com.dariopardo.jfreechartdemo.pojo"&gt;
  
  &lt;class name="Grid"&gt;
      &lt;cache usage="read-only" /&gt;
      &lt;id name="c1"/&gt;
      &lt;property name="c2"&gt;&lt;/property&gt;
      &lt;property name="c3"&gt;&lt;/property&gt;
  &lt;/class&gt;
  
  &lt;sql-query name="studentAttendanceOver95Pct"&gt;
      &lt;return alias="p" class="com.dariopardo.jfreechartdemo.pojo.Grid"&gt;
       &lt;return-property name="c1" column="school"/&gt;
       &lt;return-property name="c2" column="at_risk"/&gt;
       &lt;return-property name="c2" column="not_at_risk"/&gt;
       &lt;/return&gt;
       My custom sql query goes here
  &lt;/sql-query&gt;   
  </code></pre>
  
  <p></p>
  
  <p><strong>My DAO layer</strong></p>
  
  <pre><code>@Repository
  public class K12DaoImpl extends HibernateDaoSupport implements K12DaoManager{
  
  @Autowired
  public K12DaoImpl(SessionFactory sessionFactory) {
      super.setSessionFactory(sessionFactory);
  }
  
  @Override
  public List&lt;Grid&gt; getAttendanceBySchoolOver95Pct() {
  
      List&lt;Grid&gt; list =    getHibernateTemplate().findByNamedQuery("studentAttendanceOver95Pct");
      return list;
  }
  }
  </code></pre>
  
  <p><strong>My Service Layer</strong></p>
  
  <pre><code>@Service
  public class K12ManagerImpl implements K12Manager {
  
  @Resource
  private K12DaoManager k12DaoManager;
  
  @Override
  public List&lt;Grid&gt; studentPctAttendance() {
  
      return k12DaoManager.getAttendanceBySchoolOver95Pct();
  }
  }
  </code></pre>
  
  <p>If I comment the the hbm.xml in the mappingResources section everything seems to wire up correctly, but as soon as I bring any hbm.xml file in the mapping resources section the application fails to initialize.</p>
  
  <p>Any thoughts?</p>
  
  <p>Thanks,
  - Dario</p>