我正在执行从源数据库读取行的任务,如果目标数据库中存在相同的记录,则合并目标数据库中的行。我检查了两个entityManager对象的数据源,它们看起来很好。
我正在为各个源和目标数据库创建一个名为primary和secondary的bean ::
@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;
}
和
@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;
}
由于JPA需要在 persistance.xml 中定义一个数据源,因此我指定了相同的数据源。但仍无法合并目标数据库中的记录。 如果我错过了什么,请建议