Nagaraju Nooka
Nagaraju Nooka

Reputation: 149

How to specify relative path for hibernate.javax.cache.uri property in xml based spring configuration

I have legacy spring application which is built using xml based configuration I am trying to configure session factory with second level cache.

I have ecache.xml file present in resource folder also hibernate.javax.cache.uri property expects absolute path for ecache.xml.

if i provide URI as file:///c:/App/resources/ecache.xml it works. But this is not good for deployments, maintenance.

how can specify relative path like classpath:ehcache.xml or /WEB-INF/ehcahe.xml in spring xml based configurations ?

Note: I am not using spring boot.

<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan">
            <list>
                <value>com.example.vl.model</value>
            </list>
        </property>
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.javax.cache.uri">file:///c:/App/resources/ecache.xml</prop>
            </props>
        </property>
    </bean>

Upvotes: 4

Views: 2940

Answers (2)

Sergio Lissner
Sergio Lissner

Reputation: 75

Correct format is classpath://ehcache.xml

The following is not working:

classpath:ehcache.xml

classpath:/ehcache.xml

https://github.com/hibernate/hibernate-orm/blob/8461ba2078657caf58930b9485020fe89aff4afe/hibernate-core/src/main/java/org/hibernate/boot/registry/classloading/internal/ClassLoaderServiceImpl.java#L42

Upvotes: 0

Nagaraju Nooka
Nagaraju Nooka

Reputation: 149

OK, I am able to get absolute uri in xml based configuration using Spring EL As follows.

<prop key="hibernate.javax.cache.uri">#{ new org.springframework.core.io.ClassPathResource("/ehcache.xml").getURI().toString()}</prop>

Upvotes: 4

Related Questions