使用多个数据库休眠

Posted

技术标签:

【中文标题】使用多个数据库休眠【英文标题】:Hibernate using multiple databases 【发布时间】:2011-08-16 04:22:48 【问题描述】:

有人知道如何在休眠配置中添加另一个数据源,以及如何将 Spring 配置到该数据源,使其在我各自的 DAO 中自动注入?

这是我的带有一个数据源的代码,可以完美运行,但我不知道如何添加另一个数据源。我想添加另一个数据源,它是一个具有与实际数据库不同的表的数据库。

休眠配置

 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName">
            <value>com.mysql.jdbc.Driver</value>
        </property>
        <property name="url" value="jdbc:mysql://localhost/personal"/>
        <property name="username" value="root"/>
        <property name="password" value="mysql"/>
    </bean>


    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource">
            <ref local="dataSource"/>
        </property>
        <property name="packagesToScan">
            <list>
                <value>com.app.personal.model</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">false</prop>
            </props>
        </property>
    </bean>

    <tx:annotation-driven/>
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref local="sessionFactory"/>
        </property>
    </bean>

DAO 示例

@Repository
public class ModuloDAOHibernate extends HibernateTemplate implements ModuloDAO 

    @Autowired
    public ModuloDAOHibernate(SessionFactory sessionFactory) 
        super(sessionFactory);
    

    public List<Modulo> getAllGrupoModuloDAO() 
        Criteria criteriaList = this.getSession().createCriteria(Modulo.class);
        criteriaList.addOrder(Order.asc("orden"));
        return criteriaList.list();
    

【问题讨论】:

【参考方案1】:

我假设您有一组 DAO 应该使用 dataSource1 和适当的 sessionFactory1,而其他应该使用不同的 dataSouce2sessionFactory2 基于 dataSource2。当然,您需要声明您的第二个 dataSource 和其他 bean:只需复制您已有的配置并更改 bean id,这样它们就不会发生冲突。除了&lt;tx:annotation-driven/&gt;

<bean id="dataSource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <!-- ... -->
</bean>

<bean id="sessionFactory1" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource1"/>
    <!-- ... -->
</bean>

<bean id="transactionManager1" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory1"/>
    <!-- ... -->
</bean>


<bean id="dataSource2" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <!-- ... -->
</bean>

<bean id="sessionFactory2" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource2"/>
    <!-- ... -->
</bean>

<bean id="transactionManager2" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory2"/>
    <!-- ... -->
</bean>

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

真正的问题来了:您现在有两个事务管理器绑定到不同的会话工厂,它们又被路由到不同的数据源。但是@Transactional 注解将始终只使用一个事务管理器——默认情况下名为transactionManager 的事务管理器(注意我明确指出transactionManager1。这意味着使用第二个数据源的DAO 将参与在第一个数据源内启动的事务——这是显然不是预期的。

对此有一些解决方法,例如在@Transactional 注释中显式定义事务管理器名称(从未尝试过)或使用TransactionTemplate,但正如您所见,应该深思熟虑。

至于自动装配 - 如果您按名称自动装配,请将您的字段命名为与会话工厂或数据源 ID 相同的名称,它应该可以工作 - 但实际上是您最小的问题。

【讨论】:

Spring 3 增加了与多个事务管理器一起工作的能力,允许您为每个事务管理器命名。详细信息可以在这里找到:static.springsource.org/spring/docs/3.0.x/…【参考方案2】:

好的。我找到了另一个解决方案,那就是使用相同的方法,如下所示:添加另一个数据源和 SessionFactory,接下来在注入 sessionFactory 的 DAO 方法中添加 @Qualifier 注释以及所需的 sessionFactory 属性,如下所示:

  @Autowired 
    public ProgramaSgteDAOHibernate(@Qualifier("sessionFactory3") SessionFactory sessionFactory)  
     super(sessionFactory); 
    

【讨论】:

能否请您发布您的新 hibernate.cfg.xml 文件?我正在尝试解决同样的问题。【参考方案3】:

我遇到了同样的问题。我通过创建解决了这个问题: applicationContext.xml

<!-- dataSource properies -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:dataSource.properties" />
</bean>

<!-- MySQL -->
<bean id="mySQLdataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="$mySql.driverClassName" />
    <property name="url" value="$mySql.url" />
    <property name="username" value="$mySql.username" />
    <property name="password" value="$mySql.password" />
</bean>
<bean id="mySQLsessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="mySQLdataSource" />
    <property name="packagesToScan" value="com.victor.entity" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">$mySql.dialect</prop>
        </props>
    </property>
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
</bean>

<bean id="mySQLtransactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="mySQLsessionFactory" />
</bean>
<tx:annotation-driven proxy-target-class="true"
    transaction-manager="mySQLtransactionManager" />

<!-- ORACLE -->
<bean id="oracleDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="$oracle.driverClassName" />
    <property name="url" value="$oracle.url" />
    <property name="username" value="$oracle.username" />
    <property name="password" value="$oracle.password" />
</bean>
<bean id="oracleSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="oracleDataSource" />
    <property name="packagesToScan" value="com.victor.entity" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">$oracle.dialect</prop>
        </props>
    </property>
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
</bean>
<tx:annotation-driven proxy-target-class="true"
    transaction-manager="oracleTransactionManager" />
<bean id="oracleTransactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="oracleSessionFactory" />
</bean>

在 Dao 中注入了带有 Qualifier 注解的 sessionFactory。就我而言,我有通用的 BaseEnity:

public abstract class BaseEntityDAOImpl<T extends BaseEntity> implements BaseEntityDAO<T> 

private Class<T> persistentClass;
@Autowired
@Qualifier("oracleSessionFactory")
SessionFactory sessionFactory;

在服务 bean 中使用注解:

@Service
@Transactional(propagation = Propagation.REQUIRED, readOnly = true, value = "oracleTransactionManager")
public class UserService 

@Autowired
private UserDAO dao;

一切都很好。

【讨论】:

【参考方案4】:

我遇到了同样的问题。我是这样解决的:

首先,不同的数据库应该有不同的cfg.xml文件。然后,只要您想连接到第二个数据库,只需使用 Hibernate 的配置对象。

Configuration config = new Configuration().configure("<complete path to your cfg.xml file>");
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();

我在这里找到了这个:http://www.coderanch.com/t/468821/ORM/java/If-hibernate-cfg-xml-has

我很确定这可以扩展到 2 个以上的数据库。 希望这会有所帮助。

【讨论】:

【参考方案5】:

我知道我回复晚了,但是我以这种方式解决了问题(如何使用 spring 和 Hibernate 连接多个数据库),我希望它会有所帮助:)

NOTE: I have added the relevant code, kindly make the dao and rest with the help of impl I used in the below mentioned code.

**web.xml**

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>MultipleDatabaseConnectivityInSpring</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
     <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
         <load-on-startup>1</load-on-startup>
    </servlet>
     <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener> 
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/dispatcher-servlet.xml
        </param-value>
    </context-param>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
</web-app>


**persistence.xml**


<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    <persistence-unit name="localPersistenceUnitOne"
        transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>in.india.entities.CustomerDetails</class>
        <exclude-unlisted-classes />
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
            <property name="hibernate.jdbc.batch_size" value="0" />
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/shankar?sslmode=require" />
            <property name="hibernate.connection.username" value="username" />
            <property name="hibernate.connection.password" value="password" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>
    <persistence-unit name="localPersistenceUnitTwo"
        transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>in.india.entities.CompanyDetails</class>
        <exclude-unlisted-classes />
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
            <property name="hibernate.jdbc.batch_size" value="0" />
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/shankarTwo?sslmode=require" />
            <property name="hibernate.connection.username" value="username" />
            <property name="hibernate.connection.password" value="password" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>
</persistence>


**dispatcher-servlet**


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:task="http://www.springframework.org/schema/task" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
    default-autowire="byName"
    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-3.0.xsd  
      http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
      http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <!-- Configure messageSource -->

    <mvc:annotation-driven />
    <context:component-scan base-package="in.india.*" />
    <bean id="messageResource"
        class="org.springframework.context.support.ResourceBundleMessageSource"
        autowire="byName">
        <property name="basename" value="messageResource"></property>
    </bean>

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>



    <bean id="entityManagerFactoryOne"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        autowire="constructor">
        <property name="persistenceUnitName" value="localPersistenceUnitOne" />
    </bean>

    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource"
        autowire="byName">
        <property name="basename" value="messageResource" />
    </bean>

    <bean id="entityManagerFactoryTwo"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        autowire="constructor">
        <property name="persistenceUnitName" value="localPersistenceUnitTwo" />
    </bean>

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

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

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

    <!-- declare dependies here -->

    <bean class="in.india.service.dao.impl.CustomerServiceImpl" />
    <bean class="in.india.service.dao.impl.CompanyServiceImpl" />

    <!-- Configure MVC annotations -->
    <bean
        class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
    <bean
        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
</beans>



**java class to persist into one database**


package in.india.service.dao.impl;

import in.india.entities.CompanyDetails;
import in.india.service.CompanyService;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.transaction.annotation.Transactional;

public class CompanyServiceImpl implements CompanyService 

    @PersistenceContext(unitName = "entityManagerFactoryTwo")
    EntityManager entityManager;

    @Transactional("manager2")
    @Override
    public boolean companyService(CompanyDetails companyDetails) 

        boolean flag = false;
        try 
        
            entityManager.persist(companyDetails);
            flag = true;
         
        catch (Exception e)
        
            flag = false;
        

        return flag;
    




**java class to persist in another database** 

package in.india.service.dao.impl;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.transaction.annotation.Transactional;

import in.india.entities.CustomerDetails;
import in.india.service.CustomerService;

public class CustomerServiceImpl implements CustomerService 

    @PersistenceContext(unitName = "localPersistenceUnitOne")
    EntityManager entityManager;

    @Override
    @Transactional(value = "manager1")
    public boolean customerService(CustomerDetails companyData) 

        boolean flag = false;
        entityManager.persist(companyData);
        return flag;
    



**customer.jsp**

<%@page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <center>
        <h1>SpringWithMultipleDatabase's</h1>
    </center>
    <form:form method="GET" action="addCustomer.htm"  modelAttribute="customerBean" >
        <table>
            <tr>
                <td><form:label path="firstName">First Name</form:label></td>
                <td><form:input path="firstName" /></td>
            </tr>
            <tr>
                <td><form:label path="lastName">Last Name</form:label></td>
                <td><form:input path="lastName" /></td>
            </tr>
            <tr>
                <td><form:label path="emailId">Email Id</form:label></td>
                <td><form:input path="emailId" /></td>
            </tr>
            <tr>
                <td><form:label path="profession">Profession</form:label></td>
                <td><form:input path="profession" /></td>
            </tr>
            <tr>
                <td><form:label path="address">Address</form:label></td>
                <td><form:input path="address" /></td>
            </tr>
            <tr>
                <td><form:label path="age">Age</form:label></td>
                <td><form:input path="age" /></td>
            </tr>
            <tr>
                <td><input type="submit" value="Submit"/></td>
             </tr>
        </table>
    </form:form>
</body>
</html>


**company.jsp**


<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>ScheduleJobs</title>
</head>
<body>
 <center><h1>SpringWithMultipleDatabase's</h1></center>
 <form:form method="GET" action="addCompany.htm"  modelAttribute="companyBean" >
 <table>
    <tr>
        <td><form:label path="companyName">Company Name</form:label></td>
        <td><form:input path="companyName" /></td>
    </tr>
    <tr>
        <td><form:label path="companyStrength">Company Strength</form:label></td>
        <td><form:input path="companyStrength" /></td>
    </tr>
    <tr>
        <td><form:label path="companyLocation">Company Location</form:label></td>
        <td><form:input path="companyLocation" /></td>
    </tr>
     <tr>
        <td>
            <input type="submit" value="Submit"/>
        </td>
    </tr>
 </table>
 </form:form>
</body>
</html>

**index.jsp**

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home</title>
</head>
<body>
 <center><h1>Multiple Database Connectivity In Spring sdfsdsd</h1></center>

<a href='customerRequest.htm'>Click here to go on Customer page</a>
<br>
<a href='companyRequest.htm'>Click here to go on Company page</a>
</body>
</html>

**success.jsp**

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>ScheduleJobs</title>
</head>
<body>
 <center><h1>SpringWithMultipleDatabase</h1></center>
    <b>Successfully Saved</b>
</body>
</html>

**CompanyController**

package in.india.controller;

import in.india.bean.CompanyBean;
import in.india.entities.CompanyDetails;
import in.india.service.CompanyService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class CompanyController 

    @Autowired
    CompanyService companyService;

    @RequestMapping(value = "/companyRequest.htm", method = RequestMethod.GET)
    public ModelAndView addStudent(ModelMap model) 
        CompanyBean companyBean = new CompanyBean();
        model.addAttribute(companyBean);
        return new ModelAndView("company");
    

    @RequestMapping(value = "/addCompany.htm", method = RequestMethod.GET)
    public ModelAndView companyController(@ModelAttribute("companyBean") CompanyBean companyBean, Model model) 
        CompanyDetails  companyDetails = new CompanyDetails();
        companyDetails.setCompanyLocation(companyBean.getCompanyLocation());
        companyDetails.setCompanyName(companyBean.getCompanyName());
        companyDetails.setCompanyStrength(companyBean.getCompanyStrength());
        companyService.companyService(companyDetails);
        return new ModelAndView("success");

    


**CustomerController**


package in.india.controller;

import in.india.bean.CustomerBean;
import in.india.entities.CustomerDetails;
import in.india.service.CustomerService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class CustomerController 

    @Autowired
    CustomerService customerService;

    @RequestMapping(value = "/customerRequest.htm", method = RequestMethod.GET)
    public ModelAndView addStudent(ModelMap model) 
        CustomerBean customerBean = new CustomerBean();
        model.addAttribute(customerBean);
        return new ModelAndView("customer");
    

    @RequestMapping(value = "/addCustomer.htm", method = RequestMethod.GET)
    public ModelAndView customerController(@ModelAttribute("customerBean") CustomerBean customer, Model model) 
        CustomerDetails customerDetails = new CustomerDetails();
        customerDetails.setAddress(customer.getAddress());
        customerDetails.setAge(customer.getAge());
        customerDetails.setEmailId(customer.getEmailId());
        customerDetails.setFirstName(customer.getFirstName());
        customerDetails.setLastName(customer.getLastName());
        customerDetails.setProfession(customer.getProfession());
        customerService.customerService(customerDetails);
        return new ModelAndView("success");

    



**CompanyDetails Entity**


package in.india.entities;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name = "company_details")
public class CompanyDetails 

    @Id
    @SequenceGenerator(name = "company_details_seq", sequenceName = "company_details_seq", initialValue = 1, allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "company_details_seq")
    @Column(name = "company_details_id")
    private Long companyDetailsId;
    @Column(name = "company_name")
    private String companyName;
    @Column(name = "company_strength")
    private Long companyStrength;
    @Column(name = "company_location")
    private String companyLocation;

    public Long getCompanyDetailsId() 
        return companyDetailsId;
    

    public void setCompanyDetailsId(Long companyDetailsId) 
        this.companyDetailsId = companyDetailsId;
    

    public String getCompanyName() 
        return companyName;
    

    public void setCompanyName(String companyName) 
        this.companyName = companyName;
    

    public Long getCompanyStrength() 
        return companyStrength;
    

    public void setCompanyStrength(Long companyStrength) 
        this.companyStrength = companyStrength;
    

    public String getCompanyLocation() 
        return companyLocation;
    

    public void setCompanyLocation(String companyLocation) 
        this.companyLocation = companyLocation;
    



**CustomerDetails Entity**

package in.india.entities;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name = "customer_details")
public class CustomerDetails 

    @Id
    @SequenceGenerator(name = "customer_details_seq", sequenceName = "customer_details_seq", initialValue = 1, allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "customer_details_seq")
    @Column(name = "customer_details_id")
    private Long customerDetailsId;
    @Column(name = "first_name ")
    private String firstName;
    @Column(name = "last_name ")
    private String lastName;
    @Column(name = "email_id")
    private String emailId;
    @Column(name = "profession")
    private String profession;
    @Column(name = "address")
    private String address;
    @Column(name = "age")
    private int age;
    public Long getCustomerDetailsId() 
        return customerDetailsId;
    

    public void setCustomerDetailsId(Long customerDetailsId) 
        this.customerDetailsId = customerDetailsId;
    

    public String getFirstName() 
        return firstName;
    

    public void setFirstName(String firstName) 
        this.firstName = firstName;
    

    public String getLastName() 
        return lastName;
    

    public void setLastName(String lastName) 
        this.lastName = lastName;
    

    public String getEmailId() 
        return emailId;
    

    public void setEmailId(String emailId) 
        this.emailId = emailId;
    

    public String getProfession() 
        return profession;
    

    public void setProfession(String profession) 
        this.profession = profession;
    

    public String getAddress() 
        return address;
    

    public void setAddress(String address) 
        this.address = address;
    

    public int getAge() 
        return age;
    

    public void setAge(int age) 
        this.age = age;
    

【讨论】:

【参考方案6】:

嗯。最后我用这种方式解决了我的问题:

首先:添加另一个 dataSource 和 sessionFactory。

第二:在hibernate的conf末尾添加一个bean到每个使用另一个sessionFactory的DAO(隐含另一个dataSource)。像这样:

<bean id="courseDAO" class="com.app.CourseDAOHibernate">
        <property name="sessionFactory" ref="sessionFactory2"/>
    </bean>

每个使用“sessionFactory”的 DAO 都会自动被 inyect,但如果其他 DAO 使用其他数据源,您应该像该示例一样显式地 inyect。

最后我不知道这是否是一个成功的解决方案,但这对我有用。

【讨论】:

【参考方案7】:

我也会添加我的示例。也许它对于另一种情况很有用,例如从一个数据库中获取数据并使用EntityManager 将其写入另一个数据库。

所以我的applicationContext.xml

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

<!-- Connection 1 -->

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

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

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
  <property name="url" value="jdbc:oracle:thin:@remote_host:1521:xe" />
  <property name="username" value="USER" />
  <property name="password" value="PASSWORD" />
</bean>


<!-- Connection 2 -->

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

<bean id="entityManagerFactoryLocal" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <property name="dataSource" ref="dataSourceLocal" />
  <property name="persistenceUnitName" value="unitLocal" />
  <property name="packagesToScan" value="local.business.domain" />
</bean>

<bean id="dataSourceLocal" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
  <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
  <property name="username" value="USER_LOC" />
  <property name="password" value="PASSWORD_LOC" />
</bean>

business.domain 具有用于远程 连接的实体类。

local.business.domain 具有本地连接的实体。

实体具有列和关系的注释映射。

然后是 2 个 DAO 类:

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class RemoteDao 

  @PersistenceContext(unitName="unitRemote")
  protected EntityManager em;

  // ...


import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional(value="transactionManagerLocal")
public class LocalDao 

  @PersistenceContext(unitName="unitLocal")
  protected EntityManager em;

  // ...


有了这个,我可以使用 @Autowired 在我的 JUnit 测试中注入 DAO:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations =  "/config/applicationContext.xml" )
public class DaoTest 

  @Autowired
  private RemoteDao remoteDao;

  @Autowired
  private LocalDao localDao;


  @Test
  public void daoTest()
    Entity entity = remoteDao.find(id);
    localDao.persist(entity);
  


因此,如果简单的应用程序、测试或数据库迁移脚本需要,则可以在没有服务的情况下使用 DAO。

【讨论】:

【参考方案8】:

我可以通过以下方式做到这一点。

2 个数据源、2 个 sessionFatory、2 个 transactionManger 和 2 个 tx:annotation-driven。以及上下文中的每个数据库 db 详细信息 需要默认使用的需要设置primary = true。 (在此oracleDataSource连接设置为默认且abcDataSource连接需要Transactional注解)

<bean id="oracleDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:env/jdbc/oracle"/>
</bean>

<bean id="oracleSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" primary="true">
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="oracleSessionFactory"/>
</bean>

<bean id="abcDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
  <property name="jndiName" value="java:env/abc/oracle"/>
</bean>

<bean id="abcSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  <property name="dataSource" ref="abcDataSource"/>
</bean>

<bean id="abcTransactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="abcSessionFactory"/>
</bean>

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

默认会话工厂的transactionManger 将默认可用(默认是由于primary = true),其他需要在Transactional 注释中指定


@Service("ABCRead")
@Transactional(value = "abcTransactionManager") 
public class ABCRead 
  @Autowired
  AbcImpl abcImpl;   // this will connect to abcSessionFactory

  @Autowired
  XYZImpl xyzImpl;  // This will connect to oracleSessionFactory

@Service
@Repository
@Transactional("abcTransactionManager")
public class AbcImpl 
  @Autowired
  private SessionFactory sessionFactory;

@Service
public class XYZImpl 
  @Autowired
  private SessionFactory sessionFactory;

【讨论】:

【参考方案9】:

我提供了另一种方法,它只适用于 java 类和注释。 在这个 SO 帖子中查看。 https://***.com/a/61232164/5681666

【讨论】:

以上是关于使用多个数据库休眠的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Spring 管理具有不同 jdbc(或休眠?)的多个“运行时注意到”数据库连接?

多个数据库的休眠自动增量字段

如何使用具有多个连接的 SQL 查询并使用休眠计数

休眠中的多个连接关联

使用休眠标准,有没有办法逃避特殊字符?

休眠:创建索引