Skip to content

[OPENJPA-3102] Use the BeanManager passed in jakarta.persistence.bean.manager for CDI entity listeners - #328

Draft
rzo1 wants to merge 1 commit into
masterfrom
OPENJPA-3102
Draft

rzo1 wants to merge 1 commit into
masterfrom
OPENJPA-3102

Conversation

@rzo1

@rzo1 rzo1 commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

https://issues.apache.org/jira/browse/OPENJPA-3102

Jakarta Persistence 3.2 (section 9.1) says the container passes its BeanManager to createContainerEntityManagerFactory in the map under jakarta.persistence.bean.manager, and sections 3.6.1 (entity listeners) and 3.9 (attribute converters) both start with "Obtain a BeanManager instance. (See Section 9.1)". OpenJPA ignored this key: the CDI entity listener factory from OPENJPA-3089 always looked up the BeanManager in JNDI (java:comp/BeanManager, java:comp/env/BeanManager) and then via CDI.current(). TomEE passes the property (as a lazy proxy, because the EMF is often created before CDI is started for the application).

Cause:

  • jakarta.persistence.bean.manager was not a known property, so the configuration dropped it with an "unknown property" warning.
  • EntityListenerCDIFactory only had the JNDI and CDI.current() lookup.
  • Once the property is registered, setting it called equals and toString on the passed object while the EMF was created (ObjectValue.set, Value.isDefault). On TomEE's lazy proxy each call resolves the real BeanManager, which is too early.

Changes:

  • JPAProperties.BEAN_MANAGER is added and PersistenceProductDerivation registers it as an ObjectValue, like jakarta.persistence.validation.factory. OpenJPAConfiguration gets getBeanManagerInstance() and setBeanManager(Object). The value is kept as a plain Object, so the configuration does not depend on the CDI API.
  • EntityListenerCDIFactory uses the passed object if it is set (only checked with instanceof BeanManager, nothing is called on it before the first listener is created), and the old JNDI and CDI.current() lookup otherwise. It is still loaded by class name, with the fallback to EntityListenerCachedFactory.
  • If a BeanManager was passed but the CDI factory cannot be used (e.g. the object is not a BeanManager), OpenJPA logs a warning with the reason instead of only a trace message.
  • ObjectValue.set and Value.isDefault no longer call methods on a newly set object. The results are unchanged.
  • CDI injection for listeners is still only used with openjpa.EntityListenerFactory=cdi; applications that do not set it, or do not pass the property, behave as before.
  • The manual's CDI section documents the property and the fallback.

Note: a String value for jakarta.persistence.bean.manager (e.g. in persistence.xml) now fails EMF creation instead of being ignored, the same as for jakarta.persistence.validation.factory. A string can never be a BeanManager.

Tests (Derby): new TestBeanManagerPropertyCDIEntityListeners (OpenWebBeans @Cdi) passes a proxy BeanManager that forwards to the OWB one and counts calls. It checks that the proxy is not called before the first persist and is then used for injection, that the configuration returns the passed object, that the default stateless factory never calls it, and that an object that is not a BeanManager leads to a warning. It fails without the fix. The existing CDI listener tests cover the lookup without the property. The openjpa-lib, openjpa-kernel and openjpa-persistence suites and the full openjpa-persistence-jdbc suite pass.

@rzo1
rzo1 marked this pull request as ready for review November 28, 2026 17:29
@cristof

cristof commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

I have just added this feature to OPENJPA-3101, but didn't opened a PR because I'm refactoring the code to make it simpler. If you may, I'd ask to hold a bit because I'm sending soon the new version there incorporating what I may have missed and you put here.

@rzo1
rzo1 marked this pull request as draft November 28, 2026 20:10
@rzo1

rzo1 commented Nov 28, 2026

Copy link
Copy Markdown
Contributor Author

Feel free to close it once the combination is up.

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