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.

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

5 Comments

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.
OK. If you have multiple beans of the same type you'll need to use @Qualifier regardless.
How to generalize base DAO class? @Autowired @Qualifier("SessionFactory") private SessionFactory sessionFactory; public T getByKey(PK key) { return (T) getSession().get(persistentClass, key); }
If you're using @Qualifier I think this is the correct answer ;-)
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.