注释样式中使用Hibernate的多个数据库

时间:2012-03-26 14:54:30

标签: hibernate hibernate-mapping multiple-databases

网站上有关于此主题的多个讨论,但我正在使用注释来创建sessionFactory。

private SessionFactory sessionFactory;

@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;

这就是hibernate.cfg.xml的样子

<hibernate-configuration>
<session-factory>
    <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
    <property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.password">xxxxx</property>
    <property name="hibernate.connection.url">xxxxx</property>
    <property name="hibernate.connection.username">XXXX</property>
    <property name="hibernate.connection.characterEncoding">utf-8</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
    <property name="hibernate.format_sql">true</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.use_sql_comments">true</property>
    <mapping class="XXXX" />
</session-factory>
</hibernate-configuration>

这是applicationContext.xml

<!-- add tomcat datasource instance to springs context -->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/XXXXDS" />
    <property name="resourceRef" value="true" />
</bean>

在这种情况下,如何实现多个数据库集成。我遇到的一个解决方案是创建多个hibernate.cfg.xml并为其创建另一个会话工厂。

但我有两个疑问,1)我如何在自动装配中指出要选择哪个sessionFactory,2)我将不得不在hibernate.xml中复制大量信息,例如bean。对于这个问题,有没有更清洁的方法?

3 个答案:

答案 0 :(得分:2)

第一个问题:@Autowired with @Qualifier(“name”)将帮助您选择哪个sessionFatory。

@Autowired
@Qualifier("sessionFactoryName")
public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
...
}

第二个问题:您可以设置hibernate属性:schema和catalog,以便在处理多个数据库时指定数据库。我有MS SQL Server的经验,所以在实体中我添加了注释@Table(“[database]。[schema]。[tablename]”)并且它映射到多个数据库中的特定表。

答案 1 :(得分:1)

张贴参考评论。

您的会话工厂:

<bean id="mySessionFactory1" class="...">
    ...
</bean>
<bean id="mySessionFactory2" class="...">
    ...
</bean>

你的DAO:

<bean id="myHibernateDao1" abstract="true" p:sessionFactory-ref="mySessionFactory1" />
<bean id="myHibernateDao2" abstract="true" p:sessionFactory-ref="mySessionFactory2" />

<bean id="myObj1Dao" parent="myHibernateDao1" class="..." />
<bean id="myObj2Dao" parent="myHibernateDao1" class="..." />
<bean id="myObj3Dao" parent="myHibernateDao1" class="..." />
<bean id="myObj4Dao" parent="myHibernateDao2" class="..." />
<bean id="myObj5Dao" parent="myHibernateDao2" class="..." />

答案 2 :(得分:0)

import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;

@Configuration
@ComponentScan({ "component.name" })
@PropertySources(@PropertySource(value = {"file:/file1.properties", "classpath:file2.properties"}))
public class HibernateConfiguration {

    @Bean
    public LocalSessionFactoryBean firstSessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(firstDataSource());
        sessionFactory.setPackagesToScan(new String[] { "com.test" });
        sessionFactory.setHibernateProperties(hibernateProperties());
        return sessionFactory;
     }

    @Bean
    public DataSource firstDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("DRIVER_NAME");
        dataSource.setUrl("DATABASE_URL");
        dataSource.setUsername("DATABASE_USER");
        dataSource.setPassword("DATABASE_PASS");
        return dataSource;
    }

    @Bean
    public LocalSessionFactoryBean secondSessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(secondDataSource());
        sessionFactory.setPackagesToScan(new String[] { "com.test" });
        sessionFactory.setHibernateProperties(hibernateProperties());
        return sessionFactory;
     }

    @Bean
    public DataSource secondDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("DRIVER_NAME");
        dataSource.setUrl("DATABASE_URL");
        dataSource.setUsername("DATABASE_USER");
        dataSource.setPassword("DATABASE_PASS");
        return dataSource;
    }


    private Properties hibernateProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", org.hibernate.dialect.DB2Dialect);
        properties.put("hibernate.show_sql", true);
        properties.put("hibernate.format_sql", true);
        properties.put("hibernate.default_schema", SCHEMA_NAME);
        return properties;        
    }

    @Bean
    public HibernateTransactionManager firstTransactionManager() {
       HibernateTransactionManager txManager = new HibernateTransactionManager();
       txManager.setSessionFactory(firstSessionFactory().getObject());
       return txManager;
    }

    @Bean
    public HibernateTransactionManager rdsaTransactionManager() {
       HibernateTransactionManager txManager = new HibernateTransactionManager();
       txManager.setSessionFactory(rdsaSessionFactory().getObject());
       return txManager;
    }


}