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 898a22887 [OPENJPA-2940][WIP] Intermmediate commit 898a22887 is described below commit 898a228879aaf874f440c651336b01ac19a5fd07 Author: Paulo Cristovão de Araújo Silva Filho <pcris...@gmail.com> AuthorDate: Tue Jul 15 16:19:01 2025 -0300 [OPENJPA-2940][WIP] Intermmediate commit * Implementing emf creation passing PersistenceConfiguration --- .../openjpa/persistence/simple/AllFieldTypes.java | 15 +++++++++++++ .../simple/TestEntityManagerFactory.java | 26 +++++++++++++++++++++- .../persistence/PersistenceProviderImpl.java | 21 ++++++++++++----- 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/AllFieldTypes.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/AllFieldTypes.java index 36a2c64dc..eb627593c 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/AllFieldTypes.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/AllFieldTypes.java @@ -32,8 +32,11 @@ import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.UUID; import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; import jakarta.persistence.OneToMany; import jakarta.persistence.OneToOne; @@ -95,6 +98,9 @@ public class AllFieldTypes { private LocalDateTime localDateTimeField; private OffsetTime offsetTimeField; private OffsetDateTime offsetDateTimeField; + + @GeneratedValue(strategy = GenerationType.UUID) + private UUID uniqueId; public void setShortField(short shortField) { @@ -417,5 +423,14 @@ public class AllFieldTypes { public void setOffsetDateTimeField(OffsetDateTime offsetDateTimeField) { this.offsetDateTimeField = offsetDateTimeField; } + + public UUID getUniqueId() { + return uniqueId; + } + + public void setUniqueId(UUID uniqueId) { + this.uniqueId = uniqueId; + } + } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestEntityManagerFactory.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestEntityManagerFactory.java index 526bb6fbe..0903942f2 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestEntityManagerFactory.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestEntityManagerFactory.java @@ -18,9 +18,10 @@ */ package org.apache.openjpa.persistence.simple; +import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManagerFactory; import jakarta.persistence.Persistence; - +import jakarta.persistence.PersistenceConfiguration; import junit.framework.TestCase; public class TestEntityManagerFactory extends TestCase { @@ -34,4 +35,27 @@ public class TestEntityManagerFactory extends TestCase { Persistence.createEntityManagerFactory("invalid"); emf.close(); } + + public void testEMFCreation() { + PersistenceConfiguration conf = new PersistenceConfiguration("test"); + conf.managedClass(AllFieldTypes.class); +// conf.property(PersistenceConfiguration.SCHEMAGEN_DATABASE_ACTION, "drop-and-create"); + conf.property("openjpa.jdbc.SynchronizeMappings", "buildSchema(ForeignKeys=true,SchemaAction='drop,add')"); + + EntityManagerFactory emf = Persistence.createEntityManagerFactory(conf); + assertNotNull(emf); + EntityManager em = emf.createEntityManager(); + assertNotNull(em); + + AllFieldTypes aft = new AllFieldTypes(); + em.getTransaction().begin(); + em.persist(aft); + em.getTransaction().commit(); + + assertNotNull(aft.getUniqueId()); + + em.close(); + emf.close(); + } + } diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java index 09e30477d..6b62be7cc 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java @@ -56,7 +56,9 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.security.ProtectionDomain; import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.stream.Collectors; /** @@ -162,7 +164,20 @@ public class PersistenceProviderImpl return createEntityManagerFactory(name, null, m); } - @Override + @Override + public EntityManagerFactory createEntityManagerFactory(PersistenceConfiguration config) { + List<Class<?>> managedClasses = config.managedClasses(); + if (managedClasses != null && !managedClasses.isEmpty()) { + String managedClassesList = managedClasses.stream().map(Class::getName).collect(Collectors.joining(";")); + String old = config.properties().containsKey("openjpa.MetaDataFactory") + ? "," + config.properties().get("openjpa.MetaDataFactory").toString() + : ""; + config.property("openjpa.MetaDataFactory", "jpa(Types=" + managedClassesList + old + ")"); + } + return createEntityManagerFactory(config.name(), config.properties()); + } + + @Override public OpenJPAEntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo pui, Map m) { PersistenceProductDerivation pd = new PersistenceProductDerivation(); try { @@ -487,8 +502,4 @@ public class PersistenceProviderImpl return OpenJPAPersistenceUtil.isLoaded(obj, attr); } - @Override - public EntityManagerFactory createEntityManagerFactory(PersistenceConfiguration configuration) { - throw new UnsupportedOperationException("Not yet implemented (JPA 3.2)"); - } }