I want to use hibernate to connect two different databases and use different sessionfactory to operate the data
In the example below , Another datasource and LocalSessionFactoryBean will be added in the @Configuration class , but how do I give these two sessionFactory specific names?
Reference:
https://www.baeldung.com/hibernate-5-spring
@Configuration
@EnableTransactionManagement
public class HibernateConf {
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(
{"com.baeldung.hibernate.bootstrap.model" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUrl("jdbc:h2:mem:db;DB_CLOSE_DELAY=-1");
dataSource.setUsername("sa");
dataSource.setPassword("sa");
return dataSource;
}
@Bean
public PlatformTransactionManager hibernateTransactionManager() {
HibernateTransactionManager transactionManager
= new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory().getObject());
return transactionManager;
}
private final Properties hibernateProperties() {
Properties hibernateProperties = new Properties();
hibernateProperties.setProperty(
"hibernate.hbm2ddl.auto", "create-drop");
hibernateProperties.setProperty(
"hibernate.dialect", "org.hibernate.dialect.H2Dialect");
return hibernateProperties;
}
}
Want to reach the effect:
@Service
@Transactional
public class TestRepositoryImpl implements TestRepository{
@Qualifier("connectionA")
@Autowired
private SessionFactory sessionFactory;
@Qualifier("connectionB")
@Autowired
private SessionFactory sessionFactory;
...
...
}
Another project example:
@Configuration
public class DataSourceConfig {
@Primary
@Bean(name = "test1DataSource")
@ConfigurationProperties(prefix = "spring.datasource.test1")
public DataSource test1DataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "test2DataSource")
@ConfigurationProperties(prefix = "spring.datasource.test2")
public DataSource test2DataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Autowired
@Bean(name = "test1Connection")
public NamedParameterJdbcTemplate test1JdbcTemplate(@Qualifier("test1DataSource") DataSource dataSource) {
return new NamedParameterJdbcTemplate(dataSource);
}
@Autowired
@Bean(name ="test2Connection")
public NamedParameterJdbcTemplate test2JdbcTemplate(@Qualifier("test2DataSource") DataSource dataSource) {
return new NamedParameterJdbcTemplate(dataSource);
}
}
Any help is greatly appreciated!
CodePudding user response:
If you want to achieve hibernate with two Database: use session factory like below.
But i recommend use Spring Data JPA it will give more specification and easier to understand.
public class Transfer {
public static void main(String[] args) {
// get the SessionFacotry
SessionFactory connectionAFactory = HibernateConf.getSessionFactory();
SessionFactory connectionBFactory= DataSourceConfig.getSessionFactory();
// get The Session
Session connectionASes = HibernateConf.getSession();
Session connectionBSes = DataSourceConfig.getSession();
CodePudding user response:
For this you can configure your data connection beans like this:
@Configuration
public class DataSourceConfig {
@Primary
@Bean(name = "test1DataSource")
@ConfigurationProperties(prefix = "spring.datasource.test1")
public DataSource test1DataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "test2DataSource")
@ConfigurationProperties(prefix = "spring.datasource.test2")
public DataSource test2DataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Autowired
@Bean(name = "test1Connection")
public NamedParameterJdbcTemplate test1JdbcTemplate(@Qualifier("test1DataSource") DataSource dataSource) {
return new NamedParameterJdbcTemplate(dataSource);
}
@Autowired
@Bean(name ="test2Connection")
public NamedParameterJdbcTemplate test2JdbcTemplate(@Qualifier("test2DataSource") DataSource dataSource) {
return new NamedParameterJdbcTemplate(dataSource);
}
}
After that you can autowire them separately by using @Qualifier in following way in your service class:
@Service
@Transactional
public class TestRepositoryImpl implements TestRepository{
@Qualifier("test1Connection")
@Autowired
private SessionFactory sessionFactory1;
@Qualifier("test2Connection")
@Autowired
private SessionFactory sessionFactory2;
...
...
}