Thursday, February 20, 2014

JBoss EAP 6 Persistence unit problem: Jboss specify the unitName for each reference to a persistence unit

Removed
When depolying my application on JBoss EAP 6.2, I had this error:

"
 Persistence unitName was not specified and there are 2 persistence unit definitions in application deployment deployment "myapp.war".  Either change the application deployment to have only one persistence unit definition or specify the unitName for each reference to a persistence unit.
"
My application uses JPA 2.1 with Hibernate implementation 4.3. My persistence.xml contains two persistence-unit, one per DB schema. See my JPA example.



NB: with Hibernate 4.3, the provider is org.hibernate.jpa.HibernatePersistenceProvider.
JBoss (eap 6.x, as 7.x) it implicitly load some modules. JPA will also be loaded by JBoss as it detects the persistence.xml. According to the dosumentation :

"During application deployment, JPA use is detected (e.g. persistence.xml or @PersistenceContext/Unit annotations) and injects Hibernate dependencies into the application deployment. This makes it easy to deploy JPA applications."


JBoss eap 6.2 comes also with JPA 2.0 and Hibernate 4.2.7. More details about the module version is avialable in this RedHat Link


In order to remove this error while deploying, and as I provide the required libs. I have edited the standalone.xml (Shutdown JBoss first) by removing the JPA subsystem subsystem xmlns="urn:jboss:domain:jpa:1.1".




When starting JBoss, this subsytem definition will be removed even from standalone.xml.

Now my application is deploying on JBoss.

Mmmmm I have found a better think. In fact, as I am defining two persistence-unit, every time that I use @PersistenceUnit or @PersistenceContext, I should specify the unitName, otherwhise the created entityManager will not know on which persistenceUnit it is working.
After a search in all my classes, I have found that really I have forgot to mention that. So I have restored the JPA subsystem in JBoss (standalone.xml). Now everytime that I use @Persistencexxx, I specify the  unitName.

@PersistenceContext(unitName="my_first_persitence_unit") 
@PersistenceUnit(unitName="my_second_persitence_unit") 





2 comments :

  1. So, what new JPA 2.1 methods exactly are u using in your code?

    I would like to use new JPA 2.1 methods in my code (defined in EclipseLink javax.persistence.api 2.1 package).
    I can configure compile-time dependencies and include the API library with my app.

    But JBoss still uses its own JPA 2.0 API JAR and thus I get NoSuchMethod errors in runtime.

    How exactly r u replacing JPA API in Jboss EAP 6.4 ?

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete

Articles les plus consultés