简体   繁体   中英

How to have more than one persistence units in an JPA 2 application?

My system:

Eclipse Oxygen/JPA 2/JSF 2.2/Hibernate 4/JBoss AS 7

My condition:

I have an application with two persistence units (PU) declared in persistence.xml .

My JBoss error:

Caused by: java.lang.IllegalArgumentException: JBAS011470: Persistence unitName was not specified and there are 2 persistence unit definitions in application deployment "test.war".  Either change the application to have only one persistence unit definition or specify the unitName for each reference to a persistence unit.
    at org.jboss.as.jpa.container.PersistenceUnitSearch.resolvePersistenceUnitSupplier(PersistenceUnitSearch.java:69)
    at org.jboss.as.jpa.processor.JPAAnnotationParseProcessor.getPersistenceUnit(JPAAnnotationParseProcessor.java:284)
    at org.jboss.as.jpa.processor.JPAAnnotationParseProcessor.getBindingSource(JPAAnnotationParseProcessor.java:220)
    at org.jboss.as.jpa.processor.JPAAnnotationParseProcessor.processField(JPAAnnotationParseProcessor.java:151)
    at org.jboss.as.jpa.processor.JPAAnnotationParseProcessor.processPersistenceAnnotations(JPAAnnotationParseProcessor.java:118)
    at org.jboss.as.jpa.processor.JPAAnnotationParseProcessor.deploy(JPAAnnotationParseProcessor.java:90)
    at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:113) [jboss-as-server-7.1.0.Final.jar:7.1.0.Final]
    ... 5 more

My problem:

I use a framework that hides all the details on the EntityManager lifecycle. This framework gives me an ancestor class and I build all my code in a subclass, not caring about managing the EntityManager.

This ancestor class does not inject ou annotate the EntityManager, it is created by code when needed for the first time, but the exception above is thrown by JBoss during the application start when I have more than one PU.

I wrote a code in the ancestor to accept the @PersistenceUnit annotation in my subclass and to use the name set in the annotation when creating the EntityManagerFactory. When no annotation is used, the code finds out the first PU name and used it. So, the first PU existing in persistence.xml is understood as a default PU name.

However, even not injecting any EntityManagers, I still have the above exception.

What is missing in my solution?

If you have more than one persistence unit and use @PersistenceContext / @PersistenceUnit annotations, you must specify your unit name for the annotation to be unambiguous.

So instead of:

@PersistenceContext
private EntityManager manager;

you must use:

@PersistenceContext(unitName = "<unit name in persistence.xml>")
private EntityManager manager;

And instead of:

@PersistenceUnit
private EntityManagerFactory managerFactory;

you must use:

@PersistenceUnit(unitName = "<unit name in persistence.xml>")
private EntityManagerFactory managerFactory;

What the error message tells you is that the deployer has found at least one occurrence of @PersistenceContext / @PersistenceUnit without specifying a persistence unit name. That is ambiguous.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM