Search in sources :

Example 1 with SingleConnectionDataSource

use of org.jooq.tools.jdbc.SingleConnectionDataSource in project jOOQ by jOOQ.

the class Setup method run.

// This class sets up an EntityManager and configures the jOOQ DSLContext
// ----------------------------------------------------------------------
static void run(BiConsumer<EntityManager, DSLContext> consumer) throws Exception {
    Connection connection = null;
    EntityManagerFactory emf = null;
    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).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());
        Map<Object, Object> props = new HashMap<>();
        DataSource ds = new SingleConnectionDataSource(connection);
        props.put("hibernate.connection.datasource", ds);
        props.put("hibernate.archive.autodetection", "");
        emf = new HibernatePersistenceProvider().createContainerEntityManagerFactory(pui(ds), props);
        em = emf.createEntityManager();
        final EntityManager e = em;
        // Run some Hibernate / jOOQ logic inside of a transaction
        em.getTransaction().begin();
        data(em);
        consumer.accept(em, new DefaultConfiguration().set(connection).set(onStart(ctx -> e.flush())).dsl());
        em.getTransaction().commit();
    } finally {
        if (em != null)
            em.close();
        if (emf != null)
            emf.close();
        if (connection != null)
            connection.close();
    }
}
Also used : LoggingConnection(org.jooq.tools.jdbc.LoggingConnection) Language(org.jooq.example.jpa.entity.Language) Arrays(java.util.Arrays) Connection(java.sql.Connection) URL(java.net.URL) AvailableSettings(org.hibernate.cfg.AvailableSettings) PersistenceUnitTransactionType(jakarta.persistence.spi.PersistenceUnitTransactionType) HashMap(java.util.HashMap) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Film(org.jooq.example.jpa.entity.Film) ValidationMode(jakarta.persistence.ValidationMode) ExecuteListener.onStart(org.jooq.ExecuteListener.onStart) Title(org.jooq.example.jpa.embeddables.Title) SQLException(java.sql.SQLException) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) DataSource(javax.sql.DataSource) DSLContext(org.jooq.DSLContext) ClassTransformer(jakarta.persistence.spi.ClassTransformer) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport) EnumSet(java.util.EnumSet) SharedCacheMode(jakarta.persistence.SharedCacheMode) TargetType(org.hibernate.tool.schema.TargetType) LoggingConnection(org.jooq.tools.jdbc.LoggingConnection) Properties(java.util.Properties) DefaultConfiguration(org.jooq.impl.DefaultConfiguration) IOException(java.io.IOException) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) PersistenceUnitInfo(jakarta.persistence.spi.PersistenceUnitInfo) MetadataSources(org.hibernate.boot.MetadataSources) UncheckedIOException(java.io.UncheckedIOException) List(java.util.List) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) Year(java.time.Year) EntityManager(jakarta.persistence.EntityManager) Actor(org.jooq.example.jpa.entity.Actor) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) Collections(java.util.Collections) DriverManager(java.sql.DriverManager) SingleConnectionDataSource(org.jooq.tools.jdbc.SingleConnectionDataSource) SingleConnectionDataSource(org.jooq.tools.jdbc.SingleConnectionDataSource) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SQLException(java.sql.SQLException) HashMap(java.util.HashMap) Connection(java.sql.Connection) LoggingConnection(org.jooq.tools.jdbc.LoggingConnection) MetadataSources(org.hibernate.boot.MetadataSources) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) DefaultConfiguration(org.jooq.impl.DefaultConfiguration) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) DataSource(javax.sql.DataSource) SingleConnectionDataSource(org.jooq.tools.jdbc.SingleConnectionDataSource) EntityManager(jakarta.persistence.EntityManager) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport)

Example 2 with SingleConnectionDataSource

use of org.jooq.tools.jdbc.SingleConnectionDataSource in project jOOQ by jOOQ.

the class FlywayTest method setup.

@Before
public void setup() throws SQLException {
    Properties info = new Properties();
    info.put("user", "sa");
    info.put("password", "");
    connection = new org.h2.Driver().connect("jdbc:h2:mem:jooq-example-flyway-ddl-" + UUID.randomUUID(), info);
    // This flyway migration should produce the same database as the DDLDatabase
    // used by the code generator, as it is configured using flyway file sorting
    Flyway flyway = Flyway.configure().dataSource(new SingleConnectionDataSource(connection)).load();
    flyway.migrate();
}
Also used : Flyway(org.flywaydb.core.Flyway) SingleConnectionDataSource(org.jooq.tools.jdbc.SingleConnectionDataSource) Properties(java.util.Properties) Before(org.junit.Before)

Aggregations

Properties (java.util.Properties)2 SingleConnectionDataSource (org.jooq.tools.jdbc.SingleConnectionDataSource)2 EntityManager (jakarta.persistence.EntityManager)1 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)1 SharedCacheMode (jakarta.persistence.SharedCacheMode)1 ValidationMode (jakarta.persistence.ValidationMode)1 ClassTransformer (jakarta.persistence.spi.ClassTransformer)1 PersistenceUnitInfo (jakarta.persistence.spi.PersistenceUnitInfo)1 PersistenceUnitTransactionType (jakarta.persistence.spi.PersistenceUnitTransactionType)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 URL (java.net.URL)1 Connection (java.sql.Connection)1 DriverManager (java.sql.DriverManager)1 SQLException (java.sql.SQLException)1 Year (java.time.Year)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 EnumSet (java.util.EnumSet)1 HashMap (java.util.HashMap)1