2

We are configuring Hibernate using java, here is our code to configure hibernate.

@Configuration
@EnableTransactionManagement
@ComponentScan({ "org.npcc.ccms.config" })
@PropertySource(value = { "classpath:application.properties" })
public class HibernateConfiguration {
    final static Logger logger = LogManager.getLogger(HibernateConfiguration.class);

    @Autowired
    private Environment environment;

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan(new String[] { "org.npcc.ccms.model" });
        sessionFactory.setHibernateProperties(hibernateProperties());
        return sessionFactory;
     }

    @Bean(destroyMethod="")
    public DataSource dataSource() {
        JndiTemplate jndi = new JndiTemplate();
        DataSource dataSource = null;
        try {
            dataSource = (DataSource) jndi.lookup(environment.getRequiredProperty("datasource"));
        } catch (NamingException e) {
            logger.error("NamingException for java:comp/env/jdbc/ccms_cp1_orcl", e);
        }
        return dataSource;
    }

    private Properties hibernateProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
        properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
        properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
        return properties;        
    }

    @Bean
    @Autowired
    public HibernateTransactionManager transactionManager(SessionFactory s) {
       HibernateTransactionManager txManager = new HibernateTransactionManager();
       txManager.setSessionFactory(s);
       return txManager;
    }
}

My question is how can configure another datasource within same hibernate configuration class? I found solution here but using xml configuration, how this works using java configuration? Thanks in advance.

Community
  • 1
  • 1
Ramu
  • 199
  • 2
  • 11

1 Answers1

2

You will need two different beans annotated as follows:

@Bean(name="SessionFactory")
public SessionFactory sessionFactory() {

}

and:

@Bean(name="OtherSessionFactory")
public SessionFactory otherSessionFactory() {

}

And two datasources configured appropriately.

Then when you want to use the other SessionFactory you just need:

@Autowired
@Qualifier("SessionFactory")
SessionFactory sessionFactory

or

@Autowired
@Qualifier("OtherSessionFactory")
SessionFactory sessionFactory
Alex Barnes
  • 7,174
  • 1
  • 30
  • 50
  • Thanks for you response Alex, i am going to create separate configuration classes for each datasource and separate Transaction Managers with Propagation.REQUIRED in my service. – Ramu Sep 21 '15 at 04:41
  • OK. If you have multiple beans of the same type you'll need to use @Qualifier regardless. – Alex Barnes Sep 21 '15 at 04:42
  • How to generalize base DAO class? @Autowired @Qualifier("SessionFactory") private SessionFactory sessionFactory; public T getByKey(PK key) { return (T) getSession().get(persistentClass, key); } – Ramu Sep 21 '15 at 04:49
  • If you're using @Qualifier I think this is the correct answer ;-) – Alex Barnes Sep 21 '15 at 04:55
  • How can we write GenericDAO class with multiple session factories?? Say i have GenericDao class with delete() method which deletes record on session but how to generalize this across other databases. Which mean do we have to create GenericDAO per SessionFactory???? Please advise – Ramu Sep 21 '15 at 22:28