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
commit b96b230a49f6b0225767ed32e490f02bb734b1e3 Author: Paulo Cristovão de Araújo Silva Filho <[email protected]> AuthorDate: Fri Jul 18 10:28:07 2025 -0300 [OPENJPA-2940][WIP] Implemented JPA 3.2 entity(String) method in Metamodel --- .../apache/openjpa/persistence/meta/TestMetamodel.java | 15 ++++++++++++++- .../apache/openjpa/persistence/meta/MetamodelImpl.java | 18 ++++++++++++------ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/TestMetamodel.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/TestMetamodel.java index 9c90108e0..23fd442d3 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/TestMetamodel.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/TestMetamodel.java @@ -49,7 +49,7 @@ import org.apache.openjpa.persistence.relations.OneOneParent; import org.apache.openjpa.persistence.test.SingleEMFTestCase; /** - * Tests JPA 2.0 Metamodel. + * Tests JPA 3.2 Metamodel. * * @author Pinaki Poddar * @@ -316,6 +316,19 @@ public class TestMetamodel extends SingleEMFTestCase { Attribute.PersistentAttributeType attr = type.getAttribute("geocode").getPersistentAttributeType(); assertEquals(PersistentAttributeType.EMBEDDED, attr); } + + public void testEntityByString() { + assertNotNull(model.entity("ImplicitFieldAccessBase")); + } + + public void testEntityByStringException() { + try { + model.entity("InexistentEntityToSearch"); + fail("Should have thrown an exception"); + } catch (Exception ex) { + assertTrue(ex instanceof IllegalArgumentException); + } + } public void testNotFoundErrorMessage() { IdentifiableType<ImplicitFieldAccessBase> e0 = model.entity(ImplicitFieldAccessBase.class); diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/MetamodelImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/MetamodelImpl.java index b29b9b70b..1c7dc96cb 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/MetamodelImpl.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/MetamodelImpl.java @@ -55,6 +55,7 @@ import org.apache.openjpa.meta.ClassMetaData; import org.apache.openjpa.meta.FieldMetaData; import org.apache.openjpa.meta.MetaDataRepository; import org.apache.openjpa.persistence.meta.Members.Member; +import org.apache.openjpa.util.Exceptions; import org.apache.openjpa.util.InternalException; /** @@ -121,7 +122,17 @@ public class MetamodelImpl implements Metamodel, Resolver { return (EmbeddableType<X>)find(clazz, _embeddables, EMBEDDABLE, false); } - /** + @Override + public EntityType<?> entity(String entityName) { + return getEntityValuesOnly() + .parallelStream() + .filter(et -> et.getName().contentEquals(entityName)) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException(_loc.get("invalid_entity_argument", + "load", entityName == null ? "null" : Exceptions.toString(entityName)).getMessage())); + } + + /** * Return the metamodel entity type representing the entity. * @param cls the type of the represented entity * @return the metamodel entity type @@ -455,9 +466,4 @@ public class MetamodelImpl implements Metamodel, Resolver { throw new UnsupportedOperationException(); } - @Override - public EntityType<?> entity(String entityName) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Not yet implemented (JPA 3.2)"); - } }
