Java 类org.springframework.orm.jpa.EntityManagerFactoryUtils 实例源码

项目:lams    文件:OpenEntityManagerInViewFilter.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
 */
protected EntityManagerFactory lookupEntityManagerFactory() {
    WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
    String emfBeanName = getEntityManagerFactoryBeanName();
    String puName = getPersistenceUnitName();
    if (StringUtils.hasLength(emfBeanName)) {
        return wac.getBean(emfBeanName, EntityManagerFactory.class);
    }
    else if (!StringUtils.hasLength(puName) && wac.containsBean(DEFAULT_ENTITY_MANAGER_FACTORY_BEAN_NAME)) {
        return wac.getBean(DEFAULT_ENTITY_MANAGER_FACTORY_BEAN_NAME, EntityManagerFactory.class);
    }
    else {
        // Includes fallback search for single EntityManagerFactory bean by type.
        return EntityManagerFactoryUtils.findEntityManagerFactory(wac, puName);
    }
}
项目:spring4-understanding    文件:OpenEntityManagerInViewFilter.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
 */
protected EntityManagerFactory lookupEntityManagerFactory() {
    WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
    String emfBeanName = getEntityManagerFactoryBeanName();
    String puName = getPersistenceUnitName();
    if (StringUtils.hasLength(emfBeanName)) {
        return wac.getBean(emfBeanName, EntityManagerFactory.class);
    }
    else if (!StringUtils.hasLength(puName) && wac.containsBean(DEFAULT_ENTITY_MANAGER_FACTORY_BEAN_NAME)) {
        return wac.getBean(DEFAULT_ENTITY_MANAGER_FACTORY_BEAN_NAME, EntityManagerFactory.class);
    }
    else {
        // Includes fallback search for single EntityManagerFactory bean by type.
        return EntityManagerFactoryUtils.findEntityManagerFactory(wac, puName);
    }
}
项目:dachs    文件:DefaultInternalEntityListener.java   
@Override
public void beforeCommit()
{
    if (flush)
    {
        EntityManagerFactoryUtils.getTransactionalEntityManager(emf).flush();
    }

    final MutableEntityDataChangeSet cs = preChangeset.get();

    if (! cs.isEmpty())
    {
        lazySetId(cs);

        for (EntityChangeSetListener listener : entityChangeSetListeners)
        {
            listener.preDataChanged(MutableEntityDataChangeSet.clone(cs));
        }
    }

    preChangeset.remove();
}
项目:dachs    文件:EclipselinkLazyIdExtractor.java   
@Override
public String[] extractIdPropertyNames(Object entity)
{
    final EntityManager em = EntityManagerFactoryUtils.getTransactionalEntityManager(emf);
    final ClassDescriptor desc = em.unwrap(JpaEntityManager.class).getServerSession().getClassDescriptor(entity);
    if (desc != null)
    {
        final Collection<DatabaseMapping> fieldNames = desc.getMappings();
        final List<DatabaseMapping> tmp = new LinkedList<>();
        for (DatabaseMapping m : fieldNames)
        {
            if (m.isPrimaryKeyMapping())
            {
                tmp.add(m);
            }
        }
        final String[] retVal = new String[tmp.size()];
        for (int i = 0; i < retVal.length; i++)
        {
            retVal[i] = tmp.get(i).getAttributeName();
        }
        return retVal;
    }

    return null;
}
项目:ef-orm    文件:BaseDao.java   
/**
 * 获得EntityManager
 * 
 * @return
 */
protected final EntityManager getEntityManager() {
    TransactionMode tx = jefEmf.getDefault().getTxType();
    EntityManager em;
    switch (tx) {
    case JPA:
    case JTA:
        em = EntityManagerFactoryUtils.doGetTransactionalEntityManager(entityManagerFactory, null);
        if (em == null) { // 当无事务时。Spring返回null
            em = entityManagerFactory.createEntityManager(null, Collections.EMPTY_MAP);
        }
        break;
    case JDBC:
        ConnectionHolder conn = (ConnectionHolder) TransactionSynchronizationManager.getResource(jefEmf.getDefault().getDataSource());
        if (conn == null) {// 基于数据源的Spring事务
            em = entityManagerFactory.createEntityManager(null, Collections.EMPTY_MAP);
        } else {
            ManagedTransactionImpl session = new ManagedTransactionImpl(jefEmf.getDefault(), conn.getConnection());
            em = new JefEntityManager(entityManagerFactory, null, session);
        }
        break;
    default:
        throw new UnsupportedOperationException(tx.name());
    }
    return em;
}
项目:ef-orm    文件:QueryUtils.java   
/**
 * 获得EntityManager
 * 
 * @return
 */
public static final EntityManager getEntityManager(JefEntityManagerFactory jefEmf) {
    TransactionMode tx = jefEmf.getDefault().getTxType();
    EntityManager em;
    switch (tx) {
    case JPA:
    case JTA:
        em = EntityManagerFactoryUtils.doGetTransactionalEntityManager(jefEmf, null);
        if (em == null) { // 当无事务时。Spring返回null
            em = jefEmf.createEntityManager(null, Collections.EMPTY_MAP);
        }
        break;
    case JDBC:
        ConnectionHolder conn = (ConnectionHolder) TransactionSynchronizationManager.getResource(jefEmf.getDefault().getDataSource());
        if (conn == null) {// 基于数据源的Spring事务
            em = jefEmf.createEntityManager(null, Collections.EMPTY_MAP);
        } else {
            ManagedTransactionImpl session = new ManagedTransactionImpl(jefEmf.getDefault(), conn.getConnection());
            em = new JefEntityManager(jefEmf, null, session);
        }
        break;
    default:
        throw new UnsupportedOperationException(tx.name());
    }
    return em;
}
项目:class-guard    文件:OpenEntityManagerInViewFilter.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
 */
protected EntityManagerFactory lookupEntityManagerFactory() {
    WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
    String emfBeanName = getEntityManagerFactoryBeanName();
    String puName = getPersistenceUnitName();
    if (StringUtils.hasLength(emfBeanName)) {
        return wac.getBean(emfBeanName, EntityManagerFactory.class);
    }
    else if (!StringUtils.hasLength(puName) && wac.containsBean(DEFAULT_ENTITY_MANAGER_FACTORY_BEAN_NAME)) {
        return wac.getBean(DEFAULT_ENTITY_MANAGER_FACTORY_BEAN_NAME, EntityManagerFactory.class);
    }
    else {
        // Includes fallback search for single EntityManagerFactory bean by type.
        return EntityManagerFactoryUtils.findEntityManagerFactory(wac, puName);
    }
}
项目:spring-data-snowdrop    文件:JpaDatasourceMapper.java   
private EntityManager getEntityManager() {
    EntityManager em = EntityManagerFactoryUtils.getTransactionalEntityManager(emf);
    if (em == null) {
        return emf.createEntityManager();
    }
    return em;
}
项目:spring-data-snowdrop    文件:JpaDatasourceMapper.java   
protected void applyQueryImpl(Query query) {
    EntityManager em = EntityManagerFactoryUtils.getTransactionalEntityManager(emf);
    if (em == null) {
        entityManager = emf.createEntityManager();
        em = entityManager;
    }
    FullTextSession fullTextSession = Search.getFullTextSession(em.unwrap(Session.class));
    fullTextQuery = fullTextSession.createFullTextQuery(query, entityClass);
}
项目:lams    文件:OpenEntityManagerInViewInterceptor.java   
@Override
public void afterCompletion(WebRequest request, Exception ex) throws DataAccessException {
    if (!decrementParticipateCount(request)) {
        EntityManagerHolder emHolder = (EntityManagerHolder)
                TransactionSynchronizationManager.unbindResource(getEntityManagerFactory());
        logger.debug("Closing JPA EntityManager in OpenEntityManagerInViewInterceptor");
        EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager());
    }
}
项目:lams    文件:PersistenceAnnotationBeanPostProcessor.java   
/**
 * Find an EntityManagerFactory with the given name in the current
 * Spring application context.
 * @param unitName the name of the persistence unit (never empty)
 * @param requestingBeanName the name of the requesting bean
 * @return the EntityManagerFactory
 * @throws NoSuchBeanDefinitionException if there is no such EntityManagerFactory in the context
 */
protected EntityManagerFactory findNamedEntityManagerFactory(String unitName, String requestingBeanName)
        throws NoSuchBeanDefinitionException {

    EntityManagerFactory emf = EntityManagerFactoryUtils.findEntityManagerFactory(this.beanFactory, unitName);
    if (this.beanFactory instanceof ConfigurableBeanFactory) {
        ((ConfigurableBeanFactory) this.beanFactory).registerDependentBean(unitName, requestingBeanName);
    }
    return emf;
}
项目:lams    文件:HibernateJpaDialect.java   
@Override
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
    if (ex instanceof HibernateException) {
        return convertHibernateAccessException((HibernateException) ex);
    }
    if (ex instanceof PersistenceException && ex.getCause() instanceof HibernateException) {
        return convertHibernateAccessException((HibernateException) ex.getCause());
    }
    return EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(ex);
}
项目:apollo-custom    文件:EntityManagerUtil.java   
/**
 * close the entity manager.
 * Use it with caution! This is only intended for use with async request, which Spring won't
 * close the entity manager until the async request is finished.
 */
public void closeEntityManager() {
  EntityManagerHolder emHolder = (EntityManagerHolder)
      TransactionSynchronizationManager.getResource(getEntityManagerFactory());
  if (emHolder == null) {
    return;
  }
  logger.debug("Closing JPA EntityManager in EntityManagerUtil");
  EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager());
}
项目:apollo    文件:EntityManagerUtil.java   
/**
 * close the entity manager.
 * Use it with caution! This is only intended for use with async request, which Spring won't
 * close the entity manager until the async request is finished.
 */
public void closeEntityManager() {
  EntityManagerHolder emHolder = (EntityManagerHolder)
      TransactionSynchronizationManager.getResource(getEntityManagerFactory());
  if (emHolder == null) {
    return;
  }
  logger.debug("Closing JPA EntityManager in EntityManagerUtil");
  EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager());
}
项目:spring4-understanding    文件:OpenEntityManagerInViewInterceptor.java   
@Override
public void afterCompletion(WebRequest request, Exception ex) throws DataAccessException {
    if (!decrementParticipateCount(request)) {
        EntityManagerHolder emHolder = (EntityManagerHolder)
                TransactionSynchronizationManager.unbindResource(getEntityManagerFactory());
        logger.debug("Closing JPA EntityManager in OpenEntityManagerInViewInterceptor");
        EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager());
    }
}
项目:spring4-understanding    文件:PersistenceAnnotationBeanPostProcessor.java   
/**
 * Find an EntityManagerFactory with the given name in the current
 * Spring application context.
 * @param unitName the name of the persistence unit (never empty)
 * @param requestingBeanName the name of the requesting bean
 * @return the EntityManagerFactory
 * @throws NoSuchBeanDefinitionException if there is no such EntityManagerFactory in the context
 */
protected EntityManagerFactory findNamedEntityManagerFactory(String unitName, String requestingBeanName)
        throws NoSuchBeanDefinitionException {

    EntityManagerFactory emf = EntityManagerFactoryUtils.findEntityManagerFactory(this.beanFactory, unitName);
    if (this.beanFactory instanceof ConfigurableBeanFactory) {
        ((ConfigurableBeanFactory) this.beanFactory).registerDependentBean(unitName, requestingBeanName);
    }
    return emf;
}
项目:spring4-understanding    文件:HibernateJpaDialect.java   
@Override
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
    if (ex instanceof HibernateException) {
        return convertHibernateAccessException((HibernateException) ex);
    }
    if (ex instanceof PersistenceException && ex.getCause() instanceof HibernateException) {
        return convertHibernateAccessException((HibernateException) ex.getCause());
    }
    return EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(ex);
}
项目:https-github.com-g0t4-jenkins2-course-spring-boot    文件:TestEntityManager.java   
/**
 * Return the underlying {@link EntityManager} that's actually used to perform all
 * operations.
 * @return the entity manager
 */
public final EntityManager getEntityManager() {
    EntityManager manager = EntityManagerFactoryUtils
            .getTransactionalEntityManager(this.entityManagerFactory);
    Assert.state(manager != null, "No transactional EntityManager found");
    return manager;
}
项目:flowable-engine    文件:SpringEntityManagerSessionFactory.java   
@Override
public Session openSession() {
    EntityManager entityManager = EntityManagerFactoryUtils.getTransactionalEntityManager(entityManagerFactory);
    if (entityManager == null) {
        return new EntityManagerSessionImpl(entityManagerFactory, handleTransactions, closeEntityManager);
    }
    return new EntityManagerSessionImpl(entityManagerFactory, entityManager, false, false);
}
项目:flowable-engine    文件:SpringEntityManagerSessionFactory.java   
@Override
public Session openSession(CommandContext commandContext) {
    EntityManager entityManager = EntityManagerFactoryUtils.getTransactionalEntityManager(entityManagerFactory);
    if (entityManager == null) {
        return new EntityManagerSessionImpl(entityManagerFactory, handleTransactions, closeEntityManager);
    }
    return new EntityManagerSessionImpl(entityManagerFactory, entityManager, false, false);
}
项目:cuba    文件:PersistenceImpl.java   
@Override
public EntityManager getEntityManager(String store) {
    if (!TransactionSynchronizationManager.isActualTransactionActive())
        throw new IllegalStateException("No active transaction");

    EntityManagerFactory emf;
    if (Stores.isMain(store))
        emf = this.jpaEmf;
    else
        emf = AppBeans.get("entityManagerFactory_" + store);

    javax.persistence.EntityManager jpaEm = EntityManagerFactoryUtils.doGetTransactionalEntityManager(emf, null);

    if (!jpaEm.isJoinedToTransaction())
        throw new IllegalStateException("No active transaction for " + store + " database");

    EntityManager entityManager = createEntityManager(jpaEm);

    EntityManagerContext ctx = contextHolder.get(store);
    if (ctx != null) {
        entityManager.setSoftDeletion(ctx.isSoftDeletion());
    } else {
        ctx = new EntityManagerContext();
        ctx.setSoftDeletion(isSoftDeletion());
        contextHolder.set(ctx, store);
        entityManager.setSoftDeletion(isSoftDeletion());
    }

    EntityManager emProxy = (EntityManager) Proxy.newProxyInstance(
            getClass().getClassLoader(),
            new Class[]{EntityManager.class},
            new EntityManagerInvocationHandler(entityManager, store)
    );
    return emProxy;
}
项目:spring-boot-concourse    文件:TestEntityManager.java   
/**
 * Return the underlying {@link EntityManager} that's actually used to perform all
 * operations.
 * @return the entity manager
 */
public final EntityManager getEntityManager() {
    EntityManager manager = EntityManagerFactoryUtils
            .getTransactionalEntityManager(this.entityManagerFactory);
    Assert.state(manager != null, "No transactional EntityManager found");
    return manager;
}
项目:owsi-core-parent    文件:EntityManagerUtils.java   
/**
 * Suppression du EntityManager.
 */
public void closeEntityManager() {
    if (TransactionSynchronizationManager.hasResource(entityManagerFactory)) {
        EntityManagerHolder entityManagerHolder = (EntityManagerHolder) TransactionSynchronizationManager.unbindResource(entityManagerFactory);
        EntityManagerFactoryUtils.closeEntityManager(entityManagerHolder.getEntityManager());
    }
}
项目:SecureBPMN    文件:SpringEntityManagerSessionFactory.java   
public Session openSession() {
  EntityManager entityManager = EntityManagerFactoryUtils.getTransactionalEntityManager(entityManagerFactory);
  if (entityManager == null) {
    return new EntityManagerSessionImpl(entityManagerFactory, handleTransactions, closeEntityManager);
  }
  return new EntityManagerSessionImpl(entityManagerFactory, entityManager, false, false);
}
项目:ef-orm    文件:AbstractGqQuery.java   
protected Session getSession() {
    EntityManager em = EntityManagerFactoryUtils.doGetTransactionalEntityManager(emf, null);
    if (em == null) { // 当无事务时。Spring返回null
        em = emf.createEntityManager(null, Collections.EMPTY_MAP);
    }
    if (em instanceof JefEntityManager) {
        return ((JefEntityManager) em).getSession();
    }
    throw new IllegalArgumentException(em.getClass().getName());
}
项目:class-guard    文件:OpenEntityManagerInViewInterceptor.java   
public void afterCompletion(WebRequest request, Exception ex) throws DataAccessException {
    if (!decrementParticipateCount(request)) {
        EntityManagerHolder emHolder = (EntityManagerHolder)
                TransactionSynchronizationManager.unbindResource(getEntityManagerFactory());
        logger.debug("Closing JPA EntityManager in OpenEntityManagerInViewInterceptor");
        EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager());
    }
}
项目:class-guard    文件:PersistenceAnnotationBeanPostProcessor.java   
/**
 * Find an EntityManagerFactory with the given name in the current
 * Spring application context.
 * @param unitName the name of the persistence unit (never empty)
 * @param requestingBeanName the name of the requesting bean
 * @return the EntityManagerFactory
 * @throws NoSuchBeanDefinitionException if there is no such EntityManagerFactory in the context
 */
protected EntityManagerFactory findNamedEntityManagerFactory(String unitName, String requestingBeanName)
        throws NoSuchBeanDefinitionException {

    EntityManagerFactory emf = EntityManagerFactoryUtils.findEntityManagerFactory(this.beanFactory, unitName);
    if (this.beanFactory instanceof ConfigurableBeanFactory) {
        ((ConfigurableBeanFactory) this.beanFactory).registerDependentBean(unitName, requestingBeanName);
    }
    return emf;
}
项目:class-guard    文件:HibernateJpaDialect.java   
@Override
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
    if (ex instanceof HibernateException) {
        return SessionFactoryUtils.convertHibernateAccessException((HibernateException) ex);
    }
    if (ex instanceof PersistenceException && ex.getCause() instanceof HibernateException) {
        return SessionFactoryUtils.convertHibernateAccessException((HibernateException) ex.getCause());
    }
    return EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(ex);
}
项目:lutece-core    文件:JPAGenericDAO.java   
/**
 * Return the Entity Manager
 * 
 * @return The Entity Manager
 */
public EntityManager getEM( )
{
    EntityManagerFactory emf = getEntityManagerFactory( );

    if ( TransactionSynchronizationManager.isSynchronizationActive( ) )
    {
        // first, get Spring entitymanager (if available)
        try
        {
            EntityManager em = EntityManagerFactoryUtils.getTransactionalEntityManager( emf );

            if ( em == null )
            {
                LOG.error( "getEM(  ) : no EntityManager found. Will use native entity manager factory [Transaction will not be supported]" );
            }
            else
            {
                LOG.debug( "EntityManager found for the current transaction : " + em.toString( ) + " - using Factory : " + emf.toString( ) );

                return em;
            }
        }
        catch( DataAccessResourceFailureException ex )
        {
            LOG.error( ex );
        }
    }

    LOG.error( "getEM(  ) : no EntityManager found. Will use native entity manager factory [Transaction will not be supported]" );

    if ( _defaultEM == null )
    {
        _defaultEM = emf.createEntityManager( );
    }
    return _defaultEM;
}
项目:GisGMP    文件:SenderIteration.java   
@Override
public void execute() throws Exception {
    EntityManager em = emf.createEntityManager();
    EntityManagerHolder emHolder = new EntityManagerHolder(em);
    try {
        TransactionSynchronizationManager.bindResource(emf, emHolder);
        super.execute();
    } finally {
        EntityManagerHolder emHolderB = (EntityManagerHolder)
                TransactionSynchronizationManager.unbindResource(emf);
        log.debug("Closing JPA EntityManager in OpenEntityManagerInViewFilter");
        EntityManagerFactoryUtils.closeEntityManager(emHolderB.getEntityManager());
    }
    sleep(DateTry.Second*10);
}
项目:FiWare-Template-Handler    文件:SpringEntityManagerSessionFactory.java   
public Session openSession() {
  EntityManager entityManager = EntityManagerFactoryUtils.getTransactionalEntityManager(entityManagerFactory);
  if (entityManager == null) {
    return new EntityManagerSessionImpl(entityManagerFactory, handleTransactions, closeEntityManager);
  }
  return new EntityManagerSessionImpl(entityManagerFactory, entityManager, false, false);
}
项目:javaconfig-ftw    文件:SpringEntityManagerSessionFactory.java   
public Session openSession() {
  EntityManager entityManager = EntityManagerFactoryUtils.getTransactionalEntityManager(entityManagerFactory);
  if (entityManager == null) {
    return new EntityManagerSessionImpl(entityManagerFactory, handleTransactions, closeEntityManager);
  }
  return new EntityManagerSessionImpl(entityManagerFactory, entityManager, false, false);
}
项目:camunda-bpm-platform    文件:SpringEntityManagerSessionFactory.java   
public Session openSession() {
  EntityManager entityManager = EntityManagerFactoryUtils.getTransactionalEntityManager(entityManagerFactory);
  if (entityManager == null) {
    return new EntityManagerSessionImpl(entityManagerFactory, handleTransactions, closeEntityManager);
  }
  return new EntityManagerSessionImpl(entityManagerFactory, entityManager, false, false);
}
项目:spring-data-snowdrop    文件:JpaCrudTest.java   
@Before
public void setUp() {
    TransactionSynchronizationManager.initSynchronization();
    em = EntityManagerFactoryUtils.getTransactionalEntityManager(emf);
}
项目:lams    文件:PersistenceAnnotationBeanPostProcessor.java   
@Override
public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
    EntityManager emToClose = this.extendedEntityManagersToClose.remove(bean);
    EntityManagerFactoryUtils.closeEntityManager(emToClose);
}
项目:lams    文件:AsyncRequestInterceptor.java   
private void closeAfterTimeout() {
    if (this.timeoutInProgress) {
        logger.debug("Closing JPA EntityManager after async request timeout");
        EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager());
    }
}
项目:spring4-understanding    文件:PersistenceAnnotationBeanPostProcessor.java   
@Override
public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
    EntityManager emToClose = this.extendedEntityManagersToClose.remove(bean);
    EntityManagerFactoryUtils.closeEntityManager(emToClose);
}
项目:spring4-understanding    文件:AsyncRequestInterceptor.java   
private void closeAfterTimeout() {
    if (this.timeoutInProgress) {
        logger.debug("Closing JPA EntityManager after async request timeout");
        EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager());
    }
}
项目:unitils    文件:JpaModule.java   
protected EntityManager doGetPersistenceContext(Object testObject) {
    return EntityManagerFactoryUtils.getTransactionalEntityManager(getPersistenceUnit(testObject));
}
项目:unitils    文件:HibernateJpaJotmSpringTest.java   
@Before
public void initializeFixture() {
    person = new Person(1L, "johnDoe");
    entityManager = EntityManagerFactoryUtils.getTransactionalEntityManager(entityManagerFactory);
}