Search in sources :

Example 1 with PersistenceProvider

use of org.eclipse.persistence.jpa.PersistenceProvider in project opencast by opencast.

the class TestUtil method newTestEntityManagerFactory.

/**
 * Create a new entity manager factory backed by an in-memory H2 database for testing purposes.
 *
 * @param emName
 *          name of the persistence unit (see META-INF/persistence.xml)
 */
public static EntityManagerFactory newTestEntityManagerFactory(String emName) {
    // Set up the database
    ComboPooledDataSource pooledDataSource = new ComboPooledDataSource();
    try {
        pooledDataSource.setDriverClass("org.h2.Driver");
    } catch (PropertyVetoException e) {
        throw new RuntimeException(e);
    }
    pooledDataSource.setJdbcUrl("jdbc:h2:./target/db" + System.currentTimeMillis());
    pooledDataSource.setUser("sa");
    pooledDataSource.setPassword("sa");
    // Set up the persistence properties
    Map<String, Object> persistenceProps = new HashMap<String, Object>();
    persistenceProps.put("javax.persistence.nonJtaDataSource", pooledDataSource);
    persistenceProps.put("eclipselink.ddl-generation", "create-tables");
    persistenceProps.put("eclipselink.ddl-generation.output-mode", "database");
    PersistenceProvider pp = new PersistenceProvider();
    return pp.createEntityManagerFactory(emName, persistenceProps);
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) ComboPooledDataSource(com.mchange.v2.c3p0.ComboPooledDataSource) HashMap(java.util.HashMap) PersistenceProvider(org.eclipse.persistence.jpa.PersistenceProvider)

Example 2 with PersistenceProvider

use of org.eclipse.persistence.jpa.PersistenceProvider in project adeptj-modules by AdeptJ.

the class EntityManagerFactoryProvider method start.

// ---------------- INTERNAL ----------------
@Activate
protected void start(BundleContext context, EntityManagerFactoryConfig config) {
    this.lock.lock();
    try {
        String unitName = config.unitName();
        Validate.isTrue(isNotEmpty(unitName), "unitName can't be null or empty!!");
        LOGGER.info("Creating EntityManagerFactory for PersistenceUnit: [{}]", unitName);
        DataSource ds = Validate.notNull(this.dataSourceProvider.getDataSource(config.dataSourceName()), DS_NOT_NULL_MSG);
        this.emf = new PersistenceProvider().createEntityManagerFactory(unitName, JpaProperties.create(config, ds, this.validatorService.getValidatorFactory()));
        if (this.emf == null) {
            throw new IllegalStateException(EMF_NULL_MSG);
        } else {
            LOGGER.info("EntityManagerFactory [{}] created for PersistenceUnit: [{}]", this.emf, unitName);
            this.jpaCrudRepository = JpaCrudRepositories.create(unitName, this.emf, context);
        }
    } catch (Exception ex) {
        // NOSONAR
        LOGGER.error("Exception occurred while creating EntityManagerFactory!!", ex);
        // Close the EntityManagerFactory if it was created earlier but exception occurred later.
        Optional.ofNullable(this.emf).ifPresent(EntityManagerFactory::close);
        // Throw exception so that SCR won't create the component instance.
        throw new JpaBootstrapException(ex);
    } finally {
        this.lock.unlock();
    }
}
Also used : PersistenceProvider(org.eclipse.persistence.jpa.PersistenceProvider) DataSource(javax.sql.DataSource) Activate(org.osgi.service.component.annotations.Activate)

Aggregations

PersistenceProvider (org.eclipse.persistence.jpa.PersistenceProvider)2 ComboPooledDataSource (com.mchange.v2.c3p0.ComboPooledDataSource)1 PropertyVetoException (java.beans.PropertyVetoException)1 HashMap (java.util.HashMap)1 DataSource (javax.sql.DataSource)1 Activate (org.osgi.service.component.annotations.Activate)1