Example usage for javax.persistence.spi PersistenceUnitTransactionType RESOURCE_LOCAL

List of usage examples for javax.persistence.spi PersistenceUnitTransactionType RESOURCE_LOCAL

Introduction

In this page you can find the example usage for javax.persistence.spi PersistenceUnitTransactionType RESOURCE_LOCAL.

Prototype

PersistenceUnitTransactionType RESOURCE_LOCAL

To view the source code for javax.persistence.spi PersistenceUnitTransactionType RESOURCE_LOCAL.

Click Source Link

Document

Resource-local entity managers will be created.

Usage

From source file:org.wisdom.framework.jpa.PersistenceUnitComponent.java

/**
 * Starts the unit. It creates the entity manager factory and entity manager, as well as the repository and crud
 * services.//from   w  w  w  . j  a v  a  2  s.c  o  m
 */
@Validate
public void start() {
    try {
        Map<String, Object> map = new HashMap<>();
        for (Persistence.PersistenceUnit.Properties.Property p : persistenceUnitXml.getProperties()
                .getProperty()) {
            map.put(p.getName(), p.getValue());
        }

        if (isOpenJPA()) {
            map.put("openjpa.ManagedRuntime",
                    "invocation(TransactionManagerMethod=org.wisdom.framework.jpa.accessor"
                            + ".TransactionManagerAccessor.get)");
        }

        // This is not going to work with OpenJPA because the current version of OpenJPA requires an old version
        // of javax.validation. The wisdom one is too recent.
        map.put("javax.persistence.validation.factory", validator);

        Dictionary<String, Object> properties = new Hashtable<>();
        properties.put(UNIT_NAME_PROP, persistenceUnitXml.getName());
        properties.put(UNIT_VERSION_PROP, sourceBundle.bundle.getVersion());
        properties.put(UNIT_PROVIDER_PROP, provider.getClass().getName());
        List<String> entities = persistenceUnitXml.getClazz();
        properties.put(UNIT_ENTITIES_PROP, entities.toArray(new String[entities.size()]));
        properties.put(UNIT_TRANSACTION_PROP, getTransactionType().toString());

        // If the unit set the transaction to RESOURCE_LOCAL, no JTA involved.
        if (persistenceUnitXml
                .getTransactionType() == org.wisdom.framework.jpa.model.PersistenceUnitTransactionType.RESOURCE_LOCAL) {
            entityManagerFactory = provider.createContainerEntityManagerFactory(this, map);
            emfRegistration = bundleContext.registerService(EntityManagerFactory.class, entityManagerFactory,
                    properties);

            entityManager = entityManagerFactory.createEntityManager();
            emRegistration = bundleContext.registerService(EntityManager.class, entityManager, properties);

            repository = new JPARepository(persistenceUnitXml, entityManager, entityManagerFactory,
                    transactionManager, sourceBundle.bundle.getBundleContext());
        } else {
            // JTA
            entityManagerFactory = provider.createContainerEntityManagerFactory(this, map);
            entityManager = new TransactionalEntityManager(transactionManager, entityManagerFactory, this);
            emRegistration = bundleContext.registerService(EntityManager.class, entityManager, properties);
            emfRegistration = bundleContext.registerService(EntityManagerFactory.class, entityManagerFactory,
                    properties);
            repository = new JPARepository(persistenceUnitXml, entityManager, entityManagerFactory,
                    transactionManager, sourceBundle.bundle.getBundleContext());
        }
    } catch (Exception e) {
        LOGGER.error("Error while initializing the JPA services for unit {}", persistenceUnitXml.getName(), e);
    }

}

From source file:org.wisdom.framework.jpa.PersistenceUnitComponent.java

@Override
public PersistenceUnitTransactionType getTransactionType() {
    if (persistenceUnitXml
            .getTransactionType() == org.wisdom.framework.jpa.model.PersistenceUnitTransactionType.RESOURCE_LOCAL) {
        return PersistenceUnitTransactionType.RESOURCE_LOCAL;
    } else {/*from w ww  .  jav  a 2s . c  om*/
        return PersistenceUnitTransactionType.JTA;
    }
}