Skip to content

Commit 5aad2bf

Browse files
committed
HHH-7621 - allow DataSource to be passed into EntityManagerFactoryBuilder
1 parent d0f8834 commit 5aad2bf

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import javax.persistence.MappedSuperclass;
3333
import javax.persistence.PersistenceException;
3434
import javax.persistence.spi.PersistenceUnitTransactionType;
35+
import javax.sql.DataSource;
3536
import java.io.BufferedInputStream;
3637
import java.io.File;
3738
import java.io.IOException;
@@ -135,6 +136,7 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil
135136
// Explicit "injectables"
136137
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
137138
private Object validatorFactory;
139+
private DataSource dataSource;
138140
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
139141

140142
private final PersistenceUnitDescriptor persistenceUnit;
@@ -717,6 +719,13 @@ public EntityManagerFactoryBuilder withValidatorFactory(Object validatorFactory)
717719
return this;
718720
}
719721

722+
@Override
723+
public EntityManagerFactoryBuilder withDataSource(DataSource dataSource) {
724+
this.dataSource = dataSource;
725+
726+
return this;
727+
}
728+
720729
@Override
721730
public void cancel() {
722731
// todo : close the bootstrap registry (not critical, but nice to do)
@@ -827,7 +836,10 @@ else if ( keyString.startsWith( AvailableSettings.JACC_PREFIX )
827836
}
828837

829838
private void applyJdbcConnectionProperties() {
830-
if ( persistenceUnit.getJtaDataSource() != null ) {
839+
if ( dataSource != null ) {
840+
serviceRegistryBuilder.applySetting( Environment.DATASOURCE, dataSource );
841+
}
842+
else if ( persistenceUnit.getJtaDataSource() != null ) {
831843
serviceRegistryBuilder.applySetting( Environment.DATASOURCE, persistenceUnit.getJtaDataSource() );
832844
}
833845
else if ( persistenceUnit.getNonJtaDataSource() != null ) {

hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/spi/EntityManagerFactoryBuilder.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
package org.hibernate.jpa.boot.spi;
2525

2626
import javax.persistence.EntityManagerFactory;
27+
import javax.sql.DataSource;
2728

2829
/**
2930
* Represents a 2-phase JPA bootstrap process for building a Hibernate EntityManagerFactory.
@@ -50,6 +51,16 @@ public interface EntityManagerFactoryBuilder {
5051
*/
5152
public EntityManagerFactoryBuilder withValidatorFactory(Object validatorFactory);
5253

54+
/**
55+
* Allows passing in a DataSource (delayed from constructing the builder, AKA phase 2) to be used
56+
* in building the EntityManagerFactory
57+
*
58+
* @param dataSource The DataSource to use
59+
*
60+
* @return {@code this}, for method chaining
61+
*/
62+
public EntityManagerFactoryBuilder withDataSource(DataSource dataSource);
63+
5364
/**
5465
* Build {@link EntityManagerFactory} instance
5566
*

hibernate-entitymanager/src/main/java/org/hibernate/jpa/event/spi/JpaIntegrator.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,13 @@ public String toString() {
213213
}
214214
}
215215

216+
private static final String CDI_LISTENER_FACTORY_CLASS = "org.hibernate.jpa.event.internal.jpa.BeanManagerListenerFactory";
217+
216218
private ListenerFactory buildBeanManagerListenerFactory(Object beanManagerRef) {
217219
try {
218220
// specifically using our classloader here...
219221
final Class beanManagerListenerFactoryClass = getClass().getClassLoader()
220-
.loadClass( "org.hibernate.jpa.event.internal.jpa.BeanManagerListenerFactory" );
222+
.loadClass( CDI_LISTENER_FACTORY_CLASS );
221223
final Method beanManagerListenerFactoryBuilderMethod = beanManagerListenerFactoryClass.getMethod(
222224
"fromBeanManagerReference",
223225
Object.class
@@ -230,14 +232,14 @@ private ListenerFactory buildBeanManagerListenerFactory(Object beanManagerRef) {
230232
throw e.getTargetException();
231233
}
232234
}
233-
catch (ReflectiveOperationException e) {
234-
throw new HibernateException( "Could not access BeanManagerListenerFactory class to handle CDI extensions", e );
235+
catch (ClassNotFoundException e) {
236+
throw new HibernateException( "Could not locate BeanManagerListenerFactory class to handle CDI extensions", e );
235237
}
236-
catch (RuntimeException e) {
238+
catch (HibernateException e) {
237239
throw e;
238240
}
239241
catch (Throwable e) {
240-
throw new HibernateException( "Problem calling BeanManagerListenerFactory class to handle CDI extensions", e );
242+
throw new HibernateException( "Could not access BeanManagerListenerFactory class to handle CDI extensions", e );
241243
}
242244
}
243245

0 commit comments

Comments
 (0)