[英]Spring and Hibernate - Use without hardcoding password in hibernate.cfg.xml
[英]Spring and hibernate.cfg.xml
如何让Spring从hibernate.cfg.xml
加载Hibernate的属性?
我们使用的是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>
在这个配置中,Spring通过applicationContext.xml读取所有Hibernate属性。 当我创建一个hibernate.cfg.xml
(位于我的类路径的根目录,与META-INF相同的级别)时,Hibernate根本不读它(它完全被忽略)。
我想要做的是通过在hibernate.cfg.xml
插入缓存属性来配置Hibernate二级缓存:
<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>
之前我这样做的方法是实例化LocalSessionFactoryBean并设置configLocation属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.