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();
}
}
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();
}
}
}
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();
}
}
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);
}
}
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();
}
}
Aggregations