Skip to content

Commit c77832b

Browse files
committed
Added info-level logging for default unit detection
Issue: SPR-11333 (cherry picked from commit 88730bd)
1 parent 59893d1 commit c77832b

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/DefaultPersistenceUnitManager.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
import javax.persistence.spi.PersistenceUnitInfo;
3535
import javax.sql.DataSource;
3636

37+
import org.apache.commons.logging.Log;
38+
import org.apache.commons.logging.LogFactory;
39+
3740
import org.springframework.beans.factory.InitializingBean;
3841
import org.springframework.context.ResourceLoaderAware;
3942
import org.springframework.context.weaving.LoadTimeWeaverAware;
@@ -103,6 +106,8 @@ public class DefaultPersistenceUnitManager
103106
new AnnotationTypeFilter(Embeddable.class, false),
104107
new AnnotationTypeFilter(MappedSuperclass.class, false)};
105108

109+
protected final Log logger = LogFactory.getLog(getClass());
110+
106111
private String[] persistenceXmlLocations = new String[] {DEFAULT_PERSISTENCE_XML_LOCATION};
107112

108113
private String defaultPersistenceUnitRootLocation = ORIGINAL_DEFAULT_PERSISTENCE_UNIT_ROOT_LOCATION;
@@ -359,6 +364,7 @@ public void afterPropertiesSet() {
359364
public void preparePersistenceUnitInfos() {
360365
this.persistenceUnitInfoNames.clear();
361366
this.persistenceUnitInfos.clear();
367+
362368
List<SpringPersistenceUnitInfo> puis = readPersistenceUnitInfos();
363369
for (SpringPersistenceUnitInfo pui : puis) {
364370
if (pui.getPersistenceUnitRootUrl() == null) {
@@ -400,18 +406,29 @@ public void preparePersistenceUnitInfos() {
400406
*/
401407
private List<SpringPersistenceUnitInfo> readPersistenceUnitInfos() {
402408
List<SpringPersistenceUnitInfo> infos = new LinkedList<SpringPersistenceUnitInfo>();
409+
String defaultName = this.defaultPersistenceUnitName;
403410
boolean buildDefaultUnit = (this.packagesToScan != null || this.mappingResources != null);
411+
boolean foundDefaultUnit = false;
412+
404413
PersistenceUnitReader reader = new PersistenceUnitReader(this.resourcePatternResolver, this.dataSourceLookup);
405414
SpringPersistenceUnitInfo[] readInfos = reader.readPersistenceUnitInfos(this.persistenceXmlLocations);
406415
for (SpringPersistenceUnitInfo readInfo : readInfos) {
407416
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;
411419
}
412420
}
421+
413422
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+
}
415432
}
416433
return infos;
417434
}

0 commit comments

Comments
 (0)