/** * 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, @Nullable String requestingBeanName) throws NoSuchBeanDefinitionException { Assert.state(this.beanFactory != null, "ListableBeanFactory required for EntityManagerFactory bean lookup"); EntityManagerFactory emf = EntityManagerFactoryUtils.findEntityManagerFactory(this.beanFactory, unitName); if (requestingBeanName != null && this.beanFactory instanceof ConfigurableBeanFactory) { ((ConfigurableBeanFactory) this.beanFactory).registerDependentBean(unitName, requestingBeanName); } return emf; }
/** * 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 */ @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())); } }
/** * 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 */ @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())); } }
/** * 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, @Nullable String requestingBeanName) throws NoSuchBeanDefinitionException { Assert.state(this.beanFactory != null, "ListableBeanFactory required for EntityManagerFactory bean lookup"); EntityManagerFactory emf = EntityManagerFactoryUtils.findEntityManagerFactory(this.beanFactory, unitName); if (requestingBeanName != null && this.beanFactory instanceof ConfigurableBeanFactory) { ((ConfigurableBeanFactory) this.beanFactory).registerDependentBean(unitName, requestingBeanName); } return emf; }
/** * 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); } }
/** * 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 */ @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())); } }
/** * 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 */ @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())); } }
/** * 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); } }
/** * 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, @Nullable String requestingBeanName) throws NoSuchBeanDefinitionException { Assert.state(this.beanFactory != null, "ListableBeanFactory required for EntityManagerFactory bean lookup"); EntityManagerFactory emf = EntityManagerFactoryUtils.findEntityManagerFactory(this.beanFactory, unitName); if (requestingBeanName != null && this.beanFactory instanceof ConfigurableBeanFactory) { ((ConfigurableBeanFactory) this.beanFactory).registerDependentBean(unitName, requestingBeanName); } return emf; }
@Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof ContextRefreshedEvent) { for (Entry<String, List<Object>> entry : preloadMap.entrySet()) { List<Object> beanList = entry.getValue(); EntityManagerFactory emf = EntityManagerFactoryUtils .findEntityManagerFactory(applicationContext, entry .getKey()); EntityManager em = SharedEntityManagerCreator .createSharedEntityManager(emf); try { loadJpaBean.loadData(beanList, em); } catch (Exception e) { //Log here, need to catch because if errors are thrown then the transaction is left hanging for some reason } } } // Reset the preloadMap to an empty Map after processing them // This avoids multiple load of data beans when war is deployed preloadMap = new HashMap<String, List<Object>>(); }
@Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof ContextRefreshedEvent) { for (Entry<String, List<Object>> entry : preloadMap.entrySet()) { List<Object> beanList = entry.getValue(); EntityManagerFactory emf = EntityManagerFactoryUtils .findEntityManagerFactory(applicationContext, entry .getKey()); EntityManager em = SharedEntityManagerCreator .createSharedEntityManager(emf); try { loadJpaBean.loadData(beanList, em); } catch (Exception e) { //Log here, need to catch because if errors are thrown then the transaction is left hanging for some reason } } } // Reset the preloadMap to an empty Map after processing them // This avoids multiple load of data beans when war is deployed preloadMap = new HashMap<String, List<Object>>(); }
/** * 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(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); } }
/** * 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(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); } }
.findEntityManagerFactory(applicationContext, persistenceUnit); EntityManager em = SharedEntityManagerCreator .createSharedEntityManager(emf);
.findEntityManagerFactory(applicationContext, persistenceUnit); EntityManager em = SharedEntityManagerCreator .createSharedEntityManager(emf);
/** * 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 */ @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())); } }
protected EntityManager entityManager() { EntityManager entityManager = EntityManagerFactoryUtils.getTransactionalEntityManager( EntityManagerFactoryUtils.findEntityManagerFactory( ApplicationContextProvider.getBeanFactory(), AuthContextUtils.getDomain())); if (entityManager == null) { throw new IllegalStateException("Could not find EntityManager for domain " + AuthContextUtils.getDomain()); } return entityManager; }
/** * 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 */ @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())); } }
domainsHolder.getDomains().forEach((domain, datasource) -> { EntityManagerFactoryUtils.findEntityManagerFactory( ApplicationContextProvider.getBeanFactory(), domain).createEntityManager();
/** * 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); } }