HHH-10121 - Have EMF#getProperties expose ValidatorFactory injected via 2-phase load

This commit is contained in:
Steve Ebersole 2015-09-30 00:20:35 -05:00
parent e64d028306
commit eb544140a1
2 changed files with 76 additions and 0 deletions

View File

@ -123,6 +123,14 @@ public class EntityManagerFactoryImpl implements HibernateEntityManagerFactory {
HashMap<String,Object> props = new HashMap<String, Object>();
addAll( props, sessionFactory.getProperties() );
addAll( props, configurationValues );
if ( !props.containsKey( AvailableSettings.VALIDATION_FACTORY ) ) {
if ( sessionFactory.getSessionFactoryOptions().getValidatorFactoryReference() != null ) {
props.put(
AvailableSettings.VALIDATION_FACTORY,
sessionFactory.getSessionFactoryOptions().getValidatorFactoryReference()
);
}
}
maskOutSensitiveInformation( props );
this.properties = Collections.unmodifiableMap( props );
String entityManagerFactoryName = (String)this.properties.get( AvailableSettings.ENTITY_MANAGER_FACTORY_NAME);

View File

@ -0,0 +1,68 @@
/*
* 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.test.beanvalidation;
import java.net.URL;
import java.util.Collections;
import javax.persistence.EntityManagerFactory;
import javax.validation.Validation;
import javax.validation.ValidatorFactory;
import org.hibernate.jpa.AvailableSettings;
import org.hibernate.jpa.HibernatePersistenceProvider;
import org.hibernate.jpa.boot.spi.Bootstrap;
import org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder;
import org.hibernate.jpa.test.jee.OrmVersionTest;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertSame;
/**
* Test injection of ValidatorFactory using WF/Hibernate 2-phase boot process
*
* @author Steve Ebersole
*/
public class ValidatorFactory2PhaseInjectionTest extends BaseUnitTestCase {
private ValidatorFactory vf;
@Before
public void before() {
vf = Validation.byDefaultProvider().configure().buildValidatorFactory();
}
@After
public void after() {
if ( vf != null ) {
vf.close();
}
}
@Test
public void testInjectionAvailabilityFromEmf() {
EntityManagerFactoryBuilder emfb = Bootstrap.getEntityManagerFactoryBuilder(
new OrmVersionTest.PersistenceUnitInfoImpl( "my-test" ) {
@Override
public URL getPersistenceUnitRootUrl() {
// just get any known url...
return HibernatePersistenceProvider.class.getResource( "/org/hibernate/jpa/persistence_1_0.xsd" );
}
},
Collections.emptyMap()
);
emfb.withValidatorFactory( vf );
EntityManagerFactory emf = emfb.build();
assertSame( vf, emf.getProperties().get( AvailableSettings.VALIDATION_FACTORY ) );
emf.close();
}
}