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