【问题标题】:How configure hibernate.cfg.xml to applicationContext.xml as dataSource?如何将 hibernate.cfg.xml 配置为 applicationContext.xml 作为数据源?
【发布时间】:2015-05-25 16:16:37
【问题描述】:

我的 java 根目录中有 hibernate.cfg.xml 文件。

这是我的applicationContext.xml 文件。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">

    <context:component-scan base-package="com.nought">
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="persistenceUnit" />
        <property name="dataSource" ref="dataSource" />
    </bean>

    <jpa:repositories base-package="com.nought.repository"></jpa:repositories>

</beans>

如何使用 hibernate.cfg.xml 文件将数据源添加到 applicationContext.xml。

我还想在 applicationContext.xml 中实现一个包含org.springframework.orm.hibernate4.LocalSessionFactoryBean 的bean“sessionFactory”吗??

如果做,我想做什么改变??

有人可以帮帮我吗?

【问题讨论】:

    标签: java mysql xml hibernate spring-mvc


    【解决方案1】:

    这里是 hibernate.cfg.xml 的示例以及如何使用它配置 sessionFactory。

    cfg.xml

    <!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    
    <hibernate-configuration>
        <session-factory>
        <property name="connection.datasource">java:comp/env/jdbc/oracle</property>
        <property name="dialect">
            org.hibernate.dialect.Oracle9iDialect
        </property>
    
        .. other config goes here.....
       </session-factory>
    </hibernate-configuration>
    

    将此添加到您的 applicationContext.xml。

    <bean name="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    
            <property name="configLocation" value="classpath:hibernate.cfg.xml" />
            <property name="mappingResources">
                <list>              
                    <value>...your hbm file location...</value>
                </list>
            </property>
        </bean>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-20
      • 2017-08-21
      • 1970-01-01
      • 2019-12-25
      • 2021-12-30
      • 2013-09-14
      • 1970-01-01
      • 2011-08-03
      相关资源
      最近更新 更多