Example usage for org.springframework.data.jpa.repository.support JpaRepositoryFactoryBean setEntityManager

List of usage examples for org.springframework.data.jpa.repository.support JpaRepositoryFactoryBean setEntityManager

Introduction

In this page you can find the example usage for org.springframework.data.jpa.repository.support JpaRepositoryFactoryBean setEntityManager.

Prototype

@PersistenceContext
public void setEntityManager(EntityManager entityManager) 

Source Link

Document

The EntityManager to be used.

Usage

From source file:com.google.code.guice.repository.spi.JpaRepositoryProvider.java

@Override
public R get() {/* w  ww  .  j  a  va 2  s . co  m*/
    // double-checked locking with volatile
    R repo = repository;
    if (repo == null) {
        synchronized (this) {
            repo = repository;
            if (repo == null) {
                JpaRepositoryFactoryBean jpaRepositoryFactoryBean = createJpaRepositoryFactoryBean();
                PersistenceUnitConfiguration configuration = configurationManager
                        .getConfiguration(persistenceUnitName);

                // Repository will hold a link to Configuration and if underlying EM will be changed - repo instance will get it
                EntityManager entityManager = configuration.asEntityManagerProxy();
                // Needs to be set first
                jpaRepositoryFactoryBean.setTransactionManager(configuration.getTransactionManagerName());
                // Attaching to Spring's context
                jpaRepositoryFactoryBean.setBeanFactory(context);
                jpaRepositoryFactoryBean.setEntityManager(entityManager);
                jpaRepositoryFactoryBean.setRepositoryInterface(repositoryClass);
                jpaRepositoryFactoryBean.setNamedQueries(binding.getNamedQueries());
                jpaRepositoryFactoryBean.setQueryLookupStrategyKey(binding.getQueryLookupStrategyKey());

                if (customImplementationClass != null) {
                    Object customRepositoryImplementation = instantiateCustomRepository(entityManager);

                    if (customRepositoryImplementation != null) {
                        jpaRepositoryFactoryBean.setCustomImplementation(customRepositoryImplementation);
                    }
                }

                jpaRepositoryFactoryBean.afterPropertiesSet();
                repository = (R) jpaRepositoryFactoryBean.getObject();

                repo = repository;
            }
        }
    }
    return repo;
}