如何将hibernate.cfg.xml配置为applicationContext.xml作为dataSource? [英] How configure hibernate.cfg.xml to applicationContext.xml as dataSource?

查看:130
本文介绍了如何将hibernate.cfg.xml配置为applicationContext.xml作为dataSource?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是我的 applicationContext.xml 文件。

 <?xml version =1.0encoding =UTF-8?> 
< beans xmlns =http://www.springframework.org/schema/beans
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexmlns :context =http://www.springframework.org/schema/context
xmlns:jpa =http://www.springframework.org/schema/data/jpaxmlns: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 =transactionManagerclass =org.springframework.orm.jpa.JpaTransactionManager>
< property name =entityManagerFactoryref =entityManagerFactory/>
< / bean>

< tx:注解驱动的事务管理器=transactionManager/>

< bean id =entityManagerFactory
class =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
< property name =persistenceUnitNamevalue =persistenceUnit/>
< property name =dataSourceref =dataSource/>
< / bean>

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

< / beans>

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



另外我是否想在applicationContext中实现一个包含 org.springframework.orm.hibernate4.LocalSessionFactoryBean 的beansessionFactory .xml ??

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



有人可以帮忙吗



cfg.xml
$ b

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

..其他配置在这里.....
< / session-factory>
< / hibernate-configuration>

添加您的applicationContext.xml。

 < bean name =sessionFactoryclass =org.springframework.orm.hibernate4.LocalSessionFactoryBean> 

< property name =configLocationvalue =classpath:hibernate.cfg.xml/>
< property name =mappingResources>
< list>
<值> ...您的hbm文件位置...< /值>
< / list>
< / property>
< / bean>


I have hibernate.cfg.xml file in my java root directory.

Here is my applicationContext.xml file.

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

How I add a data source to applicationContext.xml using hibernate.cfg.xml file.

Also Do I want to implement a bean "sessionFactory" that contain with org.springframework.orm.hibernate4.LocalSessionFactoryBean in applicationContext.xml ??

If do, what changes that I want to do??

Can someone please help me??

解决方案

Here is sample of how of hibernate.cfg.xml and how to configure your sessionFactory with it.

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>

Add this your 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>

这篇关于如何将hibernate.cfg.xml配置为applicationContext.xml作为dataSource?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆