Skip to content

Conversation

@gavinking
Copy link
Member

Refinement of what was stated in #423 to reflect what we're actually doing with this in practice. Basically, say that the dataSource can just be the name of a persistence unit.

@gavinking gavinking requested a review from njr-11 December 5, 2025 23:56
to reflect what we're actually doing with this in practice
@gavinking gavinking force-pushed the repository-datasource branch from f30392e to 4946f8a Compare December 6, 2025 00:07
@otaviojava
Copy link
Contributor

@njr-11 could you review this one?

* {@code PersistenceUnit} annotation. Or, if the value does begin with
* {@code java:} and matches the name of a persistence unit reference,
* then the corresponding {@code EntityManagerFactory} obtained from JNDI
* is used.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remembered why I didn't write this with the PersistenceUnit name in the first place.
I'm not aware of an API or SPI that Jakarta Persistence provides to do this given the name of a persistence unit:

     * then the container-managed {@code EntityManagerFactory} for that
     * persistence unit is used, obtained as if it had been injected using the
     * {@code PersistenceUnit} annotation.

The closest I see is Persistence.createEntityManagerFactory(persistenceUnitName), but that is only for Java SE and also not container managed. It's also possible that there is something in Jakarta Persistence that I am overlooking.

I would also be fine with including some language for Java SE usage here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PersistenceContext and @PersistenceUnit both inject based on the unit name.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, the Javadoc is super-confusing:

    /**
     * (Optional) The name of the persistence unit as defined
     * in the {@code persistence.xml} file. If specified, the
     * persistence unit for the entity manager factory that is
     * accessible in JNDI must have the same name.
     */

I don't know what that second sentence means. But I'm basing my assertion on the first sentence:

The name of the persistence unit as defined in the persistence.xml file

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PersistenceContext and @PersistenceUnit both inject based on the unit name.

Right, but we have written the Data spec not to require a compile time step that generates code. I think we would need an equivalent to Persistence.createEntityManagerFactory(persistenceUnitName) for Jakarta EE usage to ensure it is always implementable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants