|
35 | 35 | import javax.persistence.spi.PersistenceUnitInfo; |
36 | 36 | import javax.sql.DataSource; |
37 | 37 |
|
| 38 | +import org.apache.commons.logging.Log; |
| 39 | +import org.apache.commons.logging.LogFactory; |
| 40 | + |
38 | 41 | import org.springframework.beans.factory.InitializingBean; |
39 | 42 | import org.springframework.context.ResourceLoaderAware; |
40 | 43 | import org.springframework.context.weaving.LoadTimeWeaverAware; |
@@ -119,6 +122,8 @@ public class DefaultPersistenceUnitManager |
119 | 122 | } |
120 | 123 |
|
121 | 124 |
|
| 125 | + protected final Log logger = LogFactory.getLog(getClass()); |
| 126 | + |
122 | 127 | private String[] persistenceXmlLocations = new String[] {DEFAULT_PERSISTENCE_XML_LOCATION}; |
123 | 128 |
|
124 | 129 | private String defaultPersistenceUnitRootLocation = ORIGINAL_DEFAULT_PERSISTENCE_UNIT_ROOT_LOCATION; |
@@ -397,6 +402,7 @@ public void afterPropertiesSet() { |
397 | 402 | public void preparePersistenceUnitInfos() { |
398 | 403 | this.persistenceUnitInfoNames.clear(); |
399 | 404 | this.persistenceUnitInfos.clear(); |
| 405 | + |
400 | 406 | List<SpringPersistenceUnitInfo> puis = readPersistenceUnitInfos(); |
401 | 407 | for (SpringPersistenceUnitInfo pui : puis) { |
402 | 408 | if (pui.getPersistenceUnitRootUrl() == null) { |
@@ -439,18 +445,29 @@ public void preparePersistenceUnitInfos() { |
439 | 445 | */ |
440 | 446 | private List<SpringPersistenceUnitInfo> readPersistenceUnitInfos() { |
441 | 447 | List<SpringPersistenceUnitInfo> infos = new LinkedList<SpringPersistenceUnitInfo>(); |
| 448 | + String defaultName = this.defaultPersistenceUnitName; |
442 | 449 | boolean buildDefaultUnit = (this.packagesToScan != null || this.mappingResources != null); |
| 450 | + boolean foundDefaultUnit = false; |
| 451 | + |
443 | 452 | PersistenceUnitReader reader = new PersistenceUnitReader(this.resourcePatternResolver, this.dataSourceLookup); |
444 | 453 | SpringPersistenceUnitInfo[] readInfos = reader.readPersistenceUnitInfos(this.persistenceXmlLocations); |
445 | 454 | for (SpringPersistenceUnitInfo readInfo : readInfos) { |
446 | 455 | infos.add(readInfo); |
447 | | - if (this.defaultPersistenceUnitName != null && |
448 | | - this.defaultPersistenceUnitName.equals(readInfo.getPersistenceUnitName())) { |
449 | | - buildDefaultUnit = false; |
| 456 | + if (defaultName != null && defaultName.equals(readInfo.getPersistenceUnitName())) { |
| 457 | + foundDefaultUnit = true; |
450 | 458 | } |
451 | 459 | } |
| 460 | + |
452 | 461 | if (buildDefaultUnit) { |
453 | | - infos.add(buildDefaultPersistenceUnitInfo()); |
| 462 | + if (foundDefaultUnit) { |
| 463 | + if (logger.isInfoEnabled()) { |
| 464 | + logger.info("Found explicit default unit with name '" + defaultName + "' in persistence.xml - " + |
| 465 | + "overriding local default unit settings ('packagesToScan'/'mappingResources')"); |
| 466 | + } |
| 467 | + } |
| 468 | + else { |
| 469 | + infos.add(buildDefaultPersistenceUnitInfo()); |
| 470 | + } |
454 | 471 | } |
455 | 472 | return infos; |
456 | 473 | } |
|
0 commit comments