From 595f068977552d42652665c1654341117daf1d77 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Thu, 20 Sep 2012 03:47:30 -0500 Subject: [PATCH] HHH-7620 - allow ValidatorFactory to be passed into EntityManagerFactoryBuilder --- .../jpa/HibernatePersistenceProvider.java | 4 +-- .../EntityManagerFactoryBuilderImpl.java | 30 ++++++++++++++++--- .../boot/spi/EntityManagerFactoryBuilder.java | 16 +++++++--- .../BaseEntityManagerFunctionalTestCase.java | 2 +- .../TestingEntityManagerFactoryGenerator.java | 2 +- .../ejb3configuration/InterceptorTest.java | 8 ++--- .../PersisterClassProviderTest.java | 2 +- .../SessionFactoryObserverTest.java | 2 +- ...entifierGeneratorStrategyProviderTest.java | 2 +- .../test/BaseEnversJPAFunctionalTestCase.java | 2 +- .../AbstractEntityManagerTest.java | 2 +- 11 files changed, 51 insertions(+), 21 deletions(-) diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/HibernatePersistenceProvider.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/HibernatePersistenceProvider.java index 5987aee1d9..90054fea1f 100755 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/HibernatePersistenceProvider.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/HibernatePersistenceProvider.java @@ -82,7 +82,7 @@ public class HibernatePersistenceProvider implements PersistenceProvider { continue; } - return Bootstrap.getEntityManagerFactoryBuilder( persistenceUnit, integration ).buildEntityManagerFactory(); + return Bootstrap.getEntityManagerFactoryBuilder( persistenceUnit, integration ).build(); } return null; @@ -100,7 +100,7 @@ public class HibernatePersistenceProvider implements PersistenceProvider { */ @Override public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map integration) { - return Bootstrap.getEntityManagerFactoryBuilder( info, integration ).buildEntityManagerFactory(); + return Bootstrap.getEntityManagerFactoryBuilder( info, integration ).build(); } private final ProviderUtil providerUtil = new ProviderUtil() { diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java index 8c10ee5c81..a786adb4b8 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java @@ -133,7 +133,7 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil private final PersistenceUnitDescriptor persistenceUnit; private final SettingsImpl settings = new SettingsImpl(); private final StandardServiceRegistryBuilder serviceRegistryBuilder; - private final Map configurationValues; + private final Map configurationValues; private final List jaccDefinitions = new ArrayList(); private final List cacheRegionDefinitions = new ArrayList(); @@ -148,6 +148,8 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil private static EntityNotFoundDelegate jpaEntityNotFoundDelegate = new JpaEntityNotFoundDelegate(); + private Object validatorFactory; + private static class JpaEntityNotFoundDelegate implements EntityNotFoundDelegate, Serializable { public void handleEntityNotFound(String entityName, Serializable id) { throw new EntityNotFoundException( "Unable to find " + entityName + " with id " + id ); @@ -198,7 +200,8 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // temporary! - public Map getConfigurationValues() { + @SuppressWarnings("unchecked") + public Map getConfigurationValues() { return Collections.unmodifiableMap( configurationValues ); } @@ -488,6 +491,8 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil applyJdbcConnectionProperties(); applyTransactionProperties(); + // this check is needed for tests. Second form happens later (mainly against the explicitly passed validator) + // when building EMF... final Object validationFactory = configurationValues.get( AvailableSettings.VALIDATION_FACTORY ); if ( validationFactory != null ) { BeanValidationIntegrator.validateFactory( validationFactory ); @@ -499,7 +504,8 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil LOG.definingFlushBeforeCompletionIgnoredInHem( Environment.FLUSH_BEFORE_COMPLETION ); } - for ( Map.Entry entry : configurationValues.entrySet() ) { + for ( Object oEntry : configurationValues.entrySet() ) { + Map.Entry entry = (Map.Entry) oEntry; if ( entry.getKey() instanceof String ) { final String keyString = (String) entry.getKey(); @@ -912,6 +918,12 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil } } + @Override + public EntityManagerFactoryBuilder withValidatorFactory(Object validatorFactory) { + this.validatorFactory = validatorFactory; + return this; + } + @Override public void cancel() { // todo : close the bootstrap registry (not critical, but nice to do) @@ -919,11 +931,21 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil } @SuppressWarnings("unchecked") - public EntityManagerFactory buildEntityManagerFactory() { + public EntityManagerFactory build() { // IMPL NOTE : TCCL handling here is temporary. // It is needed because this code still uses Hibernate Configuration and Hibernate commons-annotations // in turn which relies on TCCL being set. + if ( validatorFactory != null ) { + // NOTE : need to add it to both + configurationValues.put( AvailableSettings.VALIDATION_FACTORY, validatorFactory ); + } + + final Object validationFactory = configurationValues.get( AvailableSettings.VALIDATION_FACTORY ); + if ( validationFactory != null ) { + BeanValidationIntegrator.validateFactory( validationFactory ); + } + final ServiceRegistry serviceRegistry = buildServiceRegistry(); final ClassLoaderService classLoaderService = serviceRegistry.getService( ClassLoaderService.class ); diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/spi/EntityManagerFactoryBuilder.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/spi/EntityManagerFactoryBuilder.java index 7975390866..beb3ba83e3 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/spi/EntityManagerFactoryBuilder.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/spi/EntityManagerFactoryBuilder.java @@ -25,15 +25,13 @@ package org.hibernate.jpa.boot.spi; import javax.persistence.EntityManagerFactory; -import org.hibernate.cfg.Configuration; - /** * Represents a 2-phase JPA bootstrap process for building a Hibernate EntityManagerFactory. * * The first phase is the process of instantiating this builder. During the first phase, loading of Class references * is highly discouraged. * - * The second phase is building the EntityManagerFactory instance via {@link #buildEntityManagerFactory()}. + * The second phase is building the EntityManagerFactory instance via {@link #build}. * * If anything goes wrong during either phase and the bootstrap process needs to be aborted, {@link #cancel()} should * be called. @@ -42,12 +40,22 @@ import org.hibernate.cfg.Configuration; * @author Scott Marlow */ public interface EntityManagerFactoryBuilder { + /** + * Allows passing in a Java EE ValidatorFactory (delayed from constructing the builder, AKA phase 2) to be used + * in building the EntityManagerFactory + * + * @param validatorFactory The ValidatorFactory + * + * @return {@code this}, for method chaining + */ + public EntityManagerFactoryBuilder withValidatorFactory(Object validatorFactory); + /** * Build {@link EntityManagerFactory} instance * * @return The built {@link EntityManagerFactory} */ - public EntityManagerFactory buildEntityManagerFactory(); + public EntityManagerFactory build(); /** * Cancel the building processing. This is used to signal the builder to release any resources in the case of diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/BaseEntityManagerFunctionalTestCase.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/BaseEntityManagerFunctionalTestCase.java index 4fca26b4e3..1e971c640e 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/BaseEntityManagerFunctionalTestCase.java +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/BaseEntityManagerFunctionalTestCase.java @@ -93,7 +93,7 @@ public abstract class BaseEntityManagerFunctionalTestCase extends BaseUnitTestCa entityManagerFactory = (EntityManagerFactoryImpl) Bootstrap.getEntityManagerFactoryBuilder( buildPersistenceUnitDescriptor(), buildSettings() - ).buildEntityManagerFactory(); + ).build(); serviceRegistry = (StandardServiceRegistryImpl) entityManagerFactory.getSessionFactory() .getServiceRegistry() diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/TestingEntityManagerFactoryGenerator.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/TestingEntityManagerFactoryGenerator.java index 963d73bab3..0ecffdf0d8 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/TestingEntityManagerFactoryGenerator.java +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/TestingEntityManagerFactoryGenerator.java @@ -46,6 +46,6 @@ public class TestingEntityManagerFactoryGenerator { } public static EntityManagerFactory generateEntityManagerFactory(PersistenceUnitDescriptor descriptor, Map settings) { - return Bootstrap.getEntityManagerFactoryBuilder( descriptor, settings ).buildEntityManagerFactory(); + return Bootstrap.getEntityManagerFactoryBuilder( descriptor, settings ).build(); } } diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/ejb3configuration/InterceptorTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/ejb3configuration/InterceptorTest.java index 748509429f..8b18bbcbf1 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/ejb3configuration/InterceptorTest.java +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/ejb3configuration/InterceptorTest.java @@ -52,7 +52,7 @@ public class InterceptorTest { public void testConfiguredInterceptor() { Map settings = basicSettings(); settings.put( AvailableSettings.INTERCEPTOR, ExceptionInterceptor.class.getName() ); - EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( new PersistenceUnitDescriptorAdapter(), settings ).buildEntityManagerFactory(); + EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( new PersistenceUnitDescriptorAdapter(), settings ).build(); EntityManager em = emf.createEntityManager(); Item i = new Item(); i.setName( "Laptop" ); @@ -78,7 +78,7 @@ public class InterceptorTest { public void testConfiguredSessionInterceptor() { Map settings = basicSettings(); settings.put( AvailableSettings.SESSION_INTERCEPTOR, LocalExceptionInterceptor.class.getName() ); - EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( new PersistenceUnitDescriptorAdapter(), settings ).buildEntityManagerFactory(); + EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( new PersistenceUnitDescriptorAdapter(), settings ).build(); EntityManager em = emf.createEntityManager(); Item i = new Item(); i.setName( "Laptop" ); @@ -104,7 +104,7 @@ public class InterceptorTest { public void testEmptyCreateEntityManagerFactoryAndPropertyUse() { Map settings = basicSettings(); settings.put( AvailableSettings.INTERCEPTOR, ExceptionInterceptor.class.getName() ); - EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( new PersistenceUnitDescriptorAdapter(), settings ).buildEntityManagerFactory(); + EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( new PersistenceUnitDescriptorAdapter(), settings ).build(); EntityManager em = emf.createEntityManager(); Item i = new Item(); i.setName( "Laptop" ); @@ -130,7 +130,7 @@ public class InterceptorTest { public void testOnLoadCallInInterceptor() { Map settings = basicSettings(); settings.put( AvailableSettings.INTERCEPTOR, new ExceptionInterceptor( true ) ); - EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( new PersistenceUnitDescriptorAdapter(), settings ).buildEntityManagerFactory(); + EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( new PersistenceUnitDescriptorAdapter(), settings ).build(); EntityManager em = emf.createEntityManager(); Item i = new Item(); i.setName( "Laptop" ); diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/ejb3configuration/PersisterClassProviderTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/ejb3configuration/PersisterClassProviderTest.java index 84411b3372..529618bdd0 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/ejb3configuration/PersisterClassProviderTest.java +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/ejb3configuration/PersisterClassProviderTest.java @@ -80,7 +80,7 @@ public class PersisterClassProviderTest { EntityManagerFactory entityManagerFactory = Bootstrap.getEntityManagerFactoryBuilder( new PersistenceUnitDescriptorAdapter(), settings - ).buildEntityManagerFactory(); + ).build(); entityManagerFactory.close(); } catch ( PersistenceException e ) { diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/ejb3configuration/SessionFactoryObserverTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/ejb3configuration/SessionFactoryObserverTest.java index 1ddfbccf0b..28de0b7ca7 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/ejb3configuration/SessionFactoryObserverTest.java +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/ejb3configuration/SessionFactoryObserverTest.java @@ -50,7 +50,7 @@ public class SessionFactoryObserverTest { ); try { - final EntityManagerFactory entityManagerFactory = builder.buildEntityManagerFactory(); + final EntityManagerFactory entityManagerFactory = builder.build(); entityManagerFactory.close(); Assert.fail( "GoofyException should have been thrown" ); } diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/ejb3configuration/id/IdentifierGeneratorStrategyProviderTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/ejb3configuration/id/IdentifierGeneratorStrategyProviderTest.java index 148ff31f58..6a07a01024 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/ejb3configuration/id/IdentifierGeneratorStrategyProviderTest.java +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/ejb3configuration/id/IdentifierGeneratorStrategyProviderTest.java @@ -51,7 +51,7 @@ public class IdentifierGeneratorStrategyProviderTest { final EntityManagerFactory entityManagerFactory = Bootstrap.getEntityManagerFactoryBuilder( new PersistenceUnitInfoAdapter(), settings - ).buildEntityManagerFactory(); + ).build(); final EntityManager entityManager = entityManagerFactory.createEntityManager(); try { diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/BaseEnversJPAFunctionalTestCase.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/BaseEnversJPAFunctionalTestCase.java index 8e76f72c47..1856dba36d 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/BaseEnversJPAFunctionalTestCase.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/BaseEnversJPAFunctionalTestCase.java @@ -97,7 +97,7 @@ public abstract class BaseEnversJPAFunctionalTestCase extends AbstractEnversTest buildPersistenceUnitDescriptor(), buildSettings() ); - entityManagerFactory = (EntityManagerFactoryImpl) entityManagerFactoryBuilder.buildEntityManagerFactory(); + entityManagerFactory = (EntityManagerFactoryImpl) entityManagerFactoryBuilder.build(); serviceRegistry = (StandardServiceRegistryImpl) entityManagerFactory.getSessionFactory() .getServiceRegistry() diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/performance/AbstractEntityManagerTest.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/performance/AbstractEntityManagerTest.java index da3fdd326a..1f823d2254 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/performance/AbstractEntityManagerTest.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/performance/AbstractEntityManagerTest.java @@ -116,7 +116,7 @@ public abstract class AbstractEntityManagerTest extends AbstractEnversTest { configurationProperties ); - emf = (EntityManagerFactoryImpl) entityManagerFactoryBuilder.buildEntityManagerFactory(); + emf = (EntityManagerFactoryImpl) entityManagerFactoryBuilder.build(); serviceRegistry = (StandardServiceRegistryImpl) emf.getSessionFactory().getServiceRegistry().getParentServiceRegistry();