Spring - hibernate * .hbm.xml download from classpath resource

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

<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 that gives me a FileNotFoundException. Please tell me what I did wrong. Thank you.

+3
source share
5 answers

, src/main/resources, WEB-INF/classes Maven war ( resources ). src/main/resources/mapping, :

<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>
+4

. , - . , . ?

eclipse, , src/main/resources , target/classes.

+1
@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;
}
+1
source

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

<value>classpath:mapping/SystemUser.hbm.xml</value>
0
source

If you are loading your Spring application context from webapp, you may see this error:

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 class path as follows:

classpath:mypath/myfile.xml
0
source

Source: https://habr.com/ru/post/1726359/


All Articles