Skip to content

Commit 62e5b9d

Browse files
committed
LocalContainerEntityManagerFactoryBean's "persistenceUnitName" applies to "packagesToScan" as well; DefaultPersistenceUnitManager uses containing jar as persistence unit root URL for default unit (SPR-8832)
1 parent f367619 commit 62e5b9d

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

org.springframework.orm/src/main/java/org/springframework/orm/jpa/LocalContainerEntityManagerFactoryBean.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -123,6 +123,17 @@ public void setPersistenceXmlLocation(String persistenceXmlLocation) {
123123
this.internalPersistenceUnitManager.setPersistenceXmlLocation(persistenceXmlLocation);
124124
}
125125

126+
/**
127+
* Uses the specified persistence unit name as the name of the default
128+
* persistence unit, if applicable.
129+
* <p><b>NOTE: Only applied if no external PersistenceUnitManager specified.</b>
130+
*/
131+
@Override
132+
public void setPersistenceUnitName(String persistenceUnitName) {
133+
super.setPersistenceUnitName(persistenceUnitName);
134+
this.internalPersistenceUnitManager.setDefaultPersistenceUnitName(persistenceUnitName);
135+
}
136+
126137
/**
127138
* Set whether to use Spring-based scanning for entity classes in the classpath
128139
* instead of using JPA's standard scanning of jar files with <code>persistence.xml</code>

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -55,6 +55,7 @@
5555
import org.springframework.jdbc.datasource.lookup.MapDataSourceLookup;
5656
import org.springframework.util.ClassUtils;
5757
import org.springframework.util.ObjectUtils;
58+
import org.springframework.util.ResourceUtils;
5859

5960
/**
6061
* Default implementation of the {@link PersistenceUnitManager} interface.
@@ -328,7 +329,7 @@ public void afterPropertiesSet() {
328329
/**
329330
* Prepare the PersistenceUnitInfos according to the configuration
330331
* of this manager: scanning for <code>persistence.xml</code> files,
331-
* parsing all matching files, configurating and post-processing them.
332+
* parsing all matching files, configuring and post-processing them.
332333
* <p>PersistenceUnitInfos cannot be obtained before this preparation
333334
* method has been invoked.
334335
* @see #obtainDefaultPersistenceUnitInfo()
@@ -404,6 +405,12 @@ private SpringPersistenceUnitInfo buildDefaultPersistenceUnitInfo() {
404405
String className = reader.getClassMetadata().getClassName();
405406
if (matchesFilter(reader, readerFactory)) {
406407
scannedUnit.addManagedClassName(className);
408+
if (scannedUnit.getPersistenceUnitRootUrl() == null) {
409+
URL url = resource.getURL();
410+
if (ResourceUtils.isJarURL(url)) {
411+
scannedUnit.setPersistenceUnitRootUrl(ResourceUtils.extractJarFileURL(url));
412+
}
413+
}
407414
}
408415
}
409416
}

org.springframework.orm/src/main/java/org/springframework/orm/jpa/persistenceunit/PersistenceUnitReader.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,7 +43,7 @@
4343
import org.springframework.util.xml.SimpleSaxErrorHandler;
4444

4545
/**
46-
* Internal helper class for reading <code>persistence.xml</code> files.
46+
* Internal helper class for reading JPA-compliant <code>persistence.xml</code> files.
4747
*
4848
* @author Costin Leau
4949
* @author Juergen Hoeller
@@ -227,7 +227,7 @@ protected URL determinePersistenceUnitRootUrl(Resource resource) throws IOExcept
227227
/**
228228
* Parse the unit info DOM element.
229229
*/
230-
protected SpringPersistenceUnitInfo parsePersistenceUnitInfo(Element persistenceUnit, String version) throws IOException { // JC: Changed
230+
protected SpringPersistenceUnitInfo parsePersistenceUnitInfo(Element persistenceUnit, String version) throws IOException {
231231
SpringPersistenceUnitInfo unitInfo = new SpringPersistenceUnitInfo();
232232

233233
// set JPA version (1.0 or 2.0)

org.springframework.orm/src/test/java/org/springframework/orm/jpa/eclipselink/eclipselink-manager.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
77
<property name="persistenceXmlLocation" value="org/springframework/orm/jpa/domain/persistence.xml"/>
8+
<!--
9+
<property name="persistenceUnitName" value="Person"/>
10+
<property name="packagesToScan" value="org.springframework.orm.jpa.domain"/>
11+
-->
812
<property name="dataSource" ref="dataSource"/>
913
<property name="jpaVendorAdapter">
1014
<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">

0 commit comments

Comments
 (0)