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); 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 * Specify the JDBC DataSource that the JPA persistence provider is supposed
* to use for accessing the database. This is an alternative to keeping the * 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"; 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"); private static final boolean jpa2ApiPresent = ClassUtils.hasMethod(PersistenceUnitInfo.class, "getSharedCacheMode");
@ -111,6 +111,8 @@ public class DefaultPersistenceUnitManager
private String[] packagesToScan; private String[] packagesToScan;
private String[] mappingResources;
private DataSourceLookup dataSourceLookup = new JndiDataSourceLookup(); private DataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
private DataSource defaultDataSource; private DataSource defaultDataSource;
@ -181,6 +183,20 @@ public class DefaultPersistenceUnitManager
this.packagesToScan = packagesToScan; 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 * Specify the JDBC DataSources that the JPA persistence provider is supposed
* to use for accessing the database, resolving data source names in * 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); PersistenceUnitReader reader = new PersistenceUnitReader(this.resourcePatternResolver, this.dataSourceLookup);
List<SpringPersistenceUnitInfo> infos = new LinkedList<SpringPersistenceUnitInfo>(); List<SpringPersistenceUnitInfo> infos = new LinkedList<SpringPersistenceUnitInfo>();
infos.addAll(Arrays.asList(reader.readPersistenceUnitInfos(this.persistenceXmlLocations))); infos.addAll(Arrays.asList(reader.readPersistenceUnitInfos(this.persistenceXmlLocations)));
if (this.packagesToScan != null) { if (this.packagesToScan != null || this.mappingResources != null) {
infos.add(scanPackages()); infos.add(buildDefaultPersistenceUnitInfo());
} }
return infos; return infos;
} }
@ -371,31 +387,38 @@ public class DefaultPersistenceUnitManager
* Perform Spring-based scanning for entity classes. * Perform Spring-based scanning for entity classes.
* @see #setPackagesToScan * @see #setPackagesToScan
*/ */
private SpringPersistenceUnitInfo scanPackages() { private SpringPersistenceUnitInfo buildDefaultPersistenceUnitInfo() {
SpringPersistenceUnitInfo scannedUnit = new SpringPersistenceUnitInfo(); SpringPersistenceUnitInfo scannedUnit = new SpringPersistenceUnitInfo();
try { scannedUnit.setPersistenceUnitName(this.defaultPersistenceUnitName);
scannedUnit.setPersistenceUnitName(this.defaultPersistenceUnitName); scannedUnit.excludeUnlistedClasses();
scannedUnit.excludeUnlistedClasses(); if (this.packagesToScan != null) {
for (String pkg : this.packagesToScan) { for (String pkg : this.packagesToScan) {
String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + try {
ClassUtils.convertClassNameToResourcePath(pkg) + RESOURCE_PATTERN; String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
Resource[] resources = this.resourcePatternResolver.getResources(pattern); ClassUtils.convertClassNameToResourcePath(pkg) + ENTITY_CLASS_RESOURCE_PATTERN;
MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(this.resourcePatternResolver); Resource[] resources = this.resourcePatternResolver.getResources(pattern);
for (Resource resource : resources) { MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(this.resourcePatternResolver);
if (resource.isReadable()) { for (Resource resource : resources) {
MetadataReader reader = readerFactory.getMetadataReader(resource); if (resource.isReadable()) {
String className = reader.getClassMetadata().getClassName(); MetadataReader reader = readerFactory.getMetadataReader(resource);
if (matchesFilter(reader, readerFactory)) { String className = reader.getClassMetadata().getClassName();
scannedUnit.addManagedClassName(className); if (matchesFilter(reader, readerFactory)) {
scannedUnit.addManagedClassName(className);
}
} }
} }
} }
catch (IOException ex) {
throw new PersistenceException("Failed to scan classpath for unlisted classes", ex);
}
} }
return scannedUnit;
} }
catch (IOException ex) { if (this.mappingResources != null) {
throw new PersistenceException("Failed to scan classpath for unlisted classes", ex); for (String mappingFileName : this.mappingResources) {
scannedUnit.addMappingFileName(mappingFileName);
}
} }
return scannedUnit;
} }
/** /**