views:

4977

answers:

2

How do I get Spring to load Hibernate's properties from hibernate.cfg.xml?

We're using Spring and JPA (with Hibernate as the implementation). Spring's applicationContext.xml specifies the JPA dialect and Hibernate properties:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
        </props>
    </property>
</bean>

In this configuration, Spring is reading all the Hibernate properties via applicationContext.xml . When I create a hibernate.cfg.xml (located at the root of my classpath, the same level as META-INF), Hibernate doesn't read it at all (it's completely ignored).

What I'm trying to do is configure Hibernate second level cache by inserting the cache properties in hibernate.cfg.xml:

<cache 
    usage="transactional|read-write|nonstrict-read-write|read-only"
    region="RegionName"
    include="all|non-lazy"
/>
+2  A: 

The way I've done this before is by instantiating a LocalSessionFactoryBean and setting the configLocation property.

bpapa
+8  A: 

Try something like this...

<bean
    id="mySessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

    <property name="configLocation">    
        <value>
            classpath:location_of_config_file/hibernate.cfg.xml
        </value>
    </property>

    <property name="hibernateProperties">
        <props>

            ...    


        </props>    
    </property>

</bean>
Stevan Rose