/** * 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); }
/** * 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); }
/** * Parse and build all persistence unit infos defined in the specified XML file(s). * @param persistenceXmlLocation the resource location (can be a pattern) * @return the resulting PersistenceUnitInfo instances */ public SpringPersistenceUnitInfo[] readPersistenceUnitInfos(String persistenceXmlLocation) { return readPersistenceUnitInfos(new String[] {persistenceXmlLocation}); }
@Test public void defaultDomainWithScan() { this.manager.setPackagesToScan("org.springframework.orm.jpa.domain"); this.manager.setResourceLoader(new DefaultResourceLoader( CandidateComponentsTestClassLoader.disableIndex(getClass().getClassLoader()))); testDefaultDomain(); }
private SpringPersistenceUnitInfo buildDefaultPersistenceUnitInfo() { this.manager.preparePersistenceUnitInfos(); return (SpringPersistenceUnitInfo) this.manager.obtainDefaultPersistenceUnitInfo(); }
@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 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); }
/** * 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); }
@Override public DataSource getDataSource() { if (this.persistenceUnitInfo != null) { return (this.persistenceUnitInfo.getJtaDataSource() != null ? this.persistenceUnitInfo.getJtaDataSource() : this.persistenceUnitInfo.getNonJtaDataSource()); } return (this.internalPersistenceUnitManager.getDefaultJtaDataSource() != null ? this.internalPersistenceUnitManager.getDefaultJtaDataSource() : this.internalPersistenceUnitManager.getDefaultDataSource()); }
/** * 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 "jtaDataSource" * on the PersistenceUnitInfo passed to the PersistenceProvider, as well as * overriding data source configuration in {@code persistence.xml} (if any). * <p><b>NOTE: Only applied if no external PersistenceUnitManager specified.</b> * @see javax.persistence.spi.PersistenceUnitInfo#getJtaDataSource() * @see #setPersistenceUnitManager */ public void setJtaDataSource(DataSource jtaDataSource) { this.internalPersistenceUnitManager.setDataSourceLookup(new SingleDataSourceLookup(jtaDataSource)); this.internalPersistenceUnitManager.setDefaultJtaDataSource(jtaDataSource); }
@Override public void afterPropertiesSet() { if (this.loadTimeWeaver == null && InstrumentationLoadTimeWeaver.isInstrumentationAvailable()) { this.loadTimeWeaver = new InstrumentationLoadTimeWeaver(this.resourcePatternResolver.getClassLoader()); } preparePersistenceUnitInfos(); }
@Override public void setResourceLoader(ResourceLoader resourceLoader) { this.internalPersistenceUnitManager.setResourceLoader(resourceLoader); }
/** * Hook method allowing subclasses to customize each PersistenceUnitInfo. * <p>The default implementation delegates to all registered PersistenceUnitPostProcessors. * It is usually preferable to register further entity classes, jar files etc there * rather than in a subclass of this manager, to be able to reuse the post-processors. * @param pui the chosen PersistenceUnitInfo, as read from {@code persistence.xml}. * Passed in as MutablePersistenceUnitInfo. * @see #setPersistenceUnitPostProcessors */ protected void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo pui) { PersistenceUnitPostProcessor[] postProcessors = getPersistenceUnitPostProcessors(); if (postProcessors != null) { for (PersistenceUnitPostProcessor postProcessor : postProcessors) { postProcessor.postProcessPersistenceUnitInfo(pui); } } }
/** * Set the location of the {@code persistence.xml} file * we want to use. This is a Spring resource location. * <p>Default is "classpath:META-INF/persistence.xml". * <p><b>NOTE: Only applied if no external PersistenceUnitManager specified.</b> * @param persistenceXmlLocation a Spring resource String * identifying the location of the {@code persistence.xml} file * that this LocalContainerEntityManagerFactoryBean should parse * @see #setPersistenceUnitManager */ public void setPersistenceXmlLocation(String persistenceXmlLocation) { this.internalPersistenceUnitManager.setPersistenceXmlLocation(persistenceXmlLocation); }
/** * Specify one or more mapping resources (equivalent to {@code <mapping-file>} * entries in {@code persistence.xml}) for the default persistence unit. * Can be used on its own or in combination with entity scanning in the classpath, * in both cases avoiding {@code persistence.xml}. * <p>Note that mapping resources must be relative to the classpath root, * e.g. "META-INF/mappings.xml" or "com/mycompany/repository/mappings.xml", * so that they can be loaded through {@code ClassLoader.getResource}. * <p>If no explicit mapping resources have been specified next to * {@link #setPackagesToScan packages to scan}, 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>Note that specifying an empty array/list here suppresses the default * {@code META-INF/orm.xml} check. On the other hand, explicitly specifying * {@code META-INF/orm.xml} here will register that file even if it happens * to be co-located with a {@code persistence.xml} file. * <p><b>NOTE: Only applied if no external PersistenceUnitManager specified.</b> * @see #setPersistenceUnitManager * @see DefaultPersistenceUnitManager#setMappingResources */ public void setMappingResources(String... mappingResources) { this.internalPersistenceUnitManager.setMappingResources(mappingResources); }
/** * Specify the Spring LoadTimeWeaver to use for class instrumentation according * to the JPA class transformer contract. * <p>It is a not required to specify a LoadTimeWeaver: Most providers will be * able to provide a subset of their functionality without class instrumentation * as well, or operate with their VM agent specified on JVM startup. * <p>In terms of Spring-provided weaving options, the most important ones are * InstrumentationLoadTimeWeaver, which requires a Spring-specific (but very general) * VM agent specified on JVM startup, and ReflectiveLoadTimeWeaver, which interacts * with an underlying ClassLoader based on specific extended methods being available * on it. * <p><b>NOTE:</b> As of Spring 2.5, the context's default LoadTimeWeaver (defined * as bean with name "loadTimeWeaver") will be picked up automatically, if available, * removing the need for LoadTimeWeaver configuration on each affected target bean. * Consider using the {@code context:load-time-weaver} XML tag for creating * such a shared LoadTimeWeaver (autodetecting the environment by default). * <p><b>NOTE:</b> Only applied if no external PersistenceUnitManager specified. * Otherwise, the external {@link #setPersistenceUnitManager PersistenceUnitManager} * is responsible for the weaving configuration. * @see org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver * @see org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver */ @Override public void setLoadTimeWeaver(LoadTimeWeaver loadTimeWeaver) { this.internalPersistenceUnitManager.setLoadTimeWeaver(loadTimeWeaver); }
/** * Specify the JPA 2.0 shared cache mode for this persistence unit, * overriding a value in {@code persistence.xml} if set. * <p><b>NOTE: Only applied if no external PersistenceUnitManager specified.</b> * @since 4.0 * @see javax.persistence.spi.PersistenceUnitInfo#getSharedCacheMode() * @see #setPersistenceUnitManager */ public void setSharedCacheMode(SharedCacheMode sharedCacheMode) { this.internalPersistenceUnitManager.setSharedCacheMode(sharedCacheMode); }
/** * Specify the JPA 2.0 validation mode for this persistence unit, * overriding a value in {@code persistence.xml} if set. * <p><b>NOTE: Only applied if no external PersistenceUnitManager specified.</b> * @since 4.0 * @see javax.persistence.spi.PersistenceUnitInfo#getValidationMode() * @see #setPersistenceUnitManager */ public void setValidationMode(ValidationMode validationMode) { this.internalPersistenceUnitManager.setValidationMode(validationMode); }
/** * This implementation delegates to the LoadTimeWeaver, if specified. */ @Override public void addTransformer(ClassTransformer classTransformer) { if (this.loadTimeWeaver == null) { throw new IllegalStateException("Cannot apply class transformer without LoadTimeWeaver specified"); } this.loadTimeWeaver.addTransformer(new ClassFileTransformerAdapter(classTransformer)); }
private void testDefaultDomain() { SpringPersistenceUnitInfo puInfo = buildDefaultPersistenceUnitInfo(); assertThat(puInfo.getManagedClassNames(), containsInAnyOrder( "org.springframework.orm.jpa.domain.Person", "org.springframework.orm.jpa.domain.DriversLicense")); }