/** * 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); }
/** * 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); }
@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(); }
@Test public void defaultDomainWithScan() { this.manager.setPackagesToScan("org.springframework.orm.jpa.domain"); this.manager.setResourceLoader(new DefaultResourceLoader( CandidateComponentsTestClassLoader.disableIndex(getClass().getClassLoader()))); testDefaultDomain(); }
/** * 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); }
/** * 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. * * <p> * 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> * * <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> * * @param packagesToScan one or more base packages to search, analogous to Spring's component-scan configuration for * regular Spring components * * @see {@link DefaultPersistenceUnitManager#setPackagesToScan(String...)} */ public void setPackagesToScan(String... packagesToScan) { if (LOG.isInfoEnabled()) { LOG.info(getPersistenceUnitName() + ": Setting Packages to Scan for JPA Annotations:\n" + Arrays.deepToString(packagesToScan)); } persistenceUnitManager.setPackagesToScan(packagesToScan); converterPackageNames = Arrays.asList(packagesToScan); }
/** * 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; }
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(); }
manager.setPackagesToScan(packages.toArray(new String[packages.size()]));
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); } } }
@Override public Configuration buildConfigurationFromScanning(HibernateConnection connection) { String[] packagesToScan = connection.getPath().split(","); for (String packageName : packagesToScan) { log.info("Found package {}", packageName); } DefaultPersistenceUnitManager internalPersistenceUnitManager = new DefaultPersistenceUnitManager(); internalPersistenceUnitManager.setPackagesToScan(packagesToScan); String dialectName = connection.getProperties().getProperty("dialect", null); if (dialectName == null) { throw new IllegalArgumentException("A 'dialect' has to be specified."); } log.info("Found dialect {}", dialectName); internalPersistenceUnitManager.preparePersistenceUnitInfos(); PersistenceUnitInfo persistenceUnitInfo = internalPersistenceUnitManager.obtainDefaultPersistenceUnitInfo(); HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter(); jpaVendorAdapter.setDatabasePlatform(dialectName); Map<String, Object> jpaPropertyMap = jpaVendorAdapter.getJpaPropertyMap(); jpaPropertyMap.put("hibernate.archive.autodetection", "false"); if (persistenceUnitInfo instanceof SmartPersistenceUnitInfo) { ((SmartPersistenceUnitInfo) persistenceUnitInfo).setPersistenceProviderPackageName(jpaVendorAdapter.getPersistenceProviderRootPackage()); } EntityManagerFactoryBuilderImpl builder = (EntityManagerFactoryBuilderImpl) Bootstrap.getEntityManagerFactoryBuilder(persistenceUnitInfo, jpaPropertyMap); ServiceRegistry serviceRegistry = builder.buildServiceRegistry(); return builder.buildHibernateConfiguration(serviceRegistry); }