Java源码示例:javax.persistence.spi.PersistenceProviderResolverHolder
示例1
/**
* 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;
}
示例2
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;
}
示例3
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"));
}
}
}
示例4
@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");
}
示例5
@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");
}
示例6
/**
* 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");
}
}
示例7
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");
}
示例8
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";
}
}
}
示例9
@Produces
@ApplicationScoped
public PersistenceProvider getPersistenceProvider() {
return PersistenceProviderResolverHolder.getPersistenceProviderResolver().getPersistenceProviders().get(0);
}
示例10
protected List<PersistenceProvider> getProviders() {
return PersistenceProviderResolverHolder.getPersistenceProviderResolver().getPersistenceProviders();
}
示例11
/**
* 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);
}