2015-03-22 2 views
0

У меня есть файл hibernate.cfg.xml в моем корневом каталоге java.Как настроить hibernate.cfg.xml в applicationContext.xml как dataSource?

Это мой 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> 

Как добавить источник данных для applicationContext.xml с помощью файла hibernate.cfg.xml.

Также я хочу реализовать компонент "sessionFactory", содержащий org.springframework.orm.hibernate4.LocalSessionFactoryBean в applicationContext.xml ??

Если это так, какие изменения я хочу сделать?

Может кто-нибудь, пожалуйста, помогите мне ??

ответ

2

Вот пример того, как из 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> 
Смежные вопросы