From 3822aefec924204f2dc5579d239f7243021f9bfd Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Tue, 23 Apr 2024 21:11:39 -0500 Subject: [PATCH] HHH-18003 - Create a PersistenceUnitDescriptor wrapper around JPA 3.2 PersistenceConfiguration --- .../jpa/HibernatePersistenceProvider.java | 9 +- .../PersistenceConfigurationDescriptor.java | 136 ++++++++++++++++++ 2 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 hibernate-core/src/main/java/org/hibernate/jpa/boot/spi/PersistenceConfigurationDescriptor.java diff --git a/hibernate-core/src/main/java/org/hibernate/jpa/HibernatePersistenceProvider.java b/hibernate-core/src/main/java/org/hibernate/jpa/HibernatePersistenceProvider.java index e6d495ac7b..e810f961a2 100644 --- a/hibernate-core/src/main/java/org/hibernate/jpa/HibernatePersistenceProvider.java +++ b/hibernate-core/src/main/java/org/hibernate/jpa/HibernatePersistenceProvider.java @@ -19,6 +19,7 @@ import jakarta.persistence.spi.ProviderUtil; import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; import org.hibernate.jpa.boot.internal.ParsedPersistenceXmlDescriptor; +import org.hibernate.jpa.boot.spi.PersistenceConfigurationDescriptor; import org.hibernate.jpa.boot.internal.PersistenceXmlParser; import org.hibernate.jpa.boot.spi.Bootstrap; import org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder; @@ -197,8 +198,12 @@ public class HibernatePersistenceProvider implements PersistenceProvider { @Override public EntityManagerFactory createEntityManagerFactory(PersistenceConfiguration configuration) { - // todo (jpa 3.2) : implement this - throw new UnsupportedOperationException( "Not yet implemented" ); + final EntityManagerFactoryBuilder builder = getEntityManagerFactoryBuilder( + new PersistenceConfigurationDescriptor( configuration ), + Collections.emptyMap(), + HibernatePersistenceProvider.class.getClassLoader() + ); + return builder.build(); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/jpa/boot/spi/PersistenceConfigurationDescriptor.java b/hibernate-core/src/main/java/org/hibernate/jpa/boot/spi/PersistenceConfigurationDescriptor.java new file mode 100644 index 0000000000..9e739babcf --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/jpa/boot/spi/PersistenceConfigurationDescriptor.java @@ -0,0 +1,136 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html. + */ +package org.hibernate.jpa.boot.spi; + +import java.net.URL; +import java.util.List; +import java.util.Properties; + +import org.hibernate.bytecode.enhance.spi.EnhancementContext; +import org.hibernate.bytecode.spi.ClassTransformer; +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.internal.util.collections.CollectionHelper; +import org.hibernate.jpa.HibernatePersistenceProvider; + +import jakarta.persistence.PersistenceConfiguration; +import jakarta.persistence.SharedCacheMode; +import jakarta.persistence.ValidationMode; +import jakarta.persistence.spi.PersistenceUnitTransactionType; + +/** + * PersistenceUnitDescriptor wrapper around {@linkplain PersistenceConfiguration} + * + * @author Steve Ebersole + */ +public class PersistenceConfigurationDescriptor implements PersistenceUnitDescriptor { + private final PersistenceConfiguration persistenceConfiguration; + + private final Properties properties; + private final List managedClassNames; + + public PersistenceConfigurationDescriptor(PersistenceConfiguration persistenceConfiguration) { + this.persistenceConfiguration = persistenceConfiguration; + + this.properties = CollectionHelper.asProperties( persistenceConfiguration.properties() ); + this.managedClassNames = persistenceConfiguration.managedClasses().stream().map( Class::getName ).toList(); + } + + @Override + public Properties getProperties() { + return properties; + } + + @Override + public String getName() { + return persistenceConfiguration.name(); + } + + @Override + public String getProviderClassName() { + return persistenceConfiguration.provider(); + } + + @Override + public boolean isUseQuotedIdentifiers() { + return properties.get( AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS ) == Boolean.TRUE; + } + + @Override + public boolean isExcludeUnlistedClasses() { + // because we do not know the root url nor jar files we cannot do scanning + return true; + } + + @SuppressWarnings("removal") + @Override + public PersistenceUnitTransactionType getTransactionType() { + return switch ( persistenceConfiguration.transactionType() ) { + case JTA -> PersistenceUnitTransactionType.JTA; + case RESOURCE_LOCAL -> PersistenceUnitTransactionType.RESOURCE_LOCAL; + }; + } + + @Override + public ValidationMode getValidationMode() { + return persistenceConfiguration.validationMode(); + } + + @Override + public SharedCacheMode getSharedCacheMode() { + return persistenceConfiguration.sharedCacheMode(); + } + + @Override + public List getManagedClassNames() { + return managedClassNames; + } + + @Override + public List getMappingFileNames() { + return persistenceConfiguration.mappingFiles(); + } + + @Override + public Object getNonJtaDataSource() { + return persistenceConfiguration.nonJtaDataSource(); + } + + @Override + public Object getJtaDataSource() { + return persistenceConfiguration.jtaDataSource(); + } + + @Override + public ClassLoader getClassLoader() { + return HibernatePersistenceProvider.class.getClassLoader(); + } + + @Override + public ClassLoader getTempClassLoader() { + return null; + } + + @Override + public void pushClassTransformer(EnhancementContext enhancementContext) { + + } + + @Override + public ClassTransformer getClassTransformer() { + return null; + } + + @Override + public URL getPersistenceUnitRootUrl() { + return null; + } + + @Override + public List getJarFileUrls() { + return null; + } +}