This is an automated email from the ASF dual-hosted git repository. pcristof pushed a commit to branch OPENJPA-2940 in repository https://gitbox.apache.org/repos/asf/openjpa.git
The following commit(s) were added to refs/heads/OPENJPA-2940 by this push: new 44c5733bd [OPENJPA-2940][WIP] Adding support for scope and qualifiers in persistence.xml 44c5733bd is described below commit 44c5733bd5137b4f60075d6393190b097bc47051 Author: Paulo Cristovão de Araújo Silva Filho <pcris...@gmail.com> AuthorDate: Wed Aug 13 22:13:25 2025 -0300 [OPENJPA-2940][WIP] Adding support for scope and qualifiers in persistence.xml * Added tests of specifications supported * Added support for scope and qualifiers in PersistenceUnitInfo and persistence.xml --- .../conf/TestSpecificationConfiguration.java | 55 ++++++++++++++++++++++ .../puconf/TestPersistenceUnitConfig.java | 30 ++++++++++++ .../conf/META-INF/persistence-3_0-config.xml | 29 ++++++++++++ .../conf/META-INF/persistence-3_1-config.xml | 29 ++++++++++++ .../conf/META-INF/persistence-3_2-config.xml | 29 ++++++++++++ .../persistence/puconf/META-INF/persistence.xml | 16 +++++-- .../persistence/PersistenceProductDerivation.java | 17 +++++-- .../persistence/PersistenceUnitInfoImpl.java | 20 +++++++- 8 files changed, 216 insertions(+), 9 deletions(-) diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/conf/TestSpecificationConfiguration.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/conf/TestSpecificationConfiguration.java index 0f7ceae23..294160d09 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/conf/TestSpecificationConfiguration.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/conf/TestSpecificationConfiguration.java @@ -71,6 +71,61 @@ public class TestSpecificationConfiguration extends SingleEMFTestCase { } } + public void testSpecificationVersionIsJPA3_0() { + OpenJPAEntityManagerFactorySPI emf1 = null; + try { + emf1 = + (OpenJPAEntityManagerFactorySPI)OpenJPAPersistence. + createEntityManagerFactory("persistence3_0", "org/apache/openjpa/conf/META-INF/persistence-3_0-config.xml"); + + Specification spec = emf1.getConfiguration().getSpecificationInstance(); + int major = spec.getVersion(); + assertEquals(3, major); + assertEquals("0", spec.getMinorVersion()); + assertTrue(spec.isSame("JPA")); + } finally { + clear(emf1); + closeEMF(emf1); + } + } + + public void testSpecificationVersionIsJPA3_1() { + + OpenJPAEntityManagerFactorySPI emf1 = null; + try { + emf1 = + (OpenJPAEntityManagerFactorySPI)OpenJPAPersistence. + createEntityManagerFactory("persistence3_1", "org/apache/openjpa/conf/META-INF/persistence-3_1-config.xml"); + + Specification spec = emf1.getConfiguration().getSpecificationInstance(); + int major = spec.getVersion(); + assertEquals(3, major); + assertEquals("0", spec.getMinorVersion()); + assertTrue(spec.isSame("JPA")); + } finally { + clear(emf1); + closeEMF(emf1); + } + } + + public void testSpecificationVersionIsJPA3_2() { + OpenJPAEntityManagerFactorySPI emf1 = null; + try { + emf1 = + (OpenJPAEntityManagerFactorySPI)OpenJPAPersistence. + createEntityManagerFactory("persistence3_2", "org/apache/openjpa/conf/META-INF/persistence-3_2-config.xml"); + + Specification spec = emf1.getConfiguration().getSpecificationInstance(); + int major = spec.getVersion(); + assertEquals(3, major); + assertEquals("2", spec.getMinorVersion()); + assertTrue(spec.isSame("JPA")); + } finally { + clear(emf1); + closeEMF(emf1); + } + } + public void testLowerVersionCanBeSet() { super.setUp("openjpa.Specification", "JPA 1.0"); Specification spec = getSpecifcation(); diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/puconf/TestPersistenceUnitConfig.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/puconf/TestPersistenceUnitConfig.java index e6f478ea0..33eea5f6b 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/puconf/TestPersistenceUnitConfig.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/puconf/TestPersistenceUnitConfig.java @@ -106,5 +106,35 @@ public class TestPersistenceUnitConfig extends PersistenceTestCase { } } } + + public void testCreateEMFWithCustomInjectScope() { + EntityManagerFactory emf = null; + try { + emf = createEmf("PUTest-New-Scope"); + } catch (Throwable t) { + fail(t.getMessage()); + } finally { + if (emf != null) { + try { + emf.close(); + } catch (Throwable e) {} + } + } + } + + public void testCreateEMFWithCustomQualifiers() { + EntityManagerFactory emf = null; + try { + emf = createEmf("PUTest-Qualifiers"); + } catch (Throwable t) { + fail(t.getMessage()); + } finally { + if (emf != null) { + try { + emf.close(); + } catch (Throwable e) {} + } + } + } } diff --git a/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/conf/META-INF/persistence-3_0-config.xml b/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/conf/META-INF/persistence-3_0-config.xml new file mode 100644 index 000000000..689756dc1 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/conf/META-INF/persistence-3_0-config.xml @@ -0,0 +1,29 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +--> +<persistence xmlns="https://jakarta.ee/xml/ns/persistence" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + version="3.0"> + + <persistence-unit name="persistence3_0"> + <description>PU for testing JPA 3.0 spec level</description> + </persistence-unit> + + +</persistence> diff --git a/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/conf/META-INF/persistence-3_1-config.xml b/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/conf/META-INF/persistence-3_1-config.xml new file mode 100644 index 000000000..98e319a95 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/conf/META-INF/persistence-3_1-config.xml @@ -0,0 +1,29 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +--> +<persistence xmlns="https://jakarta.ee/xml/ns/persistence" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + version="3.0"> + + <persistence-unit name="persistence3_1"> + <description>PU for testing JPA 3.1 spec level</description> + </persistence-unit> + + +</persistence> diff --git a/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/conf/META-INF/persistence-3_2-config.xml b/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/conf/META-INF/persistence-3_2-config.xml new file mode 100644 index 000000000..006652b28 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/conf/META-INF/persistence-3_2-config.xml @@ -0,0 +1,29 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +--> +<persistence xmlns="https://jakarta.ee/xml/ns/persistence" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + version="3.2"> + + <persistence-unit name="persistence3_2"> + <description>PU for testing JPA 3.2 spec level</description> + </persistence-unit> + + +</persistence> diff --git a/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/puconf/META-INF/persistence.xml b/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/puconf/META-INF/persistence.xml index c8447338a..bb3fa5245 100644 --- a/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/puconf/META-INF/persistence.xml +++ b/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/puconf/META-INF/persistence.xml @@ -16,10 +16,9 @@ specific language governing permissions and limitations under the License. --> -<persistence xmlns="http://java.sun.com/xml/ns/persistence" +<persistence xmlns="https://jakarta.ee/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://java.sun.com/xml/ns/persistence - http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> + version="3.2"> <persistence-unit name="PUTest-Good" transaction-type="RESOURCE_LOCAL"> <class>org.apache.openjpa.persistence.common.apps.Address</class> @@ -48,4 +47,15 @@ <class>org.apache.openjpa.persistence.common.apps.Address</class> <class>org.apache.openjpa.persistence.common.apps.CompUser</class> </persistence-unit> + <persistence-unit name="PUTest-New-Scope" transaction-type="RESOURCE_LOCAL"> + <scope>som.ecustom.scopeannotated.Scope</scope> + <class>org.apache.openjpa.persistence.common.apps.Address</class> + <class>org.apache.openjpa.persistence.common.apps.CompUser</class> + </persistence-unit> + <persistence-unit name="PUTest-Qualifiers" transaction-type="RESOURCE_LOCAL"> + <qualifier>som.ecustom.QualifierForInjection</qualifier> + <qualifier>som.ecustom.SecondQualifierForInjection</qualifier> + <class>org.apache.openjpa.persistence.common.apps.Address</class> + <class>org.apache.openjpa.persistence.common.apps.CompUser</class> + </persistence-unit> </persistence> diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java index 330f3acec..45a62def9 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java @@ -415,10 +415,10 @@ public class PersistenceProductDerivation } @Override - public List getAnchorsInResource(String resource) throws Exception { + public List<String> getAnchorsInResource(String resource) throws Exception { ConfigurationParser parser = new ConfigurationParser(null); try { - List results = new ArrayList(); + List<String> results = new ArrayList<>(); ClassLoader loader = Thread.currentThread().getContextClassLoader(); List<URL> urls = getResourceURLs(resource, loader); if (urls != null) { @@ -924,8 +924,17 @@ public class PersistenceProductDerivation if ("provider".equals(name)) _info.setPersistenceProviderClassName(currentText()); break; - case 's' : // shared-cache-mode - _info.setSharedCacheMode(JPAProperties.getEnumValue(SharedCacheMode.class, currentText())); + case 'q': + if (("qualifier").equals(name)) { + _info.addQualifierAnnotationNames(currentText()); + } + break; + case 's' : + if ("shared-cache-mode".equals(name)) { + _info.setSharedCacheMode(JPAProperties.getEnumValue(SharedCacheMode.class, currentText())); + } else if ("scope".equals(name)) { + _info.setScopeAnnotationName(currentText()); + } break; case 'v': // validation-mode _info.setValidationMode(JPAProperties.getEnumValue(ValidationMode.class, currentText())); diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceUnitInfoImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceUnitInfoImpl.java index 691873ef9..1112d9c84 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceUnitInfoImpl.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceUnitInfoImpl.java @@ -85,6 +85,8 @@ public class PersistenceUnitInfoImpl private String _schemaVersion = "1.0"; private ValidationMode _validationMode; private SharedCacheMode _sharedCacheMode; + private List<String> _qualifierAnnotationNames; + private String _scopeAnnotationName; // A persistence unit is defined by a persistence.xml file. The jar // file or directory whose META-INF directory contains the @@ -607,15 +609,29 @@ public class PersistenceUnitInfoImpl public void setSharedCacheMode(SharedCacheMode mode) { _sharedCacheMode = mode; } + + public void setScopeAnnotationName(String scopeAnnotationName) { + _scopeAnnotationName = scopeAnnotationName; + } @Override public String getScopeAnnotationName() { - throw new UnsupportedOperationException("Not yet implemented (JPA 3.2)"); + return _scopeAnnotationName; + } + + public void addQualifierAnnotationNames(String qualifierAnnotationName) { + if (_qualifierAnnotationNames == null) { + _qualifierAnnotationNames = new ArrayList<String>(); + } + _qualifierAnnotationNames.add(qualifierAnnotationName); } @Override public List<String> getQualifierAnnotationNames() { - throw new UnsupportedOperationException("Not yet implemented (JPA 3.2)"); + if (_qualifierAnnotationNames == null) { + return List.of(); + } + return _qualifierAnnotationNames; } public static PersistenceUnitInfoImpl convert(PersistenceConfiguration config) {