0

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

Anand Kadhi
  • 1,790
  • 4
  • 27
  • 40
  • 1
    You cannot simply copy from one to another entity. You would have to create a new instance, copy all values (if you have nested objects copy those as well). You cannot simply read entity1 from source and save entity1 in target. That isn't how JPA works. Also `persistence.xml` you can drop that when configuring it with Spring. Also without transactions nothing will be persisted... – M. Deinum May 15 '17 at 12:35
  • @M.Deinum : Thanks I have transactions in place but if i am creating a new instance and copy values along with primary identifier then still facing the same problem. Did you mean by new instance shouldn't i copy primary key values – Anand Kadhi May 15 '17 at 12:47
  • If you can copy identifiers or not depends on how you specified them. Hibernate will also use those to determine if it is a new one or not. – M. Deinum May 15 '17 at 13:01
  • Yeah when I am calling to persist() passing an entity which is not in primary database then it throws "**PersistentObjectException**: detached entity passed to persist" And when i am calling merge internally it calls "select SEQ.nextval from dual" and gets next value of primary key in target database and after merging nothing is reflected in target database – Anand Kadhi May 15 '17 at 13:15

0 Answers0