2009-01-23 83 views
24

如何讓Spring從hibernate.cfg.xml加載Hibernate的屬性?Spring和hibernate.cfg.xml

我們使用Spring和JPA(以Hibernate作爲實現)。 Spring的applicationContext.xml指定JPA方言和Hibernate屬性:

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

在這種配置中,春天的applicationContext.xml通過閱讀完所有的Hibernate屬性。當我創建一個hibernate.cfg.xml(位於我的類路徑的根,與META-INF相同)時,Hibernate根本不會讀取它(它完全被忽略)。

我試圖做的是配置Hibernate二級緩存通過插入緩存屬性在hibernate.cfg.xml

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

回答

27

嘗試這樣的事情...

<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> 
相關問題