javax.persistence.spi.PersistenceProviderResolverHolder Java Examples

The following examples show how to use javax.persistence.spi.PersistenceProviderResolverHolder. 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: Persistence.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create and return an EntityManagerFactory for the named persistence unit
 * using the given properties.
 * 
 * @param persistenceUnitName
 *            the name of the persistence unit
 * @param properties
 *            Additional properties to use when creating the factory. 
 *            These properties may include properties to control
 *            schema generation.  The values of these properties override 
 *            any values that may have been configured elsewhere.
 * @return the factory that creates EntityManagers configured according to
 *         the specified persistence unit.
 */
public static EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map properties) {

    EntityManagerFactory emf = null;
    PersistenceProviderResolver resolver = PersistenceProviderResolverHolder.getPersistenceProviderResolver();

    List<PersistenceProvider> providers = resolver.getPersistenceProviders();

    for (PersistenceProvider provider : providers) {
        emf = provider.createEntityManagerFactory(persistenceUnitName, properties);
        if (emf != null) {
            break;
        }
    }
    if (emf == null) {
        throw new PersistenceException("No Persistence provider for EntityManager named " + persistenceUnitName);
    }
    return emf;
}
 
Example #2
Source File: Persistence.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public boolean isLoaded(Object entity) {
    PersistenceProviderResolver resolver = PersistenceProviderResolverHolder.getPersistenceProviderResolver();

    List<PersistenceProvider> providers = resolver.getPersistenceProviders();

    for (PersistenceProvider provider : providers) {
        LoadState loadstate = provider.getProviderUtil().isLoaded(entity);
        if(loadstate == LoadState.LOADED) {
            return true;
        } else if (loadstate == LoadState.NOT_LOADED) {
            return false;
        } // else continue
    }
    //None of the providers could determine the load state
    return true;
}
 
Example #3
Source File: JpaPersistence.java    From javamelody with Apache License 2.0 6 votes vote down vote up
private void guessDelegate() {
	// https://issues.apache.org/jira/browse/SIRONA-44
	// https://github.com/javamelody/javamelody/issues/460
	final List<PersistenceProvider> persistenceProviders = PersistenceProviderResolverHolder
			.getPersistenceProviderResolver().getPersistenceProviders();
	for (final PersistenceProvider persistenceProvider : persistenceProviders) {
		if (!getClass().isInstance(persistenceProvider)) {
			delegate = persistenceProvider;
			break;
		}
	}
	if (delegate == null) {
		for (final String provider : PROVIDERS) {
			try {
				delegate = newPersistence(provider);
				break;
			} catch (final Throwable th2) { // NOPMD
				continue;
			}
		}
		if (delegate == null) {
			throw new IllegalStateException(
					new ClassNotFoundException("Can't find a delegate"));
		}
	}
}
 
Example #4
Source File: PersistenceConfigurationProviderTest.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
@Deployment
public static WebArchive deploy()
{
    // set the dummy PersistenceProviderResolver which creates our DummyEntityManagerFactory
    PersistenceProviderResolverHolder.setPersistenceProviderResolver(new TestPersistenceProviderResolver());

    JavaArchive testJar = ShrinkWrap.create(JavaArchive.class, "unitDefinitionTest.jar")
            .addPackage(ArchiveUtils.SHARED_PACKAGE)
            .addClass(PersistenceConfigurationProviderTest.class)
            .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");

    return ShrinkWrap.create(WebArchive.class)
            .addAsLibraries(ArchiveUtils.getDeltaSpikeCoreAndJpaArchive())
            .addAsLibraries(testJar)
            .addAsWebInfResource(ArchiveUtils.getBeansXml(), "beans.xml");
}
 
Example #5
Source File: EntityManagerFactoryProducerTest.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
@Deployment
public static WebArchive deploy()
{
    // set the dummy PersistenceProviderResolver which creates our DummyEntityManagerFactory
    PersistenceProviderResolverHolder.setPersistenceProviderResolver(new TestPersistenceProviderResolver());

    JavaArchive testJar = ShrinkWrap.create(JavaArchive.class, "unitDefinitionTest.jar")
            .addPackage(ArchiveUtils.SHARED_PACKAGE)
            .addPackage(EntityManagerFactoryProducerTest.class.getPackage().getName())
            .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
            .addAsResource(new StringAsset(TestPersistenceProviderResolver.class.getName()),
                    "META-INF/services/javax.persistence.spi.PersistenceProviderResolver");

    return ShrinkWrap.create(WebArchive.class)
            .addAsLibraries(ArchiveUtils.getDeltaSpikeCoreAndJpaArchive())
            .addAsLibraries(testJar)
            .addAsWebInfResource(ArchiveUtils.getBeansXml(), "beans.xml");
}
 
Example #6
Source File: JpaPersistence.java    From javamelody with Apache License 2.0 5 votes vote down vote up
/**
 * Active le monitoring JPA par défaut,
 * même si <provider>net.bull.javamelody.JpaPersistence</provider> n'est pas dans META-INF/persistence.xml
 */
public static void initPersistenceProviderResolver() {
	try {
		PersistenceProviderResolver resolver = PersistenceProviderResolverHolder
				.getPersistenceProviderResolver();
		if (!(resolver instanceof JavaMelodyPersistenceProviderResolver)) {
			resolver = new JavaMelodyPersistenceProviderResolver(resolver);
			PersistenceProviderResolverHolder.setPersistenceProviderResolver(resolver);
			LOG.debug("JPA persistence provider resolver initialized");
		}
	} catch (final Throwable t) { // NOPMD
		LOG.info("initialization of jpa persistence provider resolver failed, skipping");
	}
}
 
Example #7
Source File: TestJpa.java    From javamelody with Apache License 2.0 5 votes vote down vote up
private PersistenceProvider getJpaPersistence() {
	final List<PersistenceProvider> persistenceProviders = PersistenceProviderResolverHolder
			.getPersistenceProviderResolver().getPersistenceProviders();
	for (final PersistenceProvider persistenceProvider : persistenceProviders) {
		if (persistenceProvider instanceof JpaPersistence) {
			return persistenceProvider;
		}
	}
	throw new IllegalStateException("JpaPersistence not found in PersistenceProviders");
}
 
Example #8
Source File: JPA.java    From activejpa with Apache License 2.0 5 votes vote down vote up
private JPA() {
	List<PersistenceProvider> providers = PersistenceProviderResolverHolder.getPersistenceProviderResolver().getPersistenceProviders();
	if (providers != null) {
		String providerClass = providers.get(0).getClass().getCanonicalName();
		if (providerClass.equals(HIBERNATE_PERSISTENCE) || providerClass.equals(HIBERNATE_JPA_PERSISTENCE)) {
			cacheableHint = "org.hibernate.cacheable";
		} else if (providerClass.equals(ECLIPSE_PERSISTENCE)) {
			cacheableHint = "eclipselink.query-results-cache";
		} else if (providerClass.equals(OPENJPA_PERSISTENCE)) {
			cacheableHint = "openjpa.QueryCache";
		}
	}
}
 
Example #9
Source File: EntityManagerFactoryProvider.java    From hammock with Apache License 2.0 4 votes vote down vote up
@Produces
@ApplicationScoped
public PersistenceProvider getPersistenceProvider() {
    return PersistenceProviderResolverHolder.getPersistenceProviderResolver().getPersistenceProviders().get(0);
}
 
Example #10
Source File: JPAPlugin.java    From minnal with Apache License 2.0 4 votes vote down vote up
protected List<PersistenceProvider> getProviders() {
	return PersistenceProviderResolverHolder.getPersistenceProviderResolver().getPersistenceProviders();
}
 
Example #11
Source File: Persistence.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Create database schemas and/or tables and/or create DDL
 * scripts as determined by the supplied properties.
 * <p>
 * Called when schema generation is to occur as a separate phase
 * from creation of the entity manager factory.
 * <p>
 * @param persistenceUnitName the name of the persistence unit
 * @param map properties for schema generation;  these may
 *             also contain provider-specific properties.  The
 *             value of these properties override any values that
 *             may have been configured elsewhere..             
 * @throws PersistenceException if insufficient or inconsistent
 *         configuration information is provided or if schema
 *         generation otherwise fails.
 *
 * @since Java Persistence 2.1
 */
public static void generateSchema(String persistenceUnitName, Map map) {
    PersistenceProviderResolver resolver = PersistenceProviderResolverHolder.getPersistenceProviderResolver();
    List<PersistenceProvider> providers = resolver.getPersistenceProviders();
    
    for (PersistenceProvider provider : providers) {
        if (provider.generateSchema(persistenceUnitName, map)) {
            return;
        }
    }
    
    throw new PersistenceException("No Persistence provider to generate schema named " + persistenceUnitName);
}