Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring - hibernate load *.hbm.xml from classpath resource

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.

like image 832
robinmag Avatar asked Feb 27 '23 21:02

robinmag


2 Answers

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>
like image 98
Pascal Thivent Avatar answered Mar 05 '23 14:03

Pascal Thivent


@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;
}
like image 24
Syam Elakapalli Avatar answered Mar 05 '23 14:03

Syam Elakapalli