Skip to content

Commit 219eeb2

Browse files
committed
Added JPA 2.0 compliant "sharedCacheMode" and "validationMode" properties to DefaultPersistenceUnitManager and LocalContainerEntityManagerFactoryBean
Issue: SPR-10764
1 parent a841923 commit 219eeb2

2 files changed

Lines changed: 58 additions & 3 deletions

File tree

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import javax.persistence.EntityManagerFactory;
2020
import javax.persistence.PersistenceException;
21+
import javax.persistence.SharedCacheMode;
22+
import javax.persistence.ValidationMode;
2123
import javax.persistence.spi.PersistenceProvider;
2224
import javax.persistence.spi.PersistenceUnitInfo;
2325
import javax.sql.DataSource;
@@ -66,6 +68,7 @@
6668
* metadata as assembled by this FactoryBean.
6769
*
6870
* <p><b>NOTE: Spring's JPA support requires JPA 2.0 or higher, as of Spring 4.0.</b>
71+
* Spring's persistence unit bootstrapping automatically detects JPA 2.1 at runtime.
6972
*
7073
* @author Juergen Hoeller
7174
* @author Rod Johnson
@@ -170,6 +173,28 @@ public void setMappingResources(String... mappingResources) {
170173
this.internalPersistenceUnitManager.setMappingResources(mappingResources);
171174
}
172175

176+
/**
177+
* Specify the JPA 2.0 shared cache mode for this persistence unit,
178+
* overriding a value in {@code persistence.xml} if set.
179+
* <p><b>NOTE: Only applied if no external PersistenceUnitManager specified.</b>
180+
* @see javax.persistence.spi.PersistenceUnitInfo#getSharedCacheMode()
181+
* @see #setPersistenceUnitManager
182+
*/
183+
public void setSharedCacheMode(SharedCacheMode sharedCacheMode) {
184+
this.internalPersistenceUnitManager.setSharedCacheMode(sharedCacheMode);
185+
}
186+
187+
/**
188+
* Specify the JPA 2.0 validation mode for this persistence unit,
189+
* overriding a value in {@code persistence.xml} if set.
190+
* <p><b>NOTE: Only applied if no external PersistenceUnitManager specified.</b>
191+
* @see javax.persistence.spi.PersistenceUnitInfo#getValidationMode()
192+
* @see #setPersistenceUnitManager
193+
*/
194+
public void setValidationMode(ValidationMode validationMode) {
195+
this.internalPersistenceUnitManager.setValidationMode(validationMode);
196+
}
197+
173198
/**
174199
* Specify the JDBC DataSource that the JPA persistence provider is supposed
175200
* to use for accessing the database. This is an alternative to keeping the

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

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import javax.persistence.Entity;
3131
import javax.persistence.MappedSuperclass;
3232
import javax.persistence.PersistenceException;
33+
import javax.persistence.SharedCacheMode;
34+
import javax.persistence.ValidationMode;
3335
import javax.persistence.spi.PersistenceUnitInfo;
3436
import javax.sql.DataSource;
3537

@@ -68,6 +70,9 @@
6870
* DataSource names are by default interpreted as JNDI names, and no load time weaving
6971
* is available (which requires weaving to be turned off in the persistence provider).
7072
*
73+
* <p><b>NOTE: Spring's JPA support requires JPA 2.0 or higher, as of Spring 4.0.</b>
74+
* Spring's persistence unit bootstrapping automatically detects JPA 2.1 at runtime.
75+
*
7176
* @author Juergen Hoeller
7277
* @since 2.0
7378
* @see #setPersistenceXmlLocations
@@ -124,6 +129,10 @@ public class DefaultPersistenceUnitManager
124129

125130
private String[] mappingResources;
126131

132+
private SharedCacheMode sharedCacheMode;
133+
134+
private ValidationMode validationMode;
135+
127136
private DataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
128137

129138
private DataSource defaultDataSource;
@@ -210,6 +219,24 @@ public void setMappingResources(String... mappingResources) {
210219
this.mappingResources = mappingResources;
211220
}
212221

222+
/**
223+
* Specify the JPA 2.0 shared cache mode for all of this manager's persistence
224+
* units, overriding any value in {@code persistence.xml} if set.
225+
* @see javax.persistence.spi.PersistenceUnitInfo#getSharedCacheMode()
226+
*/
227+
public void setSharedCacheMode(SharedCacheMode sharedCacheMode) {
228+
this.sharedCacheMode = sharedCacheMode;
229+
}
230+
231+
/**
232+
* Specify the JPA 2.0 validation mode for all of this manager's persistence
233+
* units, overriding any value in {@code persistence.xml} if set.
234+
* @see javax.persistence.spi.PersistenceUnitInfo#getValidationMode()
235+
*/
236+
public void setValidationMode(ValidationMode validationMode) {
237+
this.validationMode = validationMode;
238+
}
239+
213240
/**
214241
* Specify the JDBC DataSources that the JPA persistence provider is supposed
215242
* to use for accessing the database, resolving data source names in
@@ -324,9 +351,6 @@ public PersistenceUnitPostProcessor[] getPersistenceUnitPostProcessors() {
324351
* VM agent specified on JVM startup, and ReflectiveLoadTimeWeaver, which interacts
325352
* with an underlying ClassLoader based on specific extended methods being available
326353
* on it (for example, interacting with Spring's TomcatInstrumentableClassLoader).
327-
* <p><b>NOTE:</b> As of Spring 2.5, the context's default LoadTimeWeaver (defined
328-
* as bean with name "loadTimeWeaver") will be picked up automatically, if available,
329-
* removing the need for LoadTimeWeaver configuration on each affected target bean.</b>
330354
* Consider using the {@code context:load-time-weaver} XML tag for creating
331355
* such a shared LoadTimeWeaver (autodetecting the environment by default).
332356
* @see org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver
@@ -383,6 +407,12 @@ public void preparePersistenceUnitInfos() {
383407
if (pui.getNonJtaDataSource() == null) {
384408
pui.setNonJtaDataSource(this.defaultDataSource);
385409
}
410+
if (this.sharedCacheMode != null) {
411+
pui.setSharedCacheMode(this.sharedCacheMode);
412+
}
413+
if (this.validationMode != null) {
414+
pui.setValidationMode(this.validationMode);
415+
}
386416
if (this.loadTimeWeaver != null) {
387417
pui.init(this.loadTimeWeaver);
388418
}

0 commit comments

Comments
 (0)