2009-01-23 82 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> 
相关问题