Example usage for org.springframework.orm.jpa EntityManagerFactoryUtils findEntityManagerFactory

List of usage examples for org.springframework.orm.jpa EntityManagerFactoryUtils findEntityManagerFactory

Introduction

In this page you can find the example usage for org.springframework.orm.jpa EntityManagerFactoryUtils findEntityManagerFactory.

Prototype

public static EntityManagerFactory findEntityManagerFactory(ListableBeanFactory beanFactory,
        @Nullable String unitName) throws NoSuchBeanDefinitionException 

Source Link

Document

Find an EntityManagerFactory with the given name in the given Spring application context (represented as ListableBeanFactory).

Usage

From source file:org.cruxframework.mediamanager.model.spring.filter.CustomOpenEntityManagerInViewFilter.java

/**
 * {@inheritDoc}/*  w  w  w .  ja va 2 s. co  m*/
 */
@Override
protected EntityManagerFactory lookupEntityManagerFactory() {
    String emfBeanName = getEntityManagerFactoryBeanName();
    String puName = getPersistenceUnitName();
    if (StringUtils.hasLength(emfBeanName)) {
        return SpringUtils.get().getBean(emfBeanName, EntityManagerFactory.class);
    } else if (!StringUtils.hasLength(puName)
            && SpringUtils.get().containsBean(DEFAULT_ENTITY_MANAGER_FACTORY_BEAN_NAME)) {
        return SpringUtils.get().getBean(DEFAULT_ENTITY_MANAGER_FACTORY_BEAN_NAME, EntityManagerFactory.class);
    } else {
        // Includes fallback search for single EntityManagerFactory bean by type.
        return EntityManagerFactoryUtils.findEntityManagerFactory(SpringUtils.get().getContext(), puName);
    }
}

From source file:org.jasig.jpa.OpenEntityManagerAspect.java

/**
 * Look up the EntityManagerFactory that this filter should use.
 * <p>The default implementation looks for a bean with the specified name
 * in Spring's root application context.
 * @return the EntityManagerFactory to use
 * @see #getEntityManagerFactoryBeanName
 *///from ww  w  . j a  v  a2  s . c o  m
protected EntityManagerFactory lookupEntityManagerFactory(OpenEntityManager openEntityManager) {
    String emfBeanName = openEntityManager.name();
    String puName = openEntityManager.unitName();
    if (StringUtils.hasLength(emfBeanName)) {
        return this.applicationContext.getBean(emfBeanName, EntityManagerFactory.class);
    } else if (!StringUtils.hasLength(puName) && this.applicationContext
            .containsBean(OpenEntityManagerInViewFilter.DEFAULT_ENTITY_MANAGER_FACTORY_BEAN_NAME)) {
        return this.applicationContext.getBean(
                OpenEntityManagerInViewFilter.DEFAULT_ENTITY_MANAGER_FACTORY_BEAN_NAME,
                EntityManagerFactory.class);
    } else {
        // Includes fallback search for single EntityManagerFactory bean by type.
        return EntityManagerFactoryUtils.findEntityManagerFactory(this.applicationContext, puName);
    }
}

From source file:org.lightadmin.core.config.bootstrap.LightAdminBeanDefinitionRegistryPostProcessor.java

private EntityManager findEntityManager(WebApplicationContext rootContext) {
    EntityManagerFactory entityManagerFactory = EntityManagerFactoryUtils.findEntityManagerFactory(rootContext,
            null);//w  ww. jav a  2  s. c o m

    return SharedEntityManagerCreator.createSharedEntityManager(entityManagerFactory);
}

From source file:org.apache.syncope.core.persistence.jpa.content.XMLContentLoader.java

@Override
public void load() {
    for (Map.Entry<String, DataSource> entry : domainsHolder.getDomains().entrySet()) {
        // create EntityManager so OpenJPA will build the SQL schema
        EntityManagerFactoryUtils
                .findEntityManagerFactory(ApplicationContextProvider.getBeanFactory(), entry.getKey())
                .createEntityManager();//  w  ww  . jav a2  s . c o m

        JdbcTemplate jdbcTemplate = new JdbcTemplate(entry.getValue());
        boolean existingData;
        try {
            existingData = jdbcTemplate.queryForObject("SELECT COUNT(0) FROM " + JPAConf.TABLE,
                    Integer.class) > 0;
        } catch (DataAccessException e) {
            LOG.error("[{}] Could not access to table " + JPAConf.TABLE, entry.getKey(), e);
            existingData = true;
        }

        if (existingData) {
            LOG.info("[{}] Data found in the database, leaving untouched", entry.getKey());
        } else {
            LOG.info("[{}] Empty database found, loading default content", entry.getKey());

            try {
                ResourceWithFallbackLoader contentXML = ApplicationContextProvider.getBeanFactory()
                        .getBean(entry.getKey() + "ContentXML", ResourceWithFallbackLoader.class);
                loadDefaultContent(entry.getKey(), contentXML, entry.getValue());
            } catch (Exception e) {
                LOG.error("[{}] While loading default content", entry.getKey(), e);
            }
            try {
                createIndexes(entry.getKey(), entry.getValue());
                createViews(entry.getKey(), entry.getValue());
            } catch (IOException e) {
                LOG.error("[{}] While creating indexes and views", entry.getKey(), e);
            }
        }
    }
}

From source file:org.kuali.student.common.util.jpa.LoadSqlListener.java

private void process(String entityKey, String sqlFileName) {
    EntityManagerFactory emf = EntityManagerFactoryUtils.findEntityManagerFactory(applicationContext,
            entityKey);/*from  w  ww  . ja  va 2s .  c  o m*/
    EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);

    File sqlFile;
    BufferedReader in;
    try {
        if (sqlFileName.startsWith("classpath:")) {
            sqlFile = new ClassPathResource(sqlFileName.substring("classpath:".length())).getFile();
        } else {
            sqlFile = new File(sqlFileName);
        }
        in = new BufferedReader(new FileReader(sqlFile));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    String ln = "";

    TransactionDefinition txDefinition = new DefaultTransactionDefinition();
    TransactionStatus txStatus = jtaTxManager.getTransaction(txDefinition);

    try {
        while ((ln = in.readLine()) != null) {
            if (!ln.startsWith("/") && !ln.startsWith("--") && StringUtils.isNotBlank(ln)) {
                ln = ln.replaceFirst("[;/]\\s*$", "");
                em.createNativeQuery(ln).executeUpdate();
            }
        }
        jtaTxManager.commit(txStatus);
    } catch (Exception e) {
        logger.error("Error loading sql file " + sqlFileName + ". Failing statement was '" + ln + "'", e);
        jtaTxManager.rollback(txStatus);
    } finally {
        try {
            in.close();
        } catch (IOException e) {
            logger.error("IO Stream closed " + e);
        }
    }
}

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

/**
 * Retrieves an EntityManagerFactory by persistence unit name, if none set explicitly.
 * Falls back to a default EntityManagerFactory bean if no persistence unit specified.
 * @see #setPersistenceUnitName//from  w  w  w. java 2s . c o  m
 */
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
    if (getEntityManagerFactory() == null) {
        if (!(beanFactory instanceof ListableBeanFactory)) {
            throw new IllegalStateException("Cannot retrieve EntityManagerFactory by persistence unit name "
                    + "in a non-listable BeanFactory: " + beanFactory);
        }
        ListableBeanFactory lbf = (ListableBeanFactory) beanFactory;
        setEntityManagerFactory(
                EntityManagerFactoryUtils.findEntityManagerFactory(lbf, getPersistenceUnitName()));
    }
}