Java Code Examples for javax.persistence.spi.PersistenceProvider#createContainerEntityManagerFactory()

The following examples show how to use javax.persistence.spi.PersistenceProvider#createContainerEntityManagerFactory() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EntityManagerBean.java    From openwebbeans-meecrowave with Apache License 2.0 6 votes vote down vote up
void init(final PersistenceUnitInfo info, final BeanManager bm) {
    final PersistenceProvider provider;
    try {
        provider = PersistenceProvider.class.cast(
                Thread.currentThread().getContextClassLoader().loadClass(info.getPersistenceProviderClassName()).newInstance());
    } catch (final InstantiationException | IllegalAccessException | ClassNotFoundException e) {
        throw new IllegalArgumentException("Bad provider: " + info.getPersistenceProviderClassName());
    }
    final EntityManagerFactory factory = provider.createContainerEntityManagerFactory(info, new HashMap() {{
        put("javax.persistence.bean.manager", bm);
        if (ValidationMode.NONE != info.getValidationMode()) {
            ofNullable(findValidatorFactory(bm)).ifPresent(factory -> put("javax.persistence.validation.factory", factory));
        }
    }});
    instanceFactory = synchronization == SynchronizationType.SYNCHRONIZED ? factory::createEntityManager : () -> factory.createEntityManager(synchronization);
}
 
Example 2
Source File: JpaPersistence.java    From javamelody with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public EntityManagerFactory createContainerEntityManagerFactory(final PersistenceUnitInfo info,
		final Map map) {
	initJpaCounter();
	final PersistenceProvider persistenceProvider = findDelegate(map);
	// on surcharge PersistenceUnitInfo.getPersistenceProviderClassName()
	// pour retourner le PersistenceProvider délégué et pas nous même
	final PersistenceUnitInfo proxiedInfo = createPersistentUnitInfoProxy(info,
			persistenceProvider);
	final Map overridenMap = new HashMap();
	if (map != null) {
		overridenMap.putAll(map);
	}
	// #869 No Persistence provider for EntityManager, with Hibernate 5.4 & JPA
	// (when JpaOverridePersistenceXmlClassLoader is not enough)
	overridenMap.put(JPA_PERSISTENCE_PROVIDER, persistenceProvider.getClass().getName());
	final EntityManagerFactory entityManagerFactory = persistenceProvider
			.createContainerEntityManagerFactory(proxiedInfo, overridenMap);
	if (entityManagerFactory == null) {
		return null;
	}
	return JpaWrapper.createEntityManagerFactoryProxy(entityManagerFactory);
}
 
Example 3
Source File: JPAPlugin.java    From minnal with Apache License 2.0 5 votes vote down vote up
public void init(Application<? extends ApplicationConfiguration> application) {
	List<PersistenceProvider> providers = getProviders();
	if (providers == null || providers.isEmpty()) {
		throw new MinnalException("No JPA persistence provider found");
	}
	PersistenceProvider provider = providers.get(0);
	PersistenceUnitInfo info = createPersistenceUnitInfo(application.getConfiguration(), provider); 
	factory = provider.createContainerEntityManagerFactory(info, null);
	JPA.instance.addPersistenceUnit(info.getPersistenceUnitName(), factory);
	application.addListener(new JerseyApplicationEventListener(application.getConfiguration().getDatabaseConfiguration()));
}
 
Example 4
Source File: SqlTestBaseTestNG.java    From quickperf with Apache License 2.0 4 votes vote down vote up
@BeforeTest
public void before() {
    PersistenceProvider persistenceProvider = new HibernatePersistenceProvider();
    PersistenceUnitInfo info = buildPersistenceUnitInfo();
    emf = persistenceProvider.createContainerEntityManagerFactory(info, new HashMap<>());
}
 
Example 5
Source File: SqlTestBaseJUnit5.java    From quickperf with Apache License 2.0 4 votes vote down vote up
@BeforeEach
public void before() {
    PersistenceProvider persistenceProvider = new HibernatePersistenceProvider();
    PersistenceUnitInfo info = buildPersistenceUnitInfo();
    emf = persistenceProvider.createContainerEntityManagerFactory(info, new HashMap<>());
}
 
Example 6
Source File: SqlTestBaseJUnit4.java    From quickperf with Apache License 2.0 4 votes vote down vote up
@Before
public void before() {
    PersistenceProvider persistenceProvider = new HibernatePersistenceProvider();
    PersistenceUnitInfo info = buildPersistenceUnitInfo();
    emf = persistenceProvider.createContainerEntityManagerFactory(info, new HashMap<>());
}
 
Example 7
Source File: SqlTestBase.java    From quickperf with Apache License 2.0 4 votes vote down vote up
@Before
public void before() {
    PersistenceProvider persistenceProvider = new HibernatePersistenceProvider();
    PersistenceUnitInfo info = buildPersistenceUnitInfo();
    emf = persistenceProvider.createContainerEntityManagerFactory(info, new HashMap<>());
}
 
Example 8
Source File: EntityManagerFactoryCallable.java    From tomee with Apache License 2.0 4 votes vote down vote up
@Override
public EntityManagerFactory call() throws Exception {
    final ClassLoader old = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(appClassLoader);
    try {
        final Class<?> clazz = appClassLoader.loadClass(persistenceProviderClassName);
        final PersistenceProvider persistenceProvider = (PersistenceProvider) clazz.newInstance();

        // Create entity manager factories with the validator factory
        final Map<String, Object> properties = new HashMap<>();
        if (!ValidationMode.NONE.equals(unitInfo.getValidationMode())) {
            properties.put("javax.persistence.validation.factory",
                    potentialValidators != null && potentialValidators.size() == 1 ? // optim to avoid lookups
                            ensureSerializable(potentialValidators.values().iterator().next()) :
                            new ValidatorFactoryWrapper(potentialValidators));
        }
        if (cdi && "true".equalsIgnoreCase(unitInfo.getProperties().getProperty("tomee.jpa.cdi", "true"))
                && "true".equalsIgnoreCase(SystemInstance.get().getProperty("tomee.jpa.cdi", "true"))) {
            properties.put("javax.persistence.bean.manager",
                    Proxy.newProxyInstance(appClassLoader, new Class<?>[]{BeanManager.class}, new BmHandler()));
        }

        customizeProperties(properties);

        // ensure no tx is there cause a managed connection would fail if the provider setAutocCommit(true) and some hib* have this good idea
        final Transaction transaction;
        if (unitInfo.isLazilyInitialized()) {
            transaction = OpenEJB.getTransactionManager().suspend();
        } else {
            transaction = null;
        }
        final EntityManagerFactory emf;
        try {
            emf = persistenceProvider.createContainerEntityManagerFactory(unitInfo, properties);
        } finally {
            if (unitInfo.isLazilyInitialized() && transaction != null) {
                OpenEJB.getTransactionManager().resume(transaction);
            }
        }

        if (unitInfo.getProperties() != null
                && "true".equalsIgnoreCase(unitInfo.getProperties().getProperty(OPENEJB_JPA_INIT_ENTITYMANAGER))
                || SystemInstance.get().getOptions().get(OPENEJB_JPA_INIT_ENTITYMANAGER, false)) {
            emf.createEntityManager().close();
        }

        if (unitInfo.getNonJtaDataSource() != null) {
            final ImportSql importer = new ImportSql(appClassLoader, unitInfo.getPersistenceUnitName(), unitInfo.getNonJtaDataSource());
            if (importer.hasSomethingToImport()) {
                emf.createEntityManager().close(); // to let OpenJPA create the database if configured this way
                importer.doImport();
            }
        }

        return emf;
    } finally {
        Thread.currentThread().setContextClassLoader(old);
    }
}