added "mappingResources" property to LocalContainerEntityManagerFactoryBean (pointing to orm.xml; SPR-8440)

This commit is contained in:
Juergen Hoeller 2011-12-06 18:18:57 +00:00
parent 1a2a3dd02a
commit d050a472a2
2 changed files with 58 additions and 20 deletions

View File

@ -140,6 +140,21 @@ public class LocalContainerEntityManagerFactoryBean extends AbstractEntityManage
this.internalPersistenceUnitManager.setPackagesToScan(packagesToScan);
}
/**
* Specify one or more mapping resources (equivalent to <code>&lt;mapping-file&gt;</code>
* entries in <code>persistence.xml</code>) 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</code>.
* <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</code>.
* <p><b>NOTE: Only applied if no external PersistenceUnitManager specified.</b>
* @see #setPersistenceUnitManager
*/
public void setMappingResources(String... mappingResources) {
this.internalPersistenceUnitManager.setMappingResources(mappingResources);
}
/**
* Specify the JDBC DataSource that the JPA persistence provider is supposed
* to use for accessing the database. This is an alternative to keeping the

View File

@ -93,7 +93,7 @@ public class DefaultPersistenceUnitManager
public final static String ORIGINAL_DEFAULT_PERSISTENCE_UNIT_NAME = "default";
private static final String RESOURCE_PATTERN = "/**/*.class";
private static final String ENTITY_CLASS_RESOURCE_PATTERN = "/**/*.class";
private static final boolean jpa2ApiPresent = ClassUtils.hasMethod(PersistenceUnitInfo.class, "getSharedCacheMode");
@ -111,6 +111,8 @@ public class DefaultPersistenceUnitManager
private String[] packagesToScan;
private String[] mappingResources;
private DataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
private DataSource defaultDataSource;
@ -181,6 +183,20 @@ public class DefaultPersistenceUnitManager
this.packagesToScan = packagesToScan;
}
/**
* Specify one or more mapping resources (equivalent to <code>&lt;mapping-file&gt;</code>
* entries in <code>persistence.xml</code>) 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</code>.
* <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</code>.
* @see #setPackagesToScan
*/
public void setMappingResources(String... mappingResources) {
this.mappingResources = mappingResources;
}
/**
* Specify the JDBC DataSources that the JPA persistence provider is supposed
* to use for accessing the database, resolving data source names in
@ -361,8 +377,8 @@ public class DefaultPersistenceUnitManager
PersistenceUnitReader reader = new PersistenceUnitReader(this.resourcePatternResolver, this.dataSourceLookup);
List<SpringPersistenceUnitInfo> infos = new LinkedList<SpringPersistenceUnitInfo>();
infos.addAll(Arrays.asList(reader.readPersistenceUnitInfos(this.persistenceXmlLocations)));
if (this.packagesToScan != null) {
infos.add(scanPackages());
if (this.packagesToScan != null || this.mappingResources != null) {
infos.add(buildDefaultPersistenceUnitInfo());
}
return infos;
}
@ -371,14 +387,15 @@ public class DefaultPersistenceUnitManager
* Perform Spring-based scanning for entity classes.
* @see #setPackagesToScan
*/
private SpringPersistenceUnitInfo scanPackages() {
private SpringPersistenceUnitInfo buildDefaultPersistenceUnitInfo() {
SpringPersistenceUnitInfo scannedUnit = new SpringPersistenceUnitInfo();
try {
scannedUnit.setPersistenceUnitName(this.defaultPersistenceUnitName);
scannedUnit.excludeUnlistedClasses();
if (this.packagesToScan != null) {
for (String pkg : this.packagesToScan) {
try {
String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
ClassUtils.convertClassNameToResourcePath(pkg) + RESOURCE_PATTERN;
ClassUtils.convertClassNameToResourcePath(pkg) + ENTITY_CLASS_RESOURCE_PATTERN;
Resource[] resources = this.resourcePatternResolver.getResources(pattern);
MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(this.resourcePatternResolver);
for (Resource resource : resources) {
@ -391,12 +408,18 @@ public class DefaultPersistenceUnitManager
}
}
}
return scannedUnit;
}
catch (IOException ex) {
throw new PersistenceException("Failed to scan classpath for unlisted classes", ex);
}
}
}
if (this.mappingResources != null) {
for (String mappingFileName : this.mappingResources) {
scannedUnit.addMappingFileName(mappingFileName);
}
}
return scannedUnit;
}
/**
* Check whether any of the configured entity type filters matches