Java Spring 和 hibernate.cfg.xml

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/471799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:03:55  来源:igfitidea点击:

Spring and hibernate.cfg.xml

javahibernatespringjpa

提问by Steve Kuo

How do I get Spring to load Hibernate's properties from hibernate.cfg.xml?

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

We're using Spring and JPA (with Hibernate as the implementation). Spring's applicationContext.xmlspecifies the JPA dialect and Hibernate properties:

我们正在使用 Spring 和 JPA(使用 Hibernate 作为实现)。SpringapplicationContext.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>

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

在此配置中,Spring 通过 applicationContext.xml 读取所有 Hibernate 属性。当我创建一个hibernate.cfg.xml(位于我的类路径的根,与 META-INF 相同的级别)时,Hibernate 根本不读取它(它完全被忽略)。

What I'm trying to do is configure Hibernate second level cache by inserting the cache properties in hibernate.cfg.xml:

我想要做的是通过在hibernate.cfg.xml以下位置插入缓存属性来配置 Hibernate 二级缓存:

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

采纳答案by Stevan Rose

Try something like this...

尝试这样的事情......

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

回答by bpapa

The way I've done this before is by instantiating a LocalSessionFactoryBeanand setting the configLocation property.

我以前这样做的方法是实例化LocalSessionFactoryBean并设置 configLocation 属性。