Spring和hibernate.cfg.xml [英] Spring and hibernate.cfg.xml
问题描述
我如何让Spring从 hibernate.cfg.xml加载Hibernate的属性
?
How do I get Spring to load Hibernate's properties from hibernate.cfg.xml
?
使用Spring和JPA(以Hibernate作为实现)。 Spring的 applicationContext.xml
指定JPA方言和Hibernate属性:
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>
在这个配置中,Spring通过applicationContext.xml读取所有的Hibernate属性。当我创建一个 hibernate.cfg.xml
(位于我的类路径的根目录,与META-INF级别相同)时,Hibernate根本不读取它(它是完全忽略)。
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).
我正在尝试通过在 hibernate.cfg.xml中插入缓存属性来配置Hibernate二级缓存
:
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"
/>
推荐答案
试试像这样...
<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>
这篇关于Spring和hibernate.cfg.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!