gpt4 book ai didi

java - Spring 和 hibernate.cfg.xml

转载 作者:IT老高 更新时间:2023-10-28 13:47:35 25 4
gpt4 key购买 nike

如何让 Spring 从 hibernate.cfg.xml 加载 Hibernate 的属性?

我们使用 Spring 和 JPA(使用 Hibernate 作为实现)。 Spring 的 applicationContext.xml 指定了 JPA dialect 和 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>

关于java - Spring 和 hibernate.cfg.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/471799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com