4

I have some hbm.xml files in classpath resource located in src/main/resources maven's folder. I used spring's LocalSessionFactoryBean to load these files with the following bean config:

<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSourceOracle"/>
    <property name="mappingResources">
        <list>
            <value>mapping/SystemUser.hbm.xml</value>
            <value>mapping/SystemCredential.hbm.xml</value>
            <value>mapping/SystemProvince.hbm.xml</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <value>
            hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
        </value>
    </property>
</bean>

But it gives me the FileNotFoundException. Please tell me what i've done wrong Thank you.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
robinmag
  • 17,520
  • 19
  • 54
  • 55
  • Similar to [WAR has two META-INF folders](http://stackoverflow.com/questions/17997731/maven-war-has-meta-inf-folder-in-two-places). – dma_k Mar 25 '15 at 10:54

5 Answers5

4

Files located in src/main/resources end up in WEB-INF/classes when using Maven with a project of type war (and the resources directory structure is preserved). So either place your mapping files in src/main/resources/mapping or use the following configuration:

<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSourceOracle"/>
        <property name="mappingResources">
                <list>
                        <value>SystemUser.hbm.xml</value>
                        <value>SystemCredential.hbm.xml</value>
                        <value>SystemProvince.hbm.xml</value>
                </list>
        </property>
        <property name="hibernateProperties">
        <value>
                hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
        </value>
    </property>
</bean>
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
3
@Autowired
private ResourceLoader rl;


@Bean
public LocalSessionFactoryBean sessionFactory() throws IOException {
    LocalSessionFactoryBean sessionFactoryBean = new   LocalSessionFactoryBean();
    sessionFactoryBean.setMappingLocations(loadResources());
}

public Resource[] loadResources() {
    Resource[] resources = null;
    try {
        resources = ResourcePatternUtils.getResourcePatternResolver(rl)
                .getResources("classpath:/hibernate/*.hbm.xml");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return resources;
}
mort
  • 12,988
  • 14
  • 52
  • 97
Syam Elakapalli
  • 256
  • 3
  • 4
1

This looks quite okay to me. Hence I don't think the problem is the config. I rather think the files simply aren't on the classpath. How did you start your application?

If you're using eclipse, make sure src/main/resources is used as source folder and resources are copied to target/classes.

sfussenegger
  • 35,575
  • 15
  • 95
  • 119
  • Yes, i have the default maven's src/main/resources as source folder and my app is a webapp. I've copied these files to web-inf/classes folder but it doesn't work – robinmag Dec 22 '09 at 17:23
  • When you say 'these files to web-inf/classes', do you mean e.g. `web-inf/classes/SystemUser.hbm.xml`? If yes, then you should move it to `web-inf/classes/mapping/SystemUser.hbm.xml`. I don't have any other idea right now. – sfussenegger Dec 22 '09 at 17:39
0

In web applications, when you write a resource path without prefix, Spring loads it from a context root (i.e., from a folder containing WEB-INF). To load resources from a classpath you should use "classpath:" prefix:

<value>classpath:mapping/SystemUser.hbm.xml</value>
axtavt
  • 239,438
  • 41
  • 511
  • 482
0

If you are loading your Spring application context from a webapp, you might see an error like this:

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 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is java.io.FileNotFoundException: ServletContext resource [/hibernate.cfg.xml] cannot be resolved to URL because it does not exist

The solution is to explicitly tell Spring to load the configuration from the classpath like so:

classpath:mypath/myfile.xml
tgogos
  • 23,218
  • 20
  • 96
  • 128