Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring and hibernate.cfg.xml

Tags:

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" /> 
like image 823
Steve Kuo Avatar asked Jan 23 '09 03:01

Steve Kuo


People also ask

Where do I put Hibernate cfg XML?

Let us create hibernate. cfg. xml configuration file and place it in the root of your application's classpath. You will have to make sure that you have testdb database available in your MySQL database and you have a user test available to access the database.

What is Hibernate cfg XML?

Hibernate Configuration File(cfg file) is the file loaded into an hibernate application when working with hibernate. Hibernate uses this file to establish connection to the database server.It is an XML file which is used to define below information. Standard name for this file is hibernate.


2 Answers

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> 
like image 134
Stevan Rose Avatar answered Oct 14 '22 09:10

Stevan Rose


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

like image 32
bpapa Avatar answered Oct 14 '22 09:10

bpapa