Search in sources :

Example 6 with PersistenceUnitInfoAdapter

use of org.hibernate.testing.util.jpa.PersistenceUnitInfoAdapter 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 7 with PersistenceUnitInfoAdapter

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

the class PersistenceUnitOverridesTests method testIntegrationOverridesOfPersistenceXmlDataSourceWithDriverManagerInfoUsingJakarta.

@Test
@TestForIssue(jiraKey = "HHH-13640")
public void testIntegrationOverridesOfPersistenceXmlDataSourceWithDriverManagerInfoUsingJakarta() {
    // mimics a DataSource defined in the persistence.xml
    final DataSourceStub dataSource = new DataSourceStub("puDataSource");
    final PersistenceUnitInfoAdapter info = new PersistenceUnitInfoAdapter() {

        @Override
        public DataSource getNonJtaDataSource() {
            return dataSource;
        }
    };
    final Map<String, Object> integrationSettings = new HashMap<>();
    integrationSettings.put(AvailableSettings.JAKARTA_JDBC_DRIVER, ConnectionProviderBuilder.DRIVER);
    integrationSettings.put(AvailableSettings.JAKARTA_JDBC_URL, ConnectionProviderBuilder.URL);
    integrationSettings.put(AvailableSettings.JAKARTA_JDBC_USER, ConnectionProviderBuilder.USER);
    integrationSettings.put(AvailableSettings.JAKARTA_JDBC_PASSWORD, ConnectionProviderBuilder.PASS);
    integrationSettings.put("hibernate.connection.init_sql", "");
    final PersistenceProvider provider = new HibernatePersistenceProvider();
    final EntityManagerFactory emf = provider.createContainerEntityManagerFactory(info, integrationSettings);
    try {
        final SessionFactoryImplementor sessionFactory = emf.unwrap(SessionFactoryImplementor.class);
        final ConnectionProvider connectionProvider = sessionFactory.getServiceRegistry().getService(ConnectionProvider.class);
        assertThat(connectionProvider, instanceOf(DriverManagerConnectionProviderImpl.class));
    } finally {
        emf.close();
    }
}
Also used : DriverManagerConnectionProviderImpl(org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl) DataSourceStub(org.hibernate.testing.jdbc.DataSourceStub) HashMap(java.util.HashMap) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) PersistenceProvider(jakarta.persistence.spi.PersistenceProvider) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) PersistenceUnitInfoAdapter(org.hibernate.testing.util.jpa.PersistenceUnitInfoAdapter) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 8 with PersistenceUnitInfoAdapter

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

the class DataSourceInjectionTest method testDatasourceInjection.

@Test
public void testDatasourceInjection() throws Exception {
    withPuRoot(puRootUrl -> {
        final PersistenceUnitInfoAdapter persistenceUnitInfo = createPuDescriptor(puRootUrl, new FakeDataSource());
        // otherwise the FakeDataSourceException will be eaten trying to resolve the Dialect
        final Map<String, Object> intgOverrides = Collections.singletonMap(AvailableSettings.DIALECT, H2Dialect.class);
        final HibernatePersistenceProvider provider = new HibernatePersistenceProvider();
        try (final SessionFactoryImplementor sf = provider.createContainerEntityManagerFactory(persistenceUnitInfo, intgOverrides).unwrap(SessionFactoryImplementor.class)) {
            try (final SessionImplementor session = sf.openSession().unwrap(SessionImplementor.class)) {
                session.createQuery("select i from Item i").list();
                Assertions.fail("Expecting FakeDataSourceException");
            } catch (PersistenceException pe) {
                try {
                    throw (RuntimeException) pe.getCause();
                } catch (FakeDataSourceException fde) {
                // success
                }
            } catch (FakeDataSourceException fde) {
            // success
            }
        }
    });
}
Also used : SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) PersistenceException(jakarta.persistence.PersistenceException) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) PersistenceUnitInfoAdapter(org.hibernate.testing.util.jpa.PersistenceUnitInfoAdapter) Test(org.junit.jupiter.api.Test)

Example 9 with PersistenceUnitInfoAdapter

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

the class PersistenceUnitOverridesTests method testCfgXmlBaseline.

@Test
public void testCfgXmlBaseline() {
    final PersistenceUnitInfoAdapter info = new PersistenceUnitInfoAdapter() {

        private final Properties props = new Properties();

        {
            props.put(AvailableSettings.CFG_XML_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));
        final EntityType<MappedEntity> entityMapping = emf.getMetamodel().entity(MappedEntity.class);
        assertThat(entityMapping, notNullValue());
    } finally {
        emf.close();
    }
}
Also used : SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) PersistenceProvider(jakarta.persistence.spi.PersistenceProvider) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) Properties(java.util.Properties) HashMap(java.util.HashMap) Map(java.util.Map) PersistenceUnitInfoAdapter(org.hibernate.testing.util.jpa.PersistenceUnitInfoAdapter) Test(org.junit.Test)

Example 10 with PersistenceUnitInfoAdapter

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

the class PersistenceUnitOverridesTests method testPassingIntegrationJpaJdbcOverrides.

@Test
public void testPassingIntegrationJpaJdbcOverrides() {
    // the integration overrides say to use the "db2" JPA connection settings (which should override the persistence unit values)
    final Map integrationOverrides = ConnectionProviderBuilder.getJpaConnectionProviderProperties("db2");
    final EntityManagerFactory emf = new HibernatePersistenceProvider().createContainerEntityManagerFactory(new PersistenceUnitInfoAdapter() {

        @Override
        public Properties getProperties() {
            // effectively, the `persistence.xml` defines `db1` as the connection settings
            return ConnectionProviderBuilder.getJpaConnectionProviderProperties("db1");
        }
    }, integrationOverrides);
    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"));
    } finally {
        emf.close();
    }
}
Also used : EntityManagerFactory(jakarta.persistence.EntityManagerFactory) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Properties(java.util.Properties) HashMap(java.util.HashMap) Map(java.util.Map) PersistenceUnitInfoAdapter(org.hibernate.testing.util.jpa.PersistenceUnitInfoAdapter) Test(org.junit.Test)

Aggregations

HibernatePersistenceProvider (org.hibernate.jpa.HibernatePersistenceProvider)13 PersistenceUnitInfoAdapter (org.hibernate.testing.util.jpa.PersistenceUnitInfoAdapter)13 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)12 Test (org.junit.Test)12 PersistenceProvider (jakarta.persistence.spi.PersistenceProvider)10 HashMap (java.util.HashMap)9 ConnectionProvider (org.hibernate.engine.jdbc.connections.spi.ConnectionProvider)9 DataSourceStub (org.hibernate.testing.jdbc.DataSourceStub)9 DataSource (javax.sql.DataSource)7 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)7 Map (java.util.Map)6 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)6 DatasourceConnectionProviderImpl (org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl)5 TestForIssue (org.hibernate.testing.TestForIssue)5 Properties (java.util.Properties)4 DriverManagerConnectionProviderImpl (org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl)4 PersistenceUnitInfo (jakarta.persistence.spi.PersistenceUnitInfo)3 DelegatingPersistenceUnitInfo (org.hibernate.testing.util.jpa.DelegatingPersistenceUnitInfo)3 PersistenceException (jakarta.persistence.PersistenceException)1 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)1