diff --git a/org.springframework.orm/src/main/java/org/springframework/orm/jpa/LocalContainerEntityManagerFactoryBean.java b/org.springframework.orm/src/main/java/org/springframework/orm/jpa/LocalContainerEntityManagerFactoryBean.java
index 6f6fd9219d..829d4703f6 100644
--- a/org.springframework.orm/src/main/java/org/springframework/orm/jpa/LocalContainerEntityManagerFactoryBean.java
+++ b/org.springframework.orm/src/main/java/org/springframework/orm/jpa/LocalContainerEntityManagerFactoryBean.java
@@ -120,7 +120,24 @@ public class LocalContainerEntityManagerFactoryBean extends AbstractEntityManage
* @see #setPersistenceUnitManager
*/
public void setPersistenceXmlLocation(String persistenceXmlLocation) {
- this.internalPersistenceUnitManager.setPersistenceXmlLocations(new String[] {persistenceXmlLocation});
+ this.internalPersistenceUnitManager.setPersistenceXmlLocation(persistenceXmlLocation);
+ }
+
+ /**
+ * Set whether to use Spring-based scanning for entity classes in the classpath
+ * instead of using JPA's standard scanning of jar files with persistence.xml
+ * markers in them. In case of Spring-based scanning, no persistence.xml
+ * is necessary; all you need to do is to specify base packages to search here.
+ *
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}). + *
NOTE: Only applied if no external PersistenceUnitManager specified. + * @param packagesToScan one or more base packages to search, analogous to + * Spring's component-scan configuration for regular Spring components + * @see #setPersistenceUnitManager + */ + public void setPackagesToScan(String[] packagesToScan) { + this.internalPersistenceUnitManager.setPackagesToScan(packagesToScan); } /** diff --git a/org.springframework.orm/src/main/java/org/springframework/orm/jpa/persistenceunit/DefaultPersistenceUnitManager.java b/org.springframework.orm/src/main/java/org/springframework/orm/jpa/persistenceunit/DefaultPersistenceUnitManager.java index 5dc25c6e91..e25f62dd34 100644 --- a/org.springframework.orm/src/main/java/org/springframework/orm/jpa/persistenceunit/DefaultPersistenceUnitManager.java +++ b/org.springframework.orm/src/main/java/org/springframework/orm/jpa/persistenceunit/DefaultPersistenceUnitManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,10 +21,16 @@ import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.net.URL; +import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; import java.util.Map; import java.util.Set; +import javax.persistence.Embeddable; +import javax.persistence.Entity; +import javax.persistence.MappedSuperclass; import javax.persistence.PersistenceException; import javax.persistence.spi.PersistenceUnitInfo; import javax.sql.DataSource; @@ -37,6 +43,11 @@ import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternUtils; +import org.springframework.core.type.classreading.CachingMetadataReaderFactory; +import org.springframework.core.type.classreading.MetadataReader; +import org.springframework.core.type.classreading.MetadataReaderFactory; +import org.springframework.core.type.filter.AnnotationTypeFilter; +import org.springframework.core.type.filter.TypeFilter; import org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver; import org.springframework.instrument.classloading.LoadTimeWeaver; import org.springframework.jdbc.datasource.lookup.DataSourceLookup; @@ -80,13 +91,26 @@ public class DefaultPersistenceUnitManager */ public final static String ORIGINAL_DEFAULT_PERSISTENCE_UNIT_ROOT_LOCATION = "classpath:"; + public final static String ORIGINAL_DEFAULT_PERSISTENCE_UNIT_NAME = "default"; + + private static final String RESOURCE_PATTERN = "/**/*.class"; + private static final boolean jpa2ApiPresent = ClassUtils.hasMethod(PersistenceUnitInfo.class, "getSharedCacheMode"); + private static final TypeFilter[] entityTypeFilters = new TypeFilter[] { + new AnnotationTypeFilter(Entity.class, false), + new AnnotationTypeFilter(Embeddable.class, false), + new AnnotationTypeFilter(MappedSuperclass.class, false)}; + private String[] persistenceXmlLocations = new String[] {DEFAULT_PERSISTENCE_XML_LOCATION}; private String defaultPersistenceUnitRootLocation = ORIGINAL_DEFAULT_PERSISTENCE_UNIT_ROOT_LOCATION; + private String defaultPersistenceUnitName = ORIGINAL_DEFAULT_PERSISTENCE_UNIT_NAME; + + private String[] packagesToScan; + private DataSourceLookup dataSourceLookup = new JndiDataSourceLookup(); private DataSource defaultDataSource; @@ -133,6 +157,30 @@ public class DefaultPersistenceUnitManager this.defaultPersistenceUnitRootLocation = defaultPersistenceUnitRootLocation; } + /** + * Specify the name of the default persistence unit, if any. Default is "default". + *
Primarily applied to a scanned persistence unit without persistence.xml
.
+ * Also applicable to selecting a default unit from several persistence units available.
+ * @see #setPackagesToScan
+ * @see #obtainDefaultPersistenceUnitInfo
+ */
+ public void setDefaultPersistenceUnitName(String defaultPersistenceUnitName) {
+ this.defaultPersistenceUnitName = defaultPersistenceUnitName;
+ }
+
+ /**
+ * Set whether to use Spring-based scanning for entity classes in the classpath
+ * instead of using JPA's standard scanning of jar files with persistence.xml
+ * markers in them. In case of Spring-based scanning, no persistence.xml
+ * is necessary; all you need to do is to specify base packages to search here.
+ *
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}).
+ */
+ public void setPackagesToScan(String[] packagesToScan) {
+ this.packagesToScan = packagesToScan;
+ }
+
/**
* Specify the JDBC DataSources that the JPA persistence provider is supposed
* to use for accessing the database, resolving data source names in
@@ -273,7 +321,7 @@ public class DefaultPersistenceUnitManager
public void preparePersistenceUnitInfos() {
this.persistenceUnitInfoNames.clear();
this.persistenceUnitInfos.clear();
- SpringPersistenceUnitInfo[] puis = readPersistenceUnitInfos();
+ Listpersistence.xml
,
* as defined in the JPA specification.
*/
- private SpringPersistenceUnitInfo[] readPersistenceUnitInfos() {
+ private List