Example usage for org.hibernate.engine.jdbc.connections.spi ConnectionProvider ConnectionProvider

List of usage examples for org.hibernate.engine.jdbc.connections.spi ConnectionProvider ConnectionProvider

Introduction

In this page you can find the example usage for org.hibernate.engine.jdbc.connections.spi ConnectionProvider ConnectionProvider.

Prototype

ConnectionProvider

Source Link

Usage

From source file:org.jooq.example.jpa.JPAExample.java

License:Apache License

@SuppressWarnings("serial")
public static void main(String[] args) throws Exception {
    Connection connection = null;
    EntityManagerFactory emf = null;/*  w  ww  .  j a va  2s .co  m*/
    EntityManager em = null;

    try {

        // Bootstrapping JDBC:
        Class.forName("org.h2.Driver");
        connection = DriverManager.getConnection("jdbc:h2:mem:jooq-jpa-example", "sa", "");
        final Connection c = connection;

        // Creating an in-memory H2 database from our entities
        MetadataSources metadata = new MetadataSources(new StandardServiceRegistryBuilder()
                .applySetting("hibernate.dialect", "org.hibernate.dialect.H2Dialect")
                .applySetting("javax.persistence.schema-generation-connection", connection)
                .applySetting("javax.persistence.create-database-schemas", true)

                // [#5607] JPADatabase causes warnings - This prevents
                // them
                .applySetting(AvailableSettings.CONNECTION_PROVIDER, new ConnectionProvider() {
                    @SuppressWarnings("rawtypes")
                    @Override
                    public boolean isUnwrappableAs(Class unwrapType) {
                        return false;
                    }

                    @Override
                    public <T> T unwrap(Class<T> unwrapType) {
                        return null;
                    }

                    @Override
                    public Connection getConnection() {
                        return c;
                    }

                    @Override
                    public void closeConnection(Connection conn) throws SQLException {
                    }

                    @Override
                    public boolean supportsAggressiveRelease() {
                        return true;
                    }
                }).build());

        metadata.addAnnotatedClass(Actor.class);
        metadata.addAnnotatedClass(Film.class);
        metadata.addAnnotatedClass(Language.class);

        SchemaExport export = new SchemaExport();
        export.create(EnumSet.of(TargetType.DATABASE), metadata.buildMetadata());

        // Setting up an EntityManager using Spring (much easier than out-of-the-box Hibernate)
        LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
        HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
        adapter.setDatabasePlatform(SQLDialect.H2.thirdParty().hibernateDialect());
        bean.setDataSource(new SingleConnectionDataSource(connection, true));
        bean.setPackagesToScan("org.jooq.example.jpa.entity");
        bean.setJpaVendorAdapter(adapter);
        bean.setPersistenceUnitName("test");
        bean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
        bean.afterPropertiesSet();

        emf = bean.getObject();
        em = emf.createEntityManager();

        final EntityManager e = em;

        // Run some Hibernate / jOOQ logic inside of a transaction
        em.getTransaction().begin();
        run(em, DSL.using(new DefaultConfiguration().set(connection).set(new DefaultExecuteListener() {
            @Override
            public void start(ExecuteContext ctx) {
                // Flush all changes from the EntityManager to the database for them to be visible in jOOQ
                e.flush();
                super.start(ctx);
            }
        })));
        em.getTransaction().commit();
    } finally {
        if (em != null)
            em.close();

        if (emf != null)
            emf.close();

        if (connection != null)
            connection.close();
    }
}

From source file:org.jooq.example.jpa.Setup.java

License:Apache License

static void run(BiConsumer<EntityManager, DSLContext> consumer) throws Exception {
    Connection connection = null;
    EntityManagerFactory emf = null;/*from   w w w  . j a  va  2s. co  m*/
    EntityManager em = null;

    try {

        // Bootstrapping JDBC:
        Class.forName("org.h2.Driver");
        connection = new LoggingConnection(
                DriverManager.getConnection("jdbc:h2:mem:jooq-jpa-example", "sa", ""));
        final Connection c = connection;

        // Creating an in-memory H2 database from our entities
        MetadataSources metadata = new MetadataSources(new StandardServiceRegistryBuilder()
                .applySetting("hibernate.dialect", "org.hibernate.dialect.H2Dialect")
                .applySetting("javax.persistence.schema-generation-connection", connection)
                .applySetting("javax.persistence.create-database-schemas", true)

                // [#5607] JPADatabase causes warnings - This prevents
                // them
                .applySetting(AvailableSettings.CONNECTION_PROVIDER, new ConnectionProvider() {
                    @SuppressWarnings("rawtypes")
                    @Override
                    public boolean isUnwrappableAs(Class unwrapType) {
                        return false;
                    }

                    @Override
                    public <T> T unwrap(Class<T> unwrapType) {
                        return null;
                    }

                    @Override
                    public Connection getConnection() {
                        return c;
                    }

                    @Override
                    public void closeConnection(Connection conn) throws SQLException {
                    }

                    @Override
                    public boolean supportsAggressiveRelease() {
                        return true;
                    }
                }).build());

        metadata.addAnnotatedClass(Actor.class);
        metadata.addAnnotatedClass(Film.class);
        metadata.addAnnotatedClass(Language.class);

        SchemaExport export = new SchemaExport();
        export.create(EnumSet.of(TargetType.DATABASE), metadata.buildMetadata());

        // Setting up an EntityManager using Spring (much easier than out-of-the-box Hibernate)
        LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
        HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
        adapter.setDatabasePlatform(SQLDialect.H2.thirdParty().hibernateDialect());
        bean.setDataSource(new SingleConnectionDataSource(connection, true));
        bean.setPackagesToScan("org.jooq.example.jpa.entity");
        bean.setJpaVendorAdapter(adapter);
        bean.setPersistenceUnitName("test");
        bean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
        bean.afterPropertiesSet();

        emf = bean.getObject();
        em = emf.createEntityManager();

        final EntityManager e = em;

        // Run some Hibernate / jOOQ logic inside of a transaction
        em.getTransaction().begin();
        data(em);

        consumer.accept(em,
                DSL.using(new DefaultConfiguration().set(connection).set(new DefaultExecuteListener() {
                    @Override
                    public void start(ExecuteContext ctx) {
                        // Flush all changes from the EntityManager to the database for them to be visible in jOOQ
                        e.flush();
                        super.start(ctx);
                    }
                })));
        em.getTransaction().commit();
    } finally {
        if (em != null)
            em.close();

        if (emf != null)
            emf.close();

        if (connection != null)
            connection.close();
    }
}

From source file:org.jooq.meta.extensions.jpa.JPADatabase.java

License:Apache License

@SuppressWarnings("serial")
ConnectionProvider connectionProvider() {
    return new ConnectionProvider() {
        @SuppressWarnings("rawtypes")
        @Override//from w w  w .  j av a2s . c  o m
        public boolean isUnwrappableAs(Class unwrapType) {
            return false;
        }

        @Override
        public <T> T unwrap(Class<T> unwrapType) {
            return null;
        }

        @Override
        public Connection getConnection() {
            return connection;
        }

        @Override
        public void closeConnection(Connection conn) {
        }

        @Override
        public boolean supportsAggressiveRelease() {
            return true;
        }
    };
}