Search in sources :

Example 1 with DelegatingPersistenceUnitInfo

use of org.hibernate.testing.util.jpa.DelegatingPersistenceUnitInfo in project hibernate-orm by hibernate.

the class PersistenceUnitOverridesTests method testPassingIntegrationJpaJdbcOverrideForJtaDataSourceProperty.

@Test
public void testPassingIntegrationJpaJdbcOverrideForJtaDataSourceProperty() {
    PersistenceProvider provider = new HibernatePersistenceProvider() {

        @Override
        public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map integrationOverrides) {
            return super.createContainerEntityManagerFactory(new DelegatingPersistenceUnitInfo(info) {

                // inject a JPA JTA DataSource setting into the PU
                final DataSource puDataSource;

                final Properties puProperties;

                {
                    puDataSource = new DataSourceStub("puDataSource");
                    puProperties = new Properties();
                    puProperties.putAll(info.getProperties());
                    puProperties.put(AvailableSettings.JPA_JTA_DATASOURCE, puDataSource);
                }

                @Override
                public DataSource getJtaDataSource() {
                    return null;
                }

                @Override
                public DataSource getNonJtaDataSource() {
                    return null;
                }

                @Override
                public Properties getProperties() {
                    return puProperties;
                }
            }, integrationOverrides);
        }
    };
    final EntityManagerFactory emf = provider.createContainerEntityManagerFactory(new PersistenceUnitInfoAdapter(), // a JTA DataSource (nor the reverse).  However, that is a useful thing to support
    ConnectionProviderBuilder.getJpaConnectionProviderProperties("db2"));
    try {
        final Map<String, Object> properties = emf.getProperties();
        final Object hibernateJdbcDriver = properties.get(AvailableSettings.URL);
        assertThat(hibernateJdbcDriver, notNullValue());
        final Object jpaJdbcDriver = properties.get(AvailableSettings.JPA_JDBC_URL);
        assertThat((String) jpaJdbcDriver, containsString("db2"));
        // see if the values had the affect to adjust the `ConnectionProvider` used
        final ConnectionProvider connectionProvider = emf.unwrap(SessionFactoryImplementor.class).getServiceRegistry().getService(ConnectionProvider.class);
        assertThat(connectionProvider, instanceOf(DriverManagerConnectionProviderImpl.class));
    } finally {
        emf.close();
    }
}
Also used : DelegatingPersistenceUnitInfo(org.hibernate.testing.util.jpa.DelegatingPersistenceUnitInfo) PersistenceProvider(jakarta.persistence.spi.PersistenceProvider) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Properties(java.util.Properties) PersistenceUnitInfo(jakarta.persistence.spi.PersistenceUnitInfo) DelegatingPersistenceUnitInfo(org.hibernate.testing.util.jpa.DelegatingPersistenceUnitInfo) PersistenceUnitInfoAdapter(org.hibernate.testing.util.jpa.PersistenceUnitInfoAdapter) DataSource(javax.sql.DataSource) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) DriverManagerConnectionProviderImpl(org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl) DataSourceStub(org.hibernate.testing.jdbc.DataSourceStub) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with DelegatingPersistenceUnitInfo

use of org.hibernate.testing.util.jpa.DelegatingPersistenceUnitInfo in project hibernate-orm by hibernate.

the class PersistenceUnitOverridesTests method testPassingIntegrationJpaDataSourceOverrideForJtaDataSourceElement.

@Test
public // )
void testPassingIntegrationJpaDataSourceOverrideForJtaDataSourceElement() {
    final DataSource puDataSource = new DataSourceStub("puDataSource");
    final DataSource integrationDataSource = new DataSourceStub("integrationDataSource");
    PersistenceProvider provider = new HibernatePersistenceProvider() {

        @Override
        public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map integrationOverrides) {
            return super.createContainerEntityManagerFactory(new DelegatingPersistenceUnitInfo(info) {

                @Override
                public DataSource getJtaDataSource() {
                    // - as opposed using `javax.persistence.jtaDataSource` under the `properties` element
                    return puDataSource;
                }
            }, integrationOverrides);
        }
    };
    final Map integrationOverrides = new HashMap();
    // noinspection unchecked
    integrationOverrides.put(AvailableSettings.JPA_JTA_DATASOURCE, integrationDataSource);
    // todo (6.0) : fix for Oracle see HHH-13432
    // integrationOverrides.put( AvailableSettings.HQL_BULK_ID_STRATEGY, new MultiTableBulkIdStrategyStub() );
    final EntityManagerFactory emf = provider.createContainerEntityManagerFactory(new PersistenceUnitInfoAdapter(), integrationOverrides);
    try {
        final Map<String, Object> properties = emf.getProperties();
        final Object datasource = properties.get(AvailableSettings.JPA_JTA_DATASOURCE);
        assertThat(datasource, is(integrationDataSource));
        // see if the values had the affect to adjust the `ConnectionProvider` used
        final ConnectionProvider connectionProvider = emf.unwrap(SessionFactoryImplementor.class).getServiceRegistry().getService(ConnectionProvider.class);
        assertThat(connectionProvider, instanceOf(DatasourceConnectionProviderImpl.class));
        final DatasourceConnectionProviderImpl datasourceConnectionProvider = (DatasourceConnectionProviderImpl) connectionProvider;
        assertThat(datasourceConnectionProvider.getDataSource(), is(integrationDataSource));
    } finally {
        emf.close();
    }
}
Also used : DelegatingPersistenceUnitInfo(org.hibernate.testing.util.jpa.DelegatingPersistenceUnitInfo) HashMap(java.util.HashMap) PersistenceProvider(jakarta.persistence.spi.PersistenceProvider) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) PersistenceUnitInfo(jakarta.persistence.spi.PersistenceUnitInfo) DelegatingPersistenceUnitInfo(org.hibernate.testing.util.jpa.DelegatingPersistenceUnitInfo) PersistenceUnitInfoAdapter(org.hibernate.testing.util.jpa.PersistenceUnitInfoAdapter) DataSource(javax.sql.DataSource) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) DataSourceStub(org.hibernate.testing.jdbc.DataSourceStub) DatasourceConnectionProviderImpl(org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 3 with DelegatingPersistenceUnitInfo

use of org.hibernate.testing.util.jpa.DelegatingPersistenceUnitInfo in project hibernate-orm by hibernate.

the class PersistenceUnitOverridesTests method testPassingIntegrationJpaJdbcOverridesForJtaDataSourceElement.

@Test
public // )
void testPassingIntegrationJpaJdbcOverridesForJtaDataSourceElement() {
    PersistenceProvider provider = new HibernatePersistenceProvider() {

        @Override
        public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map integrationOverrides) {
            return super.createContainerEntityManagerFactory(new DelegatingPersistenceUnitInfo(info) {

                // inject a JPA JTA DataSource setting into the PU
                final DataSource puDataSource = new DataSourceStub("puDataSource");

                @Override
                public DataSource getJtaDataSource() {
                    return puDataSource;
                }
            }, integrationOverrides);
        }
    };
    final EntityManagerFactory emf = provider.createContainerEntityManagerFactory(new PersistenceUnitInfoAdapter(), // a JTA DataSource (nor the reverse).  However, that is a useful thing to support
    ConnectionProviderBuilder.getJpaConnectionProviderProperties("db2"));
    try {
        final Map<String, Object> properties = emf.getProperties();
        final Object hibernateJdbcDriver = properties.get(AvailableSettings.URL);
        assertThat(hibernateJdbcDriver, notNullValue());
        final Object jpaJdbcDriver = properties.get(AvailableSettings.JPA_JDBC_URL);
        assertThat((String) jpaJdbcDriver, containsString("db2"));
        // see if the values had the affect to adjust the `ConnectionProvider` used
        final ConnectionProvider connectionProvider = emf.unwrap(SessionFactoryImplementor.class).getServiceRegistry().getService(ConnectionProvider.class);
        assertThat(connectionProvider, instanceOf(DriverManagerConnectionProviderImpl.class));
    } finally {
        emf.close();
    }
}
Also used : DelegatingPersistenceUnitInfo(org.hibernate.testing.util.jpa.DelegatingPersistenceUnitInfo) PersistenceProvider(jakarta.persistence.spi.PersistenceProvider) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) PersistenceUnitInfo(jakarta.persistence.spi.PersistenceUnitInfo) DelegatingPersistenceUnitInfo(org.hibernate.testing.util.jpa.DelegatingPersistenceUnitInfo) PersistenceUnitInfoAdapter(org.hibernate.testing.util.jpa.PersistenceUnitInfoAdapter) DataSource(javax.sql.DataSource) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) DriverManagerConnectionProviderImpl(org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl) DataSourceStub(org.hibernate.testing.jdbc.DataSourceStub) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

EntityManagerFactory (jakarta.persistence.EntityManagerFactory)3 PersistenceProvider (jakarta.persistence.spi.PersistenceProvider)3 PersistenceUnitInfo (jakarta.persistence.spi.PersistenceUnitInfo)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 DataSource (javax.sql.DataSource)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 ConnectionProvider (org.hibernate.engine.jdbc.connections.spi.ConnectionProvider)3 HibernatePersistenceProvider (org.hibernate.jpa.HibernatePersistenceProvider)3 DataSourceStub (org.hibernate.testing.jdbc.DataSourceStub)3 DelegatingPersistenceUnitInfo (org.hibernate.testing.util.jpa.DelegatingPersistenceUnitInfo)3 PersistenceUnitInfoAdapter (org.hibernate.testing.util.jpa.PersistenceUnitInfoAdapter)3 Test (org.junit.Test)3 DriverManagerConnectionProviderImpl (org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl)2 Properties (java.util.Properties)1 DatasourceConnectionProviderImpl (org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl)1