HHH-12858 - integration overrides during JPA bootstrap ought to override all logically related settings

HHH-13432 - Have EntityManagerFactory expose persistence.xml `jta-data-source` element as a `javax.persistence.nonJtaDataSource` property

(cherry picked from commit fd72e4f75f)
This commit is contained in:
Steve Ebersole 2019-10-05 22:45:29 -05:00 committed by gbadner
parent 7eb0911fd6
commit cec3107e13
2 changed files with 132 additions and 24 deletions

View File

@ -16,6 +16,7 @@ import javax.persistence.spi.PersistenceUnitInfo;
import javax.sql.DataSource;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl;
import org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
@ -23,10 +24,8 @@ import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.jpa.HibernatePersistenceProvider;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.jdbc.DataSourceStub;
import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.env.ConnectionProviderBuilder;
import org.hibernate.testing.jdbc.DataSourceStub;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.hibernate.testing.util.jpa.DelegatingPersistenceUnitInfo;
import org.hibernate.testing.util.jpa.PersistenceUnitInfoAdapter;
@ -90,27 +89,32 @@ public class PersistenceUnitOverridesTests extends BaseUnitTestCase {
Collections.singletonMap( AvailableSettings.JPA_JTA_DATASOURCE, integrationDataSource )
);
// first let's check the DataSource used in the EMF...
final ConnectionProvider connectionProvider = emf.unwrap( SessionFactoryImplementor.class )
.getServiceRegistry()
.getService( ConnectionProvider.class );
assertThat( connectionProvider, instanceOf( DatasourceConnectionProviderImpl.class ) );
final DatasourceConnectionProviderImpl dsCp = (DatasourceConnectionProviderImpl) connectionProvider;
assertThat( dsCp.getDataSource(), is( integrationDataSource ) );
try {
// first let's check the DataSource used in the EMF...
final ConnectionProvider connectionProvider = emf.unwrap( SessionFactoryImplementor.class )
.getServiceRegistry()
.getService( ConnectionProvider.class );
assertThat( connectionProvider, instanceOf( DatasourceConnectionProviderImpl.class ) );
final DatasourceConnectionProviderImpl dsCp = (DatasourceConnectionProviderImpl) connectionProvider;
assertThat( dsCp.getDataSource(), is( integrationDataSource ) );
// now let's check that it is exposed via the EMF properties
// - note : the spec does not indicate that this should work, but
// it worked this way in previous versions
final Object jtaDs = emf.getProperties().get( AvailableSettings.JPA_JTA_DATASOURCE );
assertThat( jtaDs, is( integrationDataSource ) );
// now let's check that it is exposed via the EMF properties
// - note : the spec does not indicate that this should work, but
// it worked this way in previous versions
final Object jtaDs = emf.getProperties().get( AvailableSettings.JPA_JTA_DATASOURCE );
assertThat( jtaDs, is( integrationDataSource ) );
// Additionally, we should have set Hibernate's DATASOURCE setting
final Object hibDs = emf.getProperties().get( AvailableSettings.JPA_JTA_DATASOURCE );
assertThat( hibDs, is( integrationDataSource ) );
// Additionally, we should have set Hibernate's DATASOURCE setting
final Object hibDs = emf.getProperties().get( AvailableSettings.JPA_JTA_DATASOURCE );
assertThat( hibDs, is( integrationDataSource ) );
// Make sure the non-jta-data-source setting was cleared or otherwise null
final Object nonJtaDs = emf.getProperties().get( AvailableSettings.JPA_NON_JTA_DATASOURCE );
assertThat( nonJtaDs, nullValue() );
// Make sure the non-jta-data-source setting was cleared or otherwise null
final Object nonJtaDs = emf.getProperties().get( AvailableSettings.JPA_NON_JTA_DATASOURCE );
assertThat( nonJtaDs, nullValue() );
}
finally {
emf.close();
}
}
@Test
@ -362,9 +366,101 @@ public class PersistenceUnitOverridesTests extends BaseUnitTestCase {
integrationSettings
);
final SessionFactoryImplementor sessionFactory = emf.unwrap( SessionFactoryImplementor.class );
final ConnectionProvider connectionProvider = sessionFactory.getServiceRegistry().getService( ConnectionProvider.class );
assertThat( connectionProvider, instanceOf( DriverManagerConnectionProviderImpl.class ) );
try {
final SessionFactoryImplementor sessionFactory = emf.unwrap( SessionFactoryImplementor.class );
final ConnectionProvider connectionProvider = sessionFactory.getServiceRegistry().getService(
ConnectionProvider.class );
assertThat( connectionProvider, instanceOf( DriverManagerConnectionProviderImpl.class ) );
}
finally {
emf.close();
}
}
@Test
public void testCfgXmlBaseline() {
final PersistenceUnitInfoAdapter info = new PersistenceUnitInfoAdapter() {
private final Properties props = new Properties();
{
props.put( org.hibernate.jpa.AvailableSettings.CFG_FILE, "org/hibernate/orm/test/bootstrap/jpa/hibernate.cfg.xml" );
}
@Override
public Properties getProperties() {
return props;
}
};
final PersistenceProvider provider = new HibernatePersistenceProvider();
final Map integrationSettings = Collections.emptyMap();
final EntityManagerFactory emf = provider.createContainerEntityManagerFactory(
info,
integrationSettings
);
try {
assertThat(
emf.getProperties().get( AvailableSettings.DIALECT ),
is( PersistenceUnitDialect.class.getName() )
);
assertThat(
emf.unwrap( SessionFactoryImplementor.class ).getJdbcServices().getDialect(),
instanceOf( PersistenceUnitDialect.class )
);
}
finally {
emf.close();
}
}
@Test
public void testIntegrationOverridesOfCfgXml() {
final PersistenceUnitInfoAdapter info = new PersistenceUnitInfoAdapter() {
private final Properties props = new Properties();
{
props.put( org.hibernate.jpa.AvailableSettings.CFG_FILE, "org/hibernate/orm/test/bootstrap/jpa/hibernate.cfg.xml" );
}
@Override
public Properties getProperties() {
return props;
}
};
final PersistenceProvider provider = new HibernatePersistenceProvider();
final Map integrationSettings = Collections.singletonMap(
AvailableSettings.DIALECT,
IntegrationDialect.class.getName()
);
final EntityManagerFactory emf = provider.createContainerEntityManagerFactory(
info,
integrationSettings
);
try {
assertThat(
emf.getProperties().get( AvailableSettings.DIALECT ),
is( IntegrationDialect.class.getName() )
);
assertThat(
emf.unwrap( SessionFactoryImplementor.class ).getJdbcServices().getDialect(),
instanceOf( IntegrationDialect.class )
);
}
finally {
emf.close();
}
}
public static class PersistenceUnitDialect extends Dialect {
}
@SuppressWarnings("WeakerAccess")
public static class IntegrationDialect extends Dialect {
}
}

View File

@ -0,0 +1,12 @@
<!--
~ 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
-->
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.orm.test.bootstrap.jpa.PersistenceUnitOverridesTests$PersistenceUnitDialect</property>
</session-factory>
</hibernate-configuration>