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 8e3731ed0 [OPENJPA-2940][WIP] Implementing PUU.getClass(T entity).
8e3731ed0 is described below

commit 8e3731ed0284e87935b581abef2dc5ec35cb00ef
Author: Paulo Cristovão de Araújo Silva Filho <pcris...@gmail.com>
AuthorDate: Sat Jul 19 13:39:15 2025 -0300

    [OPENJPA-2940][WIP] Implementing PUU.getClass(T entity).
---
 .../org/apache/openjpa/persistence/util/Eager.java | 53 ++++++++++++++++++++++
 .../openjpa/persistence/util/EagerEntity.java      | 40 ++++++++++------
 .../persistence/util/TestPersistenceUnitUtil.java  | 39 ++++++++++++++--
 .../persistence/util/TestPersistenceUtil.java      | 10 ++--
 .../openjpa/persistence/util/TestProviderUtil.java |  6 +--
 .../persistence/EntityManagerFactoryImpl.java      |  6 ++-
 .../persistence/OpenJPAPersistenceUtil.java        | 15 +++++-
 7 files changed, 141 insertions(+), 28 deletions(-)

diff --git 
a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/Eager.java
 
b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/Eager.java
new file mode 100644
index 000000000..4403dd71a
--- /dev/null
+++ 
b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/Eager.java
@@ -0,0 +1,53 @@
+/*
+ * 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.
+ */
+package org.apache.openjpa.persistence.util;
+
+import java.util.List;
+
+public interface Eager {
+
+       void setId(int id);
+
+       int getId();
+
+       void setName(String name);
+
+       String getName();
+
+       void setEagerEmbed(EagerEmbed eagerEmbed);
+
+       EagerEmbed getEagerEmbed();
+
+       void setTransField(String transField);
+
+       String getTransField();
+
+       void setEagerEmbedColl(List<EagerEmbed> eagerEmbedColl);
+
+       List<EagerEmbed> getEagerEmbedColl();
+
+       void setEagerEmbedRel(EagerEmbedRel eagerEmbedRel);
+
+       EagerEmbedRel getEagerEmbedRel();
+
+       Integer getVersion();
+
+       void setVersion(Integer version);
+
+}
diff --git 
a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/EagerEntity.java
 
b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/EagerEntity.java
index 307642ae8..9a8b16181 100644
--- 
a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/EagerEntity.java
+++ 
b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/EagerEntity.java
@@ -32,7 +32,7 @@ import jakarta.persistence.Transient;
 import jakarta.persistence.Version;
 
 @Entity
-public class EagerEntity {
+public class EagerEntity implements Eager {
 
     @Id
     private int id;
@@ -59,58 +59,72 @@ public class EagerEntity {
     @Transient
     private String transField;
 
-    public void setId(int id) {
+    @Override
+       public void setId(int id) {
         this.id = id;
     }
 
-    public int getId() {
+    @Override
+       public int getId() {
         return id;
     }
 
-    public void setName(String name) {
+    @Override
+       public void setName(String name) {
         this.name = name;
     }
 
-    public String getName() {
+    @Override
+       public String getName() {
         return name;
     }
 
-    public void setEagerEmbed(EagerEmbed eagerEmbed) {
+    @Override
+       public void setEagerEmbed(EagerEmbed eagerEmbed) {
         this.eagerEmbed = eagerEmbed;
     }
 
-    public EagerEmbed getEagerEmbed() {
+    @Override
+       public EagerEmbed getEagerEmbed() {
         return eagerEmbed;
     }
 
-    public void setTransField(String transField) {
+    @Override
+       public void setTransField(String transField) {
         this.transField = transField;
     }
 
-    public String getTransField() {
+    @Override
+       public String getTransField() {
         return transField;
     }
 
-    public void setEagerEmbedColl(List<EagerEmbed> eagerEmbedColl) {
+    @Override
+       public void setEagerEmbedColl(List<EagerEmbed> eagerEmbedColl) {
         this.eagerEmbedColl = eagerEmbedColl;
     }
 
-    public List<EagerEmbed> getEagerEmbedColl() {
+    @Override
+       public List<EagerEmbed> getEagerEmbedColl() {
         return eagerEmbedColl;
     }
 
-    public void setEagerEmbedRel(EagerEmbedRel eagerEmbedRel) {
+    @Override
+       public void setEagerEmbedRel(EagerEmbedRel eagerEmbedRel) {
         this.eagerEmbedRel = eagerEmbedRel;
     }
 
-    public EagerEmbedRel getEagerEmbedRel() {
+    @Override
+       public EagerEmbedRel getEagerEmbedRel() {
         return eagerEmbedRel;
     }
 
+       @Override
        public Integer getVersion() {
                return version;
        }
 
+       @Override
        public void setVersion(Integer version) {
                this.version = version;
        }
diff --git 
a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/TestPersistenceUnitUtil.java
 
b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/TestPersistenceUnitUtil.java
index 54924dff2..167446ad4 100644
--- 
a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/TestPersistenceUnitUtil.java
+++ 
b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/TestPersistenceUnitUtil.java
@@ -38,6 +38,7 @@ import jakarta.persistence.EntityManager;
 import jakarta.persistence.EntityManagerFactory;
 import jakarta.persistence.Persistence;
 import jakarta.persistence.PersistenceConfiguration;
+import jakarta.persistence.PersistenceException;
 import jakarta.persistence.PersistenceUnitUtil;
 import jakarta.persistence.metamodel.Attribute;
 
@@ -161,7 +162,7 @@ public class TestPersistenceUnitUtil {
         PersistenceUnitUtil puu = emf.getPersistenceUnitUtil();
         assertSame(emf, puu);
 
-        EagerEntity ee = createEagerEntity();
+        Eager ee = createEagerEntity();
 
         em.getTransaction().begin();
         em.persist(ee);
@@ -347,7 +348,7 @@ public class TestPersistenceUnitUtil {
     public void testBasicTypeNotLoaded() {
         PersistenceUnitUtil puu = emf.getPersistenceUnitUtil();
         EntityManager em = emf.createEntityManager();
-        EagerEntity ee = createEagerEntity();
+        Eager ee = createEagerEntity();
         int id = ee.getId();
 
         em.getTransaction().begin();
@@ -451,7 +452,7 @@ public class TestPersistenceUnitUtil {
        
        @Test(expected = IllegalArgumentException.class)
        public void testUnmanagedEntity() {
-               EagerEntity ee = createEagerEntity();
+               Eager ee = createEagerEntity();
                emf.getPersistenceUnitUtil().getVersion(ee);
        }
 
@@ -488,11 +489,39 @@ public class TestPersistenceUnitUtil {
                assertNull(puu.getVersion(e));
        }
        
+       @Test
+       public void testGetNonManagedEntityClass() {
+               PersistenceUnitUtil puu = emf.getPersistenceUnitUtil();
+               EagerEntity ee = createEagerEntity();
+               try {
+                       puu.getClass(ee);
+                       fail("Should have thrown PersistenceException");
+               } catch (PersistenceException ex) {
+                       assertTrue(ex.getMessage().contains("not persistent"));
+                       return;
+               }
+       }
+       
+       @Test
+       public void testGetManagedEntityClass() {
+               PersistenceUnitUtil puu = emf.getPersistenceUnitUtil();
+               Eager e = emf.callInTransaction(em -> {
+                       Eager e_ = createEagerEntity();
+                       em.persist(e_);
+                       return e_;
+               });
+               
+               e = (Eager) em.createQuery("SELECT a FROM EagerEntity AS a 
WHERE a.id = :id").setParameter("id", e.getId()).getSingleResult();
+               
+               Class<?> clazz = puu.getClass(e);
+               assertEquals(EagerEntity.class, clazz);
+       }
+       
        private void verifyIsLoadedEagerState(boolean loaded) {
                PersistenceUnitUtil puu = emf.getPersistenceUnitUtil();
                assertSame(emf,  puu);
                
-               EagerEntity ee = createEagerEntity();
+               Eager ee = createEagerEntity();
                
                assertFalse(puu.isLoaded(ee));
                assertFalse(puu.isLoaded(ee, "id"));
@@ -547,7 +576,7 @@ public class TestPersistenceUnitUtil {
     }
 
        private void verifyPULoadState(EntityManager em, PersistenceUnitUtil... 
puu) {
-               EagerEntity ee = createEagerEntity();
+               Eager ee = createEagerEntity();
                assertEquals(false, puu[0].isLoaded(ee));
                assertEquals(false, puu[0].isLoaded(ee, "id"));
                assertEquals(false, puu[1].isLoaded(ee));
diff --git 
a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/TestPersistenceUtil.java
 
b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/TestPersistenceUtil.java
index 3a5e051e6..3ad305ed1 100644
--- 
a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/TestPersistenceUtil.java
+++ 
b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/TestPersistenceUtil.java
@@ -80,7 +80,7 @@ public class TestPersistenceUtil extends SingleEMFTestCase{
     public void testIsApplicationLoaded() {
         PersistenceUtil putil = Persistence.getPersistenceUtil();
         EntityManager em = emf.createEntityManager();
-        EagerEntity ee = createEagerEntity();
+        Eager ee = createEagerEntity();
 
         em.getTransaction().begin();
         em.persist(ee);
@@ -110,7 +110,7 @@ public class TestPersistenceUtil extends SingleEMFTestCase{
     public void testIsDetachLoaded() {
         PersistenceUtil putil = Persistence.getPersistenceUtil();
         EntityManager em = emf.createEntityManager();
-        EagerEntity ee = createEagerEntity();
+        Eager ee = createEagerEntity();
 
         em.getTransaction().begin();
         em.persist(ee);
@@ -126,7 +126,7 @@ public class TestPersistenceUtil extends SingleEMFTestCase{
     private void verifyIsLoadedEagerState(boolean loaded) {
         PersistenceUtil putil = Persistence.getPersistenceUtil();
         EntityManager em = emf.createEntityManager();
-        EagerEntity ee = createEagerEntity();
+        Eager ee = createEagerEntity();
 
         // Vfy state is true for the unmanaged entity via
         // PeristenceUtil
@@ -185,8 +185,8 @@ public class TestPersistenceUtil extends SingleEMFTestCase{
         em.close();
     }
 
-    private EagerEntity createEagerEntity() {
-        EagerEntity ee = new EagerEntity();
+    private Eager createEagerEntity() {
+        Eager ee = new EagerEntity();
         ee.setId(new Random().nextInt());
         ee.setName("EagerEntity");
         EagerEmbed emb = createEagerEmbed();
diff --git 
a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/TestProviderUtil.java
 
b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/TestProviderUtil.java
index afb57064b..849f46226 100644
--- 
a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/TestProviderUtil.java
+++ 
b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/TestProviderUtil.java
@@ -78,7 +78,7 @@ public class TestProviderUtil extends SingleEMFTestCase{
     private void verifyIsLoadedEagerState(LoadState state) {
         ProviderUtil pu = getProviderUtil();
         EntityManager em = emf.createEntityManager();
-        EagerEntity ee = createEagerEntity(true);
+        Eager ee = createEagerEntity(true);
 
         // Vfy LoadState is unknown for the unmanaged entity
         assertEquals(LoadState.UNKNOWN, pu.isLoaded(ee));
@@ -179,7 +179,7 @@ public class TestProviderUtil extends SingleEMFTestCase{
     public void testIsApplicationLoaded() {
         ProviderUtil pu = getProviderUtil();
         EntityManager em = emf.createEntityManager();
-        EagerEntity ee = createEagerEntity(true);
+        Eager ee = createEagerEntity(true);
 
         em.getTransaction().begin();
         em.persist(ee);
@@ -208,7 +208,7 @@ public class TestProviderUtil extends SingleEMFTestCase{
     public void testIsLoadedUnknown() {
         ProviderUtil pu = getProviderUtil();
 
-        EagerEntity ee = new EagerEntity();
+        Eager ee = new EagerEntity();
 
         assertEquals(LoadState.UNKNOWN, pu.isLoaded(ee));
         assertEquals(LoadState.UNKNOWN, pu.isLoadedWithReference(ee,
diff --git 
a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java
 
b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java
index 71bb288bc..607a83ac9 100644
--- 
a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java
+++ 
b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java
@@ -556,7 +556,11 @@ public class EntityManagerFactoryImpl
     
     @Override
     public <T> Class<? extends T> getClass(T entity) {
-       throw new UnsupportedOperationException("Not yet implemented (JPA 
3.2)");
+       if (!OpenJPAPersistenceUtil.isManagedBy(this, entity)) {
+               throw new 
jakarta.persistence.PersistenceException(_loc.get("invalid_entity_argument",
+                    "getClass", entity == null ? "null" : 
Exceptions.toString(entity)).getMessage());
+       }
+       return OpenJPAPersistenceUtil.getClass(this, entity);
     }
     
     @Override
diff --git 
a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAPersistenceUtil.java
 
b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAPersistenceUtil.java
index e60e00d86..4100d06d0 100644
--- 
a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAPersistenceUtil.java
+++ 
b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAPersistenceUtil.java
@@ -194,7 +194,20 @@ public class OpenJPAPersistenceUtil {
         }
         return LoadState.UNKNOWN;
     }
-
+    
+    @SuppressWarnings("unchecked")
+       public static <T> Class<T> getClass(OpenJPAEntityManagerFactory emf, 
Object entity) {
+       if (entity != null) {
+               if (entity instanceof PersistenceCapable pc) {
+                       StateManager sm = pc.pcGetStateManager();
+                       if (sm != null && sm instanceof OpenJPAStateManager 
osm) {
+                               return (Class<T>) 
osm.getMetaData().getDescribedType();
+                       }
+               }
+       }
+               return null;
+    }
+    
     private static LoadState isLoaded(OpenJPAStateManager sm, String attr,
         HashSet<OpenJPAStateManager> pcs) {
         boolean isLoaded = true;

Reply via email to