Example usage for javax.persistence EntityManagerFactory createEntityManager

List of usage examples for javax.persistence EntityManagerFactory createEntityManager

Introduction

In this page you can find the example usage for javax.persistence EntityManagerFactory createEntityManager.

Prototype

public EntityManager createEntityManager(SynchronizationType synchronizationType);

Source Link

Document

Create a new JTA application-managed EntityManager with the specified synchronization type.

Usage

From source file:org.springframework.orm.jpa.ExtendedEntityManagerCreator.java

/**
 * Create a container-managed extended EntityManager proxy.
 * @param emf the EntityManagerFactory to create the EntityManager with.
 * If this implements the EntityManagerFactoryInfo interface, the corresponding
 * JpaDialect and PersistenceUnitInfo will be detected accordingly.
 * @param properties the properties to be passed into the {@code createEntityManager}
 * call (may be {@code null})/*from   w  ww.j av a  2s . c  o m*/
 * @param synchronizedWithTransaction whether to automatically join ongoing
 * transactions (according to the JPA 2.1 SynchronizationType rules)
 * @return a container-managed EntityManager that expects container-driven lifecycle
 * management but may opt out of automatic transaction synchronization
 * @see javax.persistence.EntityManagerFactory#createEntityManager(java.util.Map)
 * @since 4.0
 */
public static EntityManager createContainerManagedEntityManager(EntityManagerFactory emf,
        @Nullable Map<?, ?> properties, boolean synchronizedWithTransaction) {

    Assert.notNull(emf, "EntityManagerFactory must not be null");
    if (emf instanceof EntityManagerFactoryInfo) {
        EntityManagerFactoryInfo emfInfo = (EntityManagerFactoryInfo) emf;
        EntityManagerFactory nativeEmf = emfInfo.getNativeEntityManagerFactory();
        EntityManager rawEntityManager = (!CollectionUtils.isEmpty(properties)
                ? nativeEmf.createEntityManager(properties)
                : nativeEmf.createEntityManager());
        return createProxy(rawEntityManager, emfInfo, true, synchronizedWithTransaction);
    } else {
        EntityManager rawEntityManager = (!CollectionUtils.isEmpty(properties)
                ? emf.createEntityManager(properties)
                : emf.createEntityManager());
        return createProxy(rawEntityManager, null, null, null, null, true, synchronizedWithTransaction);
    }
}

From source file:com.google.code.guice.repository.filter.PersistFilter.java

@Override
protected EntityManager createEntityManager(EntityManagerFactory emf) {
    PersistenceUnitConfiguration configuration = configurationManager
            .getConfiguration(getPersistenceUnitName());
    EntityManager entityManager = emf.createEntityManager(configuration.getProperties());
    configurationManager.changeEntityManager(getPersistenceUnitName(), entityManager);
    return entityManager;
}

From source file:org.springframework.orm.jpa.EntityManagerFactoryAccessor.java

/**
 * Obtain a new EntityManager from this accessor's EntityManagerFactory.
 * <p>Can be overridden in subclasses to create specific EntityManager variants.
 * @return a new EntityManager//w ww  .  j ava2s .c  o m
 * @throws IllegalStateException if this accessor is not configured with an EntityManagerFactory
 * @see javax.persistence.EntityManagerFactory#createEntityManager()
 * @see javax.persistence.EntityManagerFactory#createEntityManager(java.util.Map)
 */
protected EntityManager createEntityManager() throws IllegalStateException {
    EntityManagerFactory emf = obtainEntityManagerFactory();
    Map<String, Object> properties = getJpaPropertyMap();
    return (!CollectionUtils.isEmpty(properties) ? emf.createEntityManager(properties)
            : emf.createEntityManager());
}