jakartaee / jakartaee/platform

Address duplicate generated EntityManagerFactory bean name deployment failures when `Injecting an Entity Manager Factory using CDI`

Open
#1,135 5 comments 0 reactions 1 assignee View on GitHub

@scottmarlow is already working on this.

Since Aug 8, 2025.

EE12
Dominant language
No language data
Stars
230
Forks
77
Avg merge
8d 5h
Merged PRs (30d)
1

Description

As per https://jakarta.ee/specifications/platform/11/jakarta-platform-spec-11.0#injecting-an-entity-manager-factory-using-cdi:

>
> 13.12.2. Injecting an Entity Manager Factory using CDI
>
> A Jakarta EE container must feature built-in integration of Jakarta Persistence with the CDI bean manager, allowing injection of a container-managed entity manager using the annotation jakarta.inject.Inject.
>
> For each persistence unit, the container must make available a bean with:
>
> - bean type EntityManagerFactory,
>
> - the qualifiers specified by qualifier XML elements in persistence.xml, or jakarta.enterprise.inject.Default, if no qualifiers are explicitly specified,
>
> - scope jakarta.enterprise.context.ApplicationScoped,
>
> - bean name given by the name of the persistence unit,
>
> - no interceptor bindings,
>
> - a bean implementation which satisfies the requirements of the Persistence specification for a container-managed entity manager factory.
>

**Is your feature request related to a problem? Please describe.**

EE implementations may create a single CDI BeanManager instance per application deployment to be used globally. EE implementations may also use other mappings such as an CDI BeanManager instance per deployment module which wouldn't get a duplicate bean error when there are duplicate persistence unit definitions.

For reference https://issues.redhat.com/browse/WFLY-19554 contains some background information about problems that can happen with existing application deployments that contain duplicate persistence unit definitions which are currently allowed by the Jakarta EE 11 specification.

In summary, think about an EAR deployment containing two subdeployments that each contain persistence definition:
```


Persistence Unit.
java:jboss/datasources/ExampleDS




```

I think that some application servers may try to create two EntityManagerFactory CDI beans named `mainPu` (once per subdeployment that contains ^ persistence.xml). As is mentioned in https://issues.redhat.com/browse/WFLY-19554, I'm seeing a deployment failure `[WELD-001414](https://issues.redhat.com/browse/WELD-1414): Bean name is ambiguous. Name mainPu resolves to beans: [Configurator Bean [interface jakarta.persistence.EntityManagerFactory, types: Object, EntityManagerFactory, AutoCloseable, qualifiers: @Any @Default], Configurator Bean [interface jakarta.persistence.EntityManagerFactory, types: Object, EntityManagerFactory, AutoCloseable, qualifiers: @Any @Default]]"}}}}`

**Describe the solution you'd like**
Clarify that the EntityManagerFactory bean may not be named when there are duplicate persistence unit definitions in an application.

**Describe alternatives you've considered**
Specify a persistence unit hint that when used will ensure that the EntityManagerFactory CDI Bean name includes the containing module archive file name (e.g something like `CDIPersistenceSchemaManagerTestCase.jar.pu2`).

**Additional context**

Current test failures seen with WildFly:

```
[INFO] Running org.jboss.as.test.integration.jpa.packaging.PersistenceUnitWarPackagingTestCase
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 5.483 s <<< FAILURE! – in org.jboss.as.test.integration.jpa.packaging.PersistenceUnitWarPackagingTestCase
[ERROR] org.jboss.as.test.integration.jpa.packaging.PersistenceUnitWarPackagingTestCase – Time elapsed: 5.481 s <<< ERROR!
org.jboss.arquillian.container.spi.client.container.DeploymentException:
Cannot deploy scopedToEar.ear: {"WFLYCTL0062: Composite operation failed and was rolled back. Steps that failed:" => {"Operation step-1" => {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"scopedToEar.ear\".WeldStartService" => "Failed to start service
Caused by: org.jboss.weld.exceptions.DeploymentException: [WELD-001414](https://issues.redhat.com/browse/WELD-1414): Bean name is ambiguous. Name mainPu resolves to beans: [Configurator Bean [interface jakarta.persistence.EntityManagerFactory, types: Object, EntityManagerFactory, AutoCloseable, qualifiers: @Any @Default], Configurator Bean [interface jakarta.persistence.EntityManagerFactory, types: Object, EntityManagerFactory, AutoCloseable, qualifiers: @Any @Default]]"}}}}

[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 4.623 s <<< FAILURE! – in org.jboss.as.test.integration.jpa.packaging.PersistenceUnitPackagingTestCase
Caused by: org.jboss.weld.exceptions.DeploymentException: [WELD-001414](https://issues.redhat.com/browse/WELD-1414): Bean name is ambiguous. Name mainPu resolves to beans: [Configurator Bean [interface jakarta.persistence.EntityManagerFactory, types: Object, EntityManagerFactory, AutoCloseable, qualifiers: @Any @Default], Configurator Bean [interface
```

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.