JPA persist , detach, merge creates two records in DB when executed in the same transaction in JBoss

Solution Unverified - Updated -

Issue

We are having problems using JPA in CMT.

Transaction is started from an EJB, and the following code is called to persist records:

EntityManager em = factory.createEntityManager();
try {
     if (serviceRequest.getDbId() == null){
         em.persist(serviceRequest);
     } else {
         em.merge(serviceRequest);
     }
} 
finally {
     em.close();
}

The code is called twice, the first time with a new object, which will execute "em.persist(serviceRequest);", and then a second time after the serviceRequest object is modified, which will execute "em.merge(serviceRequest);". Both these calls are in the same JTA transaction.
The result is that there are 2 records created in the DB, both containing the modified serverRequest. I was expecting only one record.

I attach the datasource and persistence.xml used.

EntityManager is created programmatically in the DAO like this:

entityManagerProps = new HashMap<String, String>();
entityManagerProps.put("hibernate.connection.datasource", "java:TestDS");
entityManagerProps.put("hibernate.default_schema", "TestDSSchema");
factory = Persistence.createEntityManagerFactory("TestPersist", entityManagerProps);

Environment

  • JBoss Enterprise Application Platform (EAP) 4.3.0_CP09

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase of over 48,000 articles and solutions.

Current Customers and Partners

Log in for full access

Log In
Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.