@Test public void defaultDomainWithIndex() { this.manager.setPackagesToScan("org.springframework.orm.jpa.domain"); this.manager.setResourceLoader(new DefaultResourceLoader( CandidateComponentsTestClassLoader.index(getClass().getClassLoader(), new ClassPathResource("spring.components", Person.class)))); testDefaultDomain(); }
this.persistenceUnitInfos.clear(); List<SpringPersistenceUnitInfo> puis = readPersistenceUnitInfos(); for (SpringPersistenceUnitInfo pui : puis) { if (pui.getPersistenceUnitRootUrl() == null) { pui.setPersistenceUnitRootUrl(determineDefaultPersistenceUnitRootUrl()); pui.init(this.resourcePatternResolver.getClassLoader()); postProcessPersistenceUnitInfo(pui); String name = pui.getPersistenceUnitName(); if (!this.persistenceUnitInfoNames.add(name) && !isPersistenceUnitOverrideAllowed()) { StringBuilder msg = new StringBuilder(); msg.append("Conflicting persistence unit definitions for name '").append(name).append("': ");
private SpringPersistenceUnitInfo buildDefaultPersistenceUnitInfo() { this.manager.preparePersistenceUnitInfos(); return (SpringPersistenceUnitInfo) this.manager.obtainDefaultPersistenceUnitInfo(); }
/** * Specify the JDBC DataSource that the JPA persistence provider is supposed * to use for accessing the database. This is an alternative to keeping the * JDBC configuration in {@code persistence.xml}, passing in a Spring-managed * DataSource instead. * <p>In JPA speak, a DataSource passed in here will be used as "nonJtaDataSource" * on the PersistenceUnitInfo passed to the PersistenceProvider, as well as * overriding data source configuration in {@code persistence.xml} (if any). * Note that this variant typically works for JTA transaction management as well; * if it does not, consider using the explicit {@link #setJtaDataSource} instead. * <p><b>NOTE: Only applied if no external PersistenceUnitManager specified.</b> * @see javax.persistence.spi.PersistenceUnitInfo#getNonJtaDataSource() * @see #setPersistenceUnitManager */ public void setDataSource(DataSource dataSource) { this.internalPersistenceUnitManager.setDataSourceLookup(new SingleDataSourceLookup(dataSource)); this.internalPersistenceUnitManager.setDefaultDataSource(dataSource); }
private void performDllGeneration() throws MalformedURLException { for (String eachDatabaseProductName : this.databaseProductNames) { String[] allBasePackages = this.getBasePackages(); this.logger.info("Using base packages " + Joiner.on(", ").join(allBasePackages)); final PersistenceProvider provider = new PersistenceProvider(); final DefaultPersistenceUnitManager manager = new DefaultPersistenceUnitManager(); manager.setDefaultPersistenceUnitRootLocation(null); manager.setDefaultPersistenceUnitName("default"); manager.setPackagesToScan(allBasePackages); manager.setPersistenceXmlLocations(new String[0]); manager.afterPropertiesSet(); final MutablePersistenceUnitInfo puInfo = (MutablePersistenceUnitInfo) manager.obtainDefaultPersistenceUnitInfo(); puInfo.setPersistenceProviderPackageName(provider.getClass().getName()); puInfo.setPersistenceUnitRootUrl(new URL("http://foo.bar")); this.logger.info("Managed classes found: " + puInfo.getManagedClassNames().size()); this.logger.debug("Managed class names:\n -> " + Joiner.on("\n -> ").join(puInfo.getManagedClassNames())); final Map<String, Object> properties = this.buildProperties(eachDatabaseProductName); puInfo.getProperties().putAll(properties); provider.generateSchema(puInfo, properties); } } }
protected EntityManagerFactory createEntityManagerFactory() { DefaultPersistenceUnitManager internalPersistenceUnitManager = new DefaultPersistenceUnitManager(); String[] packagesToScan = getHibernateConnection().getPath().split(","); for (String packageName : packagesToScan) { LOG.info("Found package " + packageName); } internalPersistenceUnitManager.setPackagesToScan(packagesToScan); internalPersistenceUnitManager.preparePersistenceUnitInfos(); PersistenceUnitInfo persistenceUnitInfo = internalPersistenceUnitManager.obtainDefaultPersistenceUnitInfo(); HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter(); if (persistenceUnitInfo instanceof SmartPersistenceUnitInfo) { ((SmartPersistenceUnitInfo) persistenceUnitInfo).setPersistenceProviderPackageName(jpaVendorAdapter.getPersistenceProviderRootPackage()); } Map<String, String> map = new HashMap<>(); map.put(AvailableSettings.DIALECT, getProperty(AvailableSettings.DIALECT)); map.put(AvailableSettings.USE_SECOND_LEVEL_CACHE, Boolean.FALSE.toString()); map.put(AvailableSettings.PHYSICAL_NAMING_STRATEGY, getHibernateConnection().getProperties().getProperty(AvailableSettings.PHYSICAL_NAMING_STRATEGY)); map.put(AvailableSettings.IMPLICIT_NAMING_STRATEGY, getHibernateConnection().getProperties().getProperty(AvailableSettings.IMPLICIT_NAMING_STRATEGY)); EntityManagerFactoryBuilderImpl builder = (EntityManagerFactoryBuilderImpl) Bootstrap.getEntityManagerFactoryBuilder(persistenceUnitInfo, map); return builder.build(); }
DefaultPersistenceUnitManager manager = new DefaultPersistenceUnitManager(); manager.setDefaultPersistenceUnitName(getPersistenceUnitName()); manager.setPackagesToScan(packages.toArray(new String[packages.size()])); manager.afterPropertiesSet(); SmartPersistenceUnitInfo info = (SmartPersistenceUnitInfo) manager.obtainDefaultPersistenceUnitInfo(); info.setPersistenceProviderPackageName(provider.getClass().getName()); info.getProperties().putAll(map);
@Override protected EntityManagerFactory createEntityManagerFactory() { DefaultPersistenceUnitManager internalPersistenceUnitManager = new DefaultPersistenceUnitManager(); internalPersistenceUnitManager.setPersistenceXmlLocation(getHibernateConnection().getPath()); internalPersistenceUnitManager.setDefaultPersistenceUnitRootLocation(null); internalPersistenceUnitManager.preparePersistenceUnitInfos(); PersistenceUnitInfo persistenceUnitInfo = internalPersistenceUnitManager.obtainDefaultPersistenceUnitInfo(); EntityManagerFactoryBuilderImpl builder = (EntityManagerFactoryBuilderImpl) Bootstrap.getEntityManagerFactoryBuilder(persistenceUnitInfo, Collections.emptyMap()); return builder.build(); }
_log.debug( "> DS : " + ds.toString( ) ); DefaultPersistenceUnitManager pum = new DefaultPersistenceUnitManager( ); pum.setDefaultDataSource( ds ); pum.setPersistenceUnitPostProcessors( postProcessors ); pum.afterPropertiesSet( );
/** * Creates a persistence unit manager. * * @return a persistence unit manager. */ protected DefaultPersistenceUnitManager createPersistenceUnitManager() { DefaultPersistenceUnitManager pum = new DefaultPersistenceUnitManager(); // IMPORTANT! - setting these to empty String arrays, this triggers the DefaultPersistenceUnitManager to // behave appropriately and ignore persistence.xml files from META-INF/persistence.xml as well as allowing for // an empty/minimal persistence unit to be created. // // Note that while Intellij complains about "Redundant array creation for calling varargs method", we really do // need to pass an empty array here in order for this code to work properly. pum.setPersistenceXmlLocations(new String[0]); pum.setMappingResources(new String[0]); pum.setPackagesToScan(new String[0]); return pum; }
/** * {@inheritDoc} */ @Override public void afterPropertiesSet() throws PersistenceException { if (persistenceUnitManager.getDefaultJtaDataSource() != null && persistenceUnitManager.getDefaultDataSource() != null) { throw new IllegalStateException(getPersistenceUnitName() + ": " + getClass().getSimpleName() + " was configured with both a JTA and Non-JTA " + " datasource. Must configure one or the other, but not both."); } this.internalFactoryBean.setJpaPropertyMap(defaultAndMergeJpaProperties()); persistenceUnitManager.setPersistenceUnitPostProcessors(assemblePersistenceUnitPostProcessors()); persistenceUnitManager.afterPropertiesSet(); internalFactoryBean.afterPropertiesSet(); }
/** * Set whether to use Spring-based scanning for entity classes in the classpath * instead of using JPA's standard scanning of jar files with {@code persistence.xml} * markers in them. In case of Spring-based scanning, no {@code persistence.xml} * is necessary; all you need to do is to specify base packages to search here. * <p>Default is none. Specify packages to search for autodetection of your entity * classes in the classpath. This is analogous to Spring's component-scan feature * ({@link org.springframework.context.annotation.ClassPathBeanDefinitionScanner}). * <p><b>Note: There may be limitations in comparison to regular JPA scanning.</b> * In particular, JPA providers may pick up annotated packages for provider-specific * annotations only when driven by {@code persistence.xml}. As of 4.1, Spring's * scan can detect annotated packages as well if supported by the given * {@link JpaVendorAdapter} (e.g. for Hibernate). * <p>If no explicit {@link #setMappingResources mapping resources} have been * specified in addition to these packages, Spring's setup looks for a default * {@code META-INF/orm.xml} file in the classpath, registering it as a mapping * resource for the default unit if the mapping file is not co-located with a * {@code persistence.xml} file (in which case we assume it is only meant to be * used with the persistence units defined there, like in standard JPA). * <p><b>NOTE: Only applied if no external PersistenceUnitManager specified.</b> * @param packagesToScan one or more base packages to search, analogous to * Spring's component-scan configuration for regular Spring components * @see #setPersistenceUnitManager * @see DefaultPersistenceUnitManager#setPackagesToScan */ public void setPackagesToScan(String... packagesToScan) { this.internalPersistenceUnitManager.setPackagesToScan(packagesToScan); }
@Override protected void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo pui) { // Invoke normal post processing super.postProcessPersistenceUnitInfo(pui); PersistenceUnitInfo oldPui = getPersistenceUnitInfo(((PersistenceUnitInfo) pui).getPersistenceUnitName()); if (oldPui != null) { postProcessPersistenceUnitInfo(pui, oldPui); } }
@Override public void afterPropertiesSet() { if (this.loadTimeWeaver == null && InstrumentationLoadTimeWeaver.isInstrumentationAvailable()) { this.loadTimeWeaver = new InstrumentationLoadTimeWeaver(this.resourcePatternResolver.getClassLoader()); } preparePersistenceUnitInfos(); }
@Bean public DefaultPersistenceUnitManager persistenceUnitManager() { DefaultPersistenceUnitManager persistenceUnitManager = new DefaultPersistenceUnitManager(); persistenceUnitManager.setDefaultDataSource(dataSource()); return persistenceUnitManager; }
@Override public void afterPropertiesSet() throws PersistenceException { PersistenceUnitManager managerToUse = this.persistenceUnitManager; if (this.persistenceUnitManager == null) { this.internalPersistenceUnitManager.afterPropertiesSet(); managerToUse = this.internalPersistenceUnitManager; } this.persistenceUnitInfo = determinePersistenceUnitInfo(managerToUse); JpaVendorAdapter jpaVendorAdapter = getJpaVendorAdapter(); if (jpaVendorAdapter != null && this.persistenceUnitInfo instanceof SmartPersistenceUnitInfo) { String rootPackage = jpaVendorAdapter.getPersistenceProviderRootPackage(); if (rootPackage != null) { ((SmartPersistenceUnitInfo) this.persistenceUnitInfo).setPersistenceProviderPackageName(rootPackage); } } super.afterPropertiesSet(); }
/** * Set a persistence unit root location for the default persistence unit. * <p>Default is "classpath:", that is, the root of the current classpath * (nearest root directory). To be overridden if unit-specific resolution * does not work and the classpath root is not appropriate either. * <p><b>NOTE: Only applied if no external PersistenceUnitManager specified.</b> * @since 4.3.3 * @see DefaultPersistenceUnitManager#setDefaultPersistenceUnitRootLocation */ public void setPersistenceUnitRootLocation(String defaultPersistenceUnitRootLocation) { this.internalPersistenceUnitManager.setDefaultPersistenceUnitRootLocation(defaultPersistenceUnitRootLocation); }
/** * Uses the specified persistence unit name as the name of the default * persistence unit, if applicable. * <p><b>NOTE: Only applied if no external PersistenceUnitManager specified.</b> * @see DefaultPersistenceUnitManager#setDefaultPersistenceUnitName */ @Override public void setPersistenceUnitName(@Nullable String persistenceUnitName) { super.setPersistenceUnitName(persistenceUnitName); if (persistenceUnitName != null) { this.internalPersistenceUnitManager.setDefaultPersistenceUnitName(persistenceUnitName); } }
/** * Set the PersistenceUnitPostProcessors to be applied to the * PersistenceUnitInfo used for creating this EntityManagerFactory. * <p>Such post-processors can, for example, register further entity * classes and jar files, in addition to the metadata read from * {@code persistence.xml}. * <p><b>NOTE: Only applied if no external PersistenceUnitManager specified.</b> * @see #setPersistenceUnitManager */ public void setPersistenceUnitPostProcessors(PersistenceUnitPostProcessor... postProcessors) { this.internalPersistenceUnitManager.setPersistenceUnitPostProcessors(postProcessors); }
/** * Specify the JDBC DataSource that the JPA persistence provider is supposed to use for accessing the database. * * <p> * This is an alternative to keeping the JDBC configuration in {@code persistence.xml}, passing in a Spring-managed * DataSource instead. * </p> * * <p> * In JPA speak, a DataSource passed in here will be used as "nonJtaDataSource" on the PersistenceUnitInfo passed * to the PersistenceProvider, as well as overriding data source configuration in {@code persistence.xml} (if any). * Note that this variant typically works for JTA transaction management as well; if it does not, consider using the * explicit {@link #setJtaDataSource} instead. * </p> * * @param dataSource the DataSource to use for this EntityManagerFactory * * @see javax.persistence.spi.PersistenceUnitInfo#getNonJtaDataSource() * @see {@link DefaultPersistenceUnitManager#setDefaultDataSource(javax.sql.DataSource)} */ public void setDataSource(DataSource dataSource) { persistenceUnitManager.setDefaultDataSource(dataSource); }