Example usage for javax.persistence.spi PersistenceProvider getClass

List of usage examples for javax.persistence.spi PersistenceProvider getClass

Introduction

In this page you can find the example usage for javax.persistence.spi PersistenceProvider getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:org.kie.server.services.jbpm.JbpmKieServerExtension.java

protected EntityManagerFactory build(Map<String, String> properties) {
    try {/*  w  ww.  j  a  v  a 2  s . c o m*/
        InitialContext ctx = new InitialContext();
        InputStream inputStream = PersistenceUnitInfoLoader.class.getResourceAsStream(PERSISTENCE_XML_LOCATION);
        PersistenceUnitInfo info = PersistenceUnitInfoLoader.load(inputStream, ctx,
                this.getClass().getClassLoader());
        // prepare persistence unit root location
        URL root = PersistenceUnitInfoLoader.class.getResource(PERSISTENCE_XML_LOCATION);
        String jarLocation = root.toExternalForm().split("!")[0].replace(PERSISTENCE_XML_LOCATION, "");
        try {
            ((PersistenceUnitInfoImpl) info).setPersistenceUnitRootUrl(new URL(jarLocation));
        } catch (Exception e) {
            // in case setting URL to jar file location only fails, fallback to complete URL
            ((PersistenceUnitInfoImpl) info).setPersistenceUnitRootUrl(root);
        }
        // Need to explicitly set jtaDataSource here, its value is fetched in Hibernate logger before configuration
        ((PersistenceUnitInfoImpl) info).setJtaDataSource(properties.get("javax.persistence.jtaDataSource"));
        List<PersistenceProvider> persistenceProviders = PersistenceProviderResolverHolder
                .getPersistenceProviderResolver().getPersistenceProviders();
        PersistenceProvider selectedProvider = null;
        if (persistenceProviders != null) {
            for (PersistenceProvider provider : persistenceProviders) {
                if (provider.getClass().getName().equals(info.getPersistenceProviderClassName())) {
                    selectedProvider = provider;
                    break;
                }
            }
        }

        return selectedProvider.createContainerEntityManagerFactory(info, properties);
    } catch (Exception e) {
        throw new RuntimeException("Unable to create EntityManagerFactory due to " + e.getMessage(), e);
    }
}

From source file:org.seedstack.jpa.internal.EntityManagerFactoryFactory.java

private EntityManagerFactory createEntityManagerFactory(InternalPersistenceUnitInfo info,
        Properties properties) {/*from  w  ww  . j a  v  a2  s  .  co m*/
    HashMap<String, String> propertiesMap = new HashMap<String, String>();
    if (properties != null) {
        for (Object key : properties.keySet()) {
            propertiesMap.put((String) key, properties.getProperty((String) key));
        }
    }

    EntityManagerFactory fac = null;
    List<PersistenceProvider> persistenceProviders = PersistenceProviderResolverHolder
            .getPersistenceProviderResolver().getPersistenceProviders();

    for (PersistenceProvider persistenceProvider : persistenceProviders) {
        info.setPersistenceProviderClassName(persistenceProvider.getClass().getName());
        fac = persistenceProvider.createContainerEntityManagerFactory(info, propertiesMap);
        if (fac != null) {
            break;
        }
    }

    if (fac == null) {
        throw new PersistenceException(
                "No Persistence provider for EntityManager named " + info.getPersistenceUnitName());
    }

    return fac;
}