Search in sources :

Example 6 with HibernatePersistenceProvider

use of org.hibernate.jpa.HibernatePersistenceProvider in project hibernate-orm by hibernate.

the class JpaXsdVersionsTest method testOrm20.

@Test
public void testOrm20() {
    PersistenceUnitInfoImpl pui = new PersistenceUnitInfoImpl("orm2-test", "2.0").addMappingFileName("org/hibernate/test/jpa/xml/versions/valid-orm-2_0.xml");
    HibernatePersistenceProvider hp = new HibernatePersistenceProvider();
    EntityManagerFactory emf = hp.createContainerEntityManagerFactory(pui, Collections.EMPTY_MAP);
    try {
        // exception if not entity
        emf.getMetamodel().entity(Lighter.class);
    } finally {
        emf.close();
    }
}
Also used : EntityManagerFactory(javax.persistence.EntityManagerFactory) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) Test(org.junit.Test)

Example 7 with HibernatePersistenceProvider

use of org.hibernate.jpa.HibernatePersistenceProvider in project hibernate-orm by hibernate.

the class JpaXsdVersionsTest method testInvalidOrm1.

@Test
public void testInvalidOrm1() {
    PersistenceUnitInfoImpl pui = new PersistenceUnitInfoImpl("invalid-orm1-test", "1.0").addMappingFileName("org/hibernate/test/jpa/xml/versions/invalid-orm-1_0.xml");
    HibernatePersistenceProvider hp = new HibernatePersistenceProvider();
    EntityManagerFactory emf = null;
    try {
        emf = hp.createContainerEntityManagerFactory(pui, Collections.EMPTY_MAP);
        Assert.fail("expecting 'invalid content' error");
    } catch (InvalidMappingException | AnnotationException expected) {
    // expected condition
    } catch (PersistenceException expected) {
    // expected condition
    } finally {
        if (emf != null) {
            emf.close();
        }
    }
}
Also used : InvalidMappingException(org.hibernate.InvalidMappingException) EntityManagerFactory(javax.persistence.EntityManagerFactory) PersistenceException(javax.persistence.PersistenceException) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) AnnotationException(org.hibernate.AnnotationException) Test(org.junit.Test)

Example 8 with HibernatePersistenceProvider

use of org.hibernate.jpa.HibernatePersistenceProvider in project hibernate-orm by hibernate.

the class JpaXsdVersionsTest method testOrm21.

@Test
public void testOrm21() {
    PersistenceUnitInfoImpl pui = new PersistenceUnitInfoImpl("orm2-test", "2.1").addMappingFileName("org/hibernate/test/jpa/xml/versions/valid-orm-2_1.xml");
    HibernatePersistenceProvider hp = new HibernatePersistenceProvider();
    EntityManagerFactory emf = hp.createContainerEntityManagerFactory(pui, Collections.EMPTY_MAP);
    try {
        // exception if not entity
        emf.getMetamodel().entity(Lighter.class);
    } finally {
        emf.close();
    }
}
Also used : EntityManagerFactory(javax.persistence.EntityManagerFactory) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) Test(org.junit.Test)

Example 9 with HibernatePersistenceProvider

use of org.hibernate.jpa.HibernatePersistenceProvider in project webpieces by deanhiller.

the class HibernateModule method loadByClassMeta.

private EntityManagerFactory loadByClassMeta(String clazz, MeterRegistry metrics, Injector injector) {
    log.info("Loading Hibernate from class meta.  ENTITY classloader=" + entityClassLoader + " hibernate classloader=" + this.getClass().getClassLoader() + " class=" + clazz);
    Class<?> loadClass = null;
    try {
        loadClass = entityClassLoader.loadClass(clazz);
        Object newInstance = injector.getInstance(loadClass);
        if (!(newInstance instanceof PersistenceUnitInfo))
            throw new IllegalArgumentException(clazz + " is not an instanceof PersistenceUnitInfo and must be");
        PersistenceUnitInfo info = (PersistenceUnitInfo) newInstance;
        // Can probably remove this proxy because overrideProperties should override BUT this needs lots of testing
        // on DevelopmentServer to make sure changes to code in many places don't break especially changing Dbo's
        PersistenceUnitInfo proxy = new PersistenceUnitInfoProxy(info, entityClassLoader);
        Map<String, Object> overrideProperties = createClassLoaderProperty();
        return new HibernatePersistenceProvider().createContainerEntityManagerFactory(proxy, overrideProperties);
    } catch (ClassNotFoundException | SecurityException e) {
        throw new IllegalStateException("Could not construct DB settings", e);
    }
}
Also used : HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) PersistenceUnitInfo(javax.persistence.spi.PersistenceUnitInfo)

Example 10 with HibernatePersistenceProvider

use of org.hibernate.jpa.HibernatePersistenceProvider 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)

Aggregations

HibernatePersistenceProvider (org.hibernate.jpa.HibernatePersistenceProvider)14 Test (org.junit.Test)10 EntityManagerFactory (javax.persistence.EntityManagerFactory)9 PersistenceException (javax.persistence.PersistenceException)3 URL (java.net.URL)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 AnnotationException (org.hibernate.AnnotationException)2 InvalidMappingException (org.hibernate.InvalidMappingException)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 File (java.io.File)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1