I am performing a task of reading a row from a source database and if same record exists in target database then merging the row in target database. I have checked the datasource's of both entityManager objects and they look fine.
I am creating a bean by name primary and secondary for respective source and target databases ::
@Bean(name = "primary")
@Primary
public LocalContainerEntityManagerFactoryBean entityManager() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setPersistenceUnitName("primary");
em.setPersistenceXmlLocation("classpath:./META-INF/persistence.xml");
// PropertyPlaceholderConfigurer
// configurer=appConfig.getPropertyPlaceholderConfigurer();
em.setDataSource(dataSource());
HashMap<String, Object> properties = new HashMap<String, Object>();
properties.put("hibernate.hbm2ddl.auto", hibernateHbm2ddlAuto);
properties.put("hibernate.dialect", hibernateDialect);
properties.put("hibernate.show_sql", true);
em.setJpaPropertyMap(properties);
em.setPackagesToScan(new String[] { "org.x.y" });
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
return em;
}
and
@Bean(name = "secondary")
public LocalContainerEntityManagerFactoryBean targetEntityManager() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setPersistenceUnitName("secondary");
em.setPersistenceXmlLocation("classpath:./META-INF/persistence.xml");
em.setDataSource(targetDataSource());
// PropertyPlaceholderConfigurer
// configurer=appConfig.getPropertyPlaceholderConfigurer();
em.setPackagesToScan(new String[] { "org.x.y" });
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
HashMap<String, Object> properties = new HashMap<String, Object>();
properties.put("hibernate.hbm2ddl.auto", hibernateHbm2ddlAuto);
properties.put("hibernate.dialect", hibernateDialect);
properties.put("hibernate.show_sql", true);
em.setJpaPropertyMap(properties);
return em;
}
As JPA requires one datasource to be defined in persistance.xml i have specified the same datasource's. But still unable to merge the record in target database. Please suggest if i am missing something