【问题标题】:Spring and hibernate.cfg.xmlSpring 和 hibernate.cfg.xml
【发布时间】:2010-10-03 01:49:43
【问题描述】:

如何让 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"
/>

【问题讨论】:

    标签: java hibernate spring jpa


    【解决方案1】:

    试试这样的……

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-26
        • 1970-01-01
        相关资源
        最近更新 更多