@Override public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry registry) { // Register JPA Datastore (transactional) if (beanFactory instanceof ListableBeanFactory) { String[] emfBeanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors( (ListableBeanFactory) beanFactory, EntityManagerFactory.class, true, false); if (emfBeanNames == null || emfBeanNames.length == 0) { emfBeanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors((ListableBeanFactory) beanFactory, AbstractEntityManagerFactoryBean.class, true, false); } if (emfBeanNames != null && emfBeanNames.length == 1) { String emfBeanName = (emfBeanNames[0].startsWith("&")) ? emfBeanNames[0].substring(1) : emfBeanNames[0]; JpaDatastoreRegistrar.registerDatastore(registry, null, PrimaryMode.AUTO, emfBeanName, true, false, beanClassLoader); } } }
/** * @param testObject The test instance, not null * @param jpaConfig The configuration parameters for the <code>EntityManagerFactory</code> * @return A completely configured <code>AbstractEntityManagerFactoryBean</code> */ protected AbstractEntityManagerFactoryBean createEntityManagerFactoryBean(Object testObject, JpaConfig jpaConfig) { LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean(); factoryBean.setDataSource(getDataSource()); factoryBean.setJpaVendorAdapter(getJpaProviderSupport().getSpringJpaVendorAdaptor()); String persistenceXmlFile = jpaConfig.getConfigFiles().iterator().next(); if (!StringUtils.isEmpty(persistenceXmlFile)) { factoryBean.setPersistenceXmlLocation(persistenceXmlFile); } factoryBean.setPersistenceUnitName(jpaConfig.getPersistenceUnitName()); LoadTimeWeaver loadTimeWeaver = getJpaProviderSupport().getLoadTimeWeaver(); if (loadTimeWeaver != null) { factoryBean.setLoadTimeWeaver(loadTimeWeaver); } if (jpaConfig.getConfigMethod() != null) { try { ReflectionUtils.invokeMethod(testObject, jpaConfig .getConfigMethod(), factoryBean); } catch (InvocationTargetException e) { throw new UnitilsException("Error while invoking custom config method", e.getCause()); } } factoryBean.afterPropertiesSet(); return factoryBean; }
@Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { if(bean instanceof AbstractEntityManagerFactoryBean){ SpringCacheRegionFactory.SPRING_CACHE_MANAGER.set( cacheManager ); beanNames.add( beanName ); } return bean; }
public ConfiguredOrmPersistenceUnit<EntityManagerFactory, Object> getConfiguredPersistenceUnit(Object testObject) { AbstractEntityManagerFactoryBean factoryBean = getEntityManagerFactoryBean(testObject); EntityManagerFactory entityManagerFactory = factoryBean.getObject(); Object providerSpecificConfigurationObject = getJpaModule().getJpaProviderSupport().getProviderSpecificConfigurationObject(factoryBean.getPersistenceProvider()); return new ConfiguredOrmPersistenceUnit<EntityManagerFactory, Object>(entityManagerFactory, providerSpecificConfigurationObject); }
/** * @param testObject The test instance, not null * @return Instance of {@link LocalSessionFactoryBean} that wraps the configuration of hibernate in spring */ protected AbstractEntityManagerFactoryBean getEntityManagerFactoryBean(Object testObject) { if (!getSpringModule().isApplicationContextConfiguredFor(testObject)) { return null; } Collection<?> entityManagerFactoryBeans = getSpringModule().getApplicationContext(testObject).getBeansOfType(AbstractEntityManagerFactoryBean.class).values(); if (entityManagerFactoryBeans.size() == 0) { return null; } return (AbstractEntityManagerFactoryBean) entityManagerFactoryBeans.iterator().next(); }
@Bean public AbstractEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) throws SQLException { HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter(); jpaVendorAdapter.setGenerateDdl(true); LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean(); bean.setJpaVendorAdapter(jpaVendorAdapter); bean.setPackagesToScan(Config.class.getPackage().getName()); bean.setDataSource(dataSource); return bean; }
@Bean public AbstractEntityManagerFactoryBean entityManagerFactory() { Map<String, Object> jpaProperties = new HashMap<>(); log.info("Setting up entity manager factory, with Hibernate properties ..."); LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean(); localContainerEntityManagerFactoryBean.setPackagesToScan("za.org.grassroot.core"); // localContainerEntityManagerFactoryBean.setJpaPropertyMap(jpaProperties); localContainerEntityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); localContainerEntityManagerFactoryBean.setDataSource(dataSource()); return localContainerEntityManagerFactoryBean; }
@Bean public AbstractEntityManagerFactoryBean entityManagerFactory() { LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean(); emf.setDataSource(dataSource()); emf.setPackagesToScan("papadie.model"); emf.setPersistenceProvider(new HibernatePersistence()); emf.getJpaPropertyMap().put("hibernate.hbm2ddl.auto", "create-drop"); emf.getJpaPropertyMap().put("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); emf.getJpaPropertyMap().put("hibernate.show_sql", "true"); emf.afterPropertiesSet(); return emf; }
@Bean public AbstractEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, DataSource dataSource) { return builder.dataSource(dataSource).packages("com.mrpehlivan").build(); }
/** * Constructor for JpaModule. */ public JpaModule() { super(); // Make sure recent version spring ORM module is in the classpath AbstractEntityManagerFactoryBean.class.getName(); }
protected boolean isCustomConfigMethod(Method annotatedMethod) { return annotatedMethod.getReturnType().toString().equals("void") && annotatedMethod.getParameterTypes().length == 1 && AbstractEntityManagerFactoryBean.class.isAssignableFrom(annotatedMethod.getParameterTypes()[0]); }
public ConfiguredOrmPersistenceUnit<EntityManagerFactory, Object> getConfiguredOrmPersistenceUnit(Object testObject, JpaConfig entityManagerConfig) { AbstractEntityManagerFactoryBean factoryBean = createEntityManagerFactoryBean(testObject, entityManagerConfig); EntityManagerFactory entityManagerFactory = factoryBean.getObject(); Object providerSpecificConfigurationObject = getJpaProviderSupport().getProviderSpecificConfigurationObject(factoryBean.getPersistenceProvider()); return new ConfiguredOrmPersistenceUnit<EntityManagerFactory, Object>(entityManagerFactory, providerSpecificConfigurationObject); }
public EntityManagerFactoryDependsOnPostProcessor(String... dependsOn) { super(EntityManagerFactory.class, AbstractEntityManagerFactoryBean.class, dependsOn); }
@Bean public SessionFactory sessionFactory(AbstractEntityManagerFactoryBean entityManagerFactory) throws SQLException { return ((HibernateEntityManagerFactory) entityManagerFactory.getObject()).getSessionFactory(); }
public static void main(String[] args) { boolean drop = false; boolean create = false; String outFile = null; String delimiter = ""; String activeSpringProfile = null; String dataAccessApplicationContextConfigClassPath = null; for (int i = 0; i < args.length; i++) { if (args[i].startsWith("--")) { if (args[i].equals("--drop")) { drop = true; } else if (args[i].equals("--create")) { create = true; } else if (args[i].startsWith("--output=")) { outFile = args[i].substring(9); } else if (args[i].startsWith("--delimiter=")) { delimiter = args[i].substring(12); } else if (args[i].startsWith("--activeSpringProfile=")) { activeSpringProfile = args[i].substring(22); } else if (args[i].startsWith("--dataAccessApplicationContextConfigClassPath=")) { dataAccessApplicationContextConfigClassPath = args[i].substring(46); } } } Formatter formatter = FormatStyle.DDL.getFormatter(); GenericXmlApplicationContext context = new GenericXmlApplicationContext(); if (activeSpringProfile != null) { context.getEnvironment().setActiveProfiles(activeSpringProfile); } context.load(dataAccessApplicationContextConfigClassPath); context.refresh(); AbstractEntityManagerFactoryBean bean = context .getBean(AbstractEntityManagerFactoryBean.class); Ejb3Configuration jpaConfiguration = new Ejb3Configuration().configure( bean.getPersistenceUnitInfo(), bean.getJpaPropertyMap()); SchemaExporter.export(jpaConfiguration, drop, create, outFile, delimiter, formatter); context.close(); }