|
34 | 34 | import javax.persistence.spi.PersistenceUnitInfo; |
35 | 35 | import javax.sql.DataSource; |
36 | 36 |
|
| 37 | +import org.apache.commons.logging.Log; |
| 38 | +import org.apache.commons.logging.LogFactory; |
| 39 | + |
37 | 40 | import org.springframework.beans.factory.InitializingBean; |
38 | 41 | import org.springframework.context.ResourceLoaderAware; |
39 | 42 | import org.springframework.context.weaving.LoadTimeWeaverAware; |
@@ -103,6 +106,8 @@ public class DefaultPersistenceUnitManager |
103 | 106 | new AnnotationTypeFilter(Embeddable.class, false), |
104 | 107 | new AnnotationTypeFilter(MappedSuperclass.class, false)}; |
105 | 108 |
|
| 109 | + protected final Log logger = LogFactory.getLog(getClass()); |
| 110 | + |
106 | 111 | private String[] persistenceXmlLocations = new String[] {DEFAULT_PERSISTENCE_XML_LOCATION}; |
107 | 112 |
|
108 | 113 | private String defaultPersistenceUnitRootLocation = ORIGINAL_DEFAULT_PERSISTENCE_UNIT_ROOT_LOCATION; |
@@ -359,6 +364,7 @@ public void afterPropertiesSet() { |
359 | 364 | public void preparePersistenceUnitInfos() { |
360 | 365 | this.persistenceUnitInfoNames.clear(); |
361 | 366 | this.persistenceUnitInfos.clear(); |
| 367 | + |
362 | 368 | List<SpringPersistenceUnitInfo> puis = readPersistenceUnitInfos(); |
363 | 369 | for (SpringPersistenceUnitInfo pui : puis) { |
364 | 370 | if (pui.getPersistenceUnitRootUrl() == null) { |
@@ -400,18 +406,29 @@ public void preparePersistenceUnitInfos() { |
400 | 406 | */ |
401 | 407 | private List<SpringPersistenceUnitInfo> readPersistenceUnitInfos() { |
402 | 408 | List<SpringPersistenceUnitInfo> infos = new LinkedList<SpringPersistenceUnitInfo>(); |
| 409 | + String defaultName = this.defaultPersistenceUnitName; |
403 | 410 | boolean buildDefaultUnit = (this.packagesToScan != null || this.mappingResources != null); |
| 411 | + boolean foundDefaultUnit = false; |
| 412 | + |
404 | 413 | PersistenceUnitReader reader = new PersistenceUnitReader(this.resourcePatternResolver, this.dataSourceLookup); |
405 | 414 | SpringPersistenceUnitInfo[] readInfos = reader.readPersistenceUnitInfos(this.persistenceXmlLocations); |
406 | 415 | for (SpringPersistenceUnitInfo readInfo : readInfos) { |
407 | 416 | infos.add(readInfo); |
408 | | - if (this.defaultPersistenceUnitName != null && |
409 | | - this.defaultPersistenceUnitName.equals(readInfo.getPersistenceUnitName())) { |
410 | | - buildDefaultUnit = false; |
| 417 | + if (defaultName != null && defaultName.equals(readInfo.getPersistenceUnitName())) { |
| 418 | + foundDefaultUnit = true; |
411 | 419 | } |
412 | 420 | } |
| 421 | + |
413 | 422 | if (buildDefaultUnit) { |
414 | | - infos.add(buildDefaultPersistenceUnitInfo()); |
| 423 | + if (foundDefaultUnit) { |
| 424 | + if (logger.isInfoEnabled()) { |
| 425 | + logger.info("Found explicit default unit with name '" + defaultName + "' in persistence.xml - " + |
| 426 | + "overriding local default unit settings ('packagesToScan'/'mappingResources')"); |
| 427 | + } |
| 428 | + } |
| 429 | + else { |
| 430 | + infos.add(buildDefaultPersistenceUnitInfo()); |
| 431 | + } |
415 | 432 | } |
416 | 433 | return infos; |
417 | 434 | } |
|
0 commit comments