I have a Spring application that connects to two databases at the same time. So I have for this two LocalSessionFactoryBean instances for each connection like this:
@Bean
public LocalSessionFactoryBean firstSessionFactory() {
final LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean();
lsfb.setPackagesToScan("ro.mycompany.myproject.classes");
lsfb.setDataSource(dataSourceOne);
lsfb.setEntityInterceptor(auditInterceptor1);
lsfb.setHibernateProperties(getHibernateProperties1());
return lsfb;
}
@Bean
public LocalSessionFactoryBean secondSessionFactory() {
final LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean();
lsfb.setPackagesToScan("ro.mycompany.myproject.classes2");
lsfb.setDataSource(dataSourceTwo);
lsfb.setEntityInterceptor(auditInterceptor2);
lsfb.setHibernateProperties(getHibernateProperties2());
return lsfb;
}
For the DAO layer I have a class that injects the SessionFactory object like this.
public class GenericDAOImpl extends HibernateDAOSupport implements GenericDAO {
@Autowired
private SessionFactory sessionFactory;
//Other methods goes here
}
I instantiate the beans in my config file like this:
@Bean
public GenericDAO firstGenericDAO() {
final GenericDAOImpl genericDAO = new GenericDAOImpl();
return genericDAO;
}
@Bean
public GenericDAO secondGenericDAO() {
final GenericDAOImpl genericDAO = new GenericDAOImpl();
return genericDAO;
}
How can I make the firstGenericDAO to use firstSessionFactory and secondGenericDAO to use secondSessionFactory without creating the setters method? I want to use both connection at the same time so also Spring profiles won't help me. Thank you