Skip to content

Commit 3305c71

Browse files
committed
HHH-10121 - Have EMF#getProperties expose ValidatorFactory injected via 2-phase load
(cherry picked from commit eb54414)
1 parent 187d8b8 commit 3305c71

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/EntityManagerFactoryImpl.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ public EntityManagerFactoryImpl(
123123
HashMap<String,Object> props = new HashMap<String, Object>();
124124
addAll( props, sessionFactory.getProperties() );
125125
addAll( props, configurationValues );
126+
if ( !props.containsKey( AvailableSettings.VALIDATION_FACTORY ) ) {
127+
if ( sessionFactory.getSessionFactoryOptions().getValidatorFactoryReference() != null ) {
128+
props.put(
129+
AvailableSettings.VALIDATION_FACTORY,
130+
sessionFactory.getSessionFactoryOptions().getValidatorFactoryReference()
131+
);
132+
}
133+
}
126134
maskOutSensitiveInformation( props );
127135
this.properties = Collections.unmodifiableMap( props );
128136
String entityManagerFactoryName = (String)this.properties.get( AvailableSettings.ENTITY_MANAGER_FACTORY_NAME);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.jpa.test.beanvalidation;
8+
9+
import java.net.URL;
10+
import java.util.Collections;
11+
12+
import javax.persistence.EntityManagerFactory;
13+
import javax.validation.Validation;
14+
import javax.validation.ValidatorFactory;
15+
16+
import org.hibernate.jpa.AvailableSettings;
17+
import org.hibernate.jpa.HibernatePersistenceProvider;
18+
import org.hibernate.jpa.boot.spi.Bootstrap;
19+
import org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder;
20+
import org.hibernate.jpa.test.jee.OrmVersionTest;
21+
22+
import org.hibernate.testing.junit4.BaseUnitTestCase;
23+
import org.junit.After;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
27+
import static org.junit.Assert.assertSame;
28+
29+
/**
30+
* Test injection of ValidatorFactory using WF/Hibernate 2-phase boot process
31+
*
32+
* @author Steve Ebersole
33+
*/
34+
public class ValidatorFactory2PhaseInjectionTest extends BaseUnitTestCase {
35+
private ValidatorFactory vf;
36+
37+
@Before
38+
public void before() {
39+
vf = Validation.byDefaultProvider().configure().buildValidatorFactory();
40+
}
41+
42+
@After
43+
public void after() {
44+
if ( vf != null ) {
45+
vf.close();
46+
}
47+
}
48+
49+
@Test
50+
public void testInjectionAvailabilityFromEmf() {
51+
EntityManagerFactoryBuilder emfb = Bootstrap.getEntityManagerFactoryBuilder(
52+
new OrmVersionTest.PersistenceUnitInfoImpl( "my-test" ) {
53+
@Override
54+
public URL getPersistenceUnitRootUrl() {
55+
// just get any known url...
56+
return HibernatePersistenceProvider.class.getResource( "/org/hibernate/jpa/persistence_1_0.xsd" );
57+
}
58+
},
59+
Collections.emptyMap()
60+
);
61+
62+
emfb.withValidatorFactory( vf );
63+
64+
EntityManagerFactory emf = emfb.build();
65+
assertSame( vf, emf.getProperties().get( AvailableSettings.VALIDATION_FACTORY ) );
66+
emf.close();
67+
}
68+
}

0 commit comments

Comments
 (0)