From 219eeb2369a49af607197d4e464a6a4d772a7bce Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 2 Aug 2013 17:43:34 +0200 Subject: [PATCH] Added JPA 2.0 compliant "sharedCacheMode" and "validationMode" properties to DefaultPersistenceUnitManager and LocalContainerEntityManagerFactoryBean Issue: SPR-10764 --- ...ocalContainerEntityManagerFactoryBean.java | 25 +++++++++++++ .../DefaultPersistenceUnitManager.java | 36 +++++++++++++++++-- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/LocalContainerEntityManagerFactoryBean.java b/spring-orm/src/main/java/org/springframework/orm/jpa/LocalContainerEntityManagerFactoryBean.java index a9c165c8c8..3d25f6b15a 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/LocalContainerEntityManagerFactoryBean.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/LocalContainerEntityManagerFactoryBean.java @@ -18,6 +18,8 @@ package org.springframework.orm.jpa; import javax.persistence.EntityManagerFactory; import javax.persistence.PersistenceException; +import javax.persistence.SharedCacheMode; +import javax.persistence.ValidationMode; import javax.persistence.spi.PersistenceProvider; import javax.persistence.spi.PersistenceUnitInfo; import javax.sql.DataSource; @@ -66,6 +68,7 @@ import org.springframework.util.ClassUtils; * metadata as assembled by this FactoryBean. * *

NOTE: Spring's JPA support requires JPA 2.0 or higher, as of Spring 4.0. + * Spring's persistence unit bootstrapping automatically detects JPA 2.1 at runtime. * * @author Juergen Hoeller * @author Rod Johnson @@ -170,6 +173,28 @@ public class LocalContainerEntityManagerFactoryBean extends AbstractEntityManage this.internalPersistenceUnitManager.setMappingResources(mappingResources); } + /** + * Specify the JPA 2.0 shared cache mode for this persistence unit, + * overriding a value in {@code persistence.xml} if set. + *

NOTE: Only applied if no external PersistenceUnitManager specified. + * @see javax.persistence.spi.PersistenceUnitInfo#getSharedCacheMode() + * @see #setPersistenceUnitManager + */ + public void setSharedCacheMode(SharedCacheMode sharedCacheMode) { + this.internalPersistenceUnitManager.setSharedCacheMode(sharedCacheMode); + } + + /** + * Specify the JPA 2.0 validation mode for this persistence unit, + * overriding a value in {@code persistence.xml} if set. + *

NOTE: Only applied if no external PersistenceUnitManager specified. + * @see javax.persistence.spi.PersistenceUnitInfo#getValidationMode() + * @see #setPersistenceUnitManager + */ + public void setValidationMode(ValidationMode validationMode) { + this.internalPersistenceUnitManager.setValidationMode(validationMode); + } + /** * Specify the JDBC DataSource that the JPA persistence provider is supposed * to use for accessing the database. This is an alternative to keeping the diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/DefaultPersistenceUnitManager.java b/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/DefaultPersistenceUnitManager.java index e150f63c11..313ebbf563 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/DefaultPersistenceUnitManager.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/DefaultPersistenceUnitManager.java @@ -30,6 +30,8 @@ import javax.persistence.Embeddable; import javax.persistence.Entity; import javax.persistence.MappedSuperclass; import javax.persistence.PersistenceException; +import javax.persistence.SharedCacheMode; +import javax.persistence.ValidationMode; import javax.persistence.spi.PersistenceUnitInfo; import javax.sql.DataSource; @@ -68,6 +70,9 @@ import org.springframework.util.ResourceUtils; * DataSource names are by default interpreted as JNDI names, and no load time weaving * is available (which requires weaving to be turned off in the persistence provider). * + *

NOTE: Spring's JPA support requires JPA 2.0 or higher, as of Spring 4.0. + * Spring's persistence unit bootstrapping automatically detects JPA 2.1 at runtime. + * * @author Juergen Hoeller * @since 2.0 * @see #setPersistenceXmlLocations @@ -124,6 +129,10 @@ public class DefaultPersistenceUnitManager private String[] mappingResources; + private SharedCacheMode sharedCacheMode; + + private ValidationMode validationMode; + private DataSourceLookup dataSourceLookup = new JndiDataSourceLookup(); private DataSource defaultDataSource; @@ -210,6 +219,24 @@ public class DefaultPersistenceUnitManager this.mappingResources = mappingResources; } + /** + * Specify the JPA 2.0 shared cache mode for all of this manager's persistence + * units, overriding any value in {@code persistence.xml} if set. + * @see javax.persistence.spi.PersistenceUnitInfo#getSharedCacheMode() + */ + public void setSharedCacheMode(SharedCacheMode sharedCacheMode) { + this.sharedCacheMode = sharedCacheMode; + } + + /** + * Specify the JPA 2.0 validation mode for all of this manager's persistence + * units, overriding any value in {@code persistence.xml} if set. + * @see javax.persistence.spi.PersistenceUnitInfo#getValidationMode() + */ + public void setValidationMode(ValidationMode validationMode) { + this.validationMode = validationMode; + } + /** * Specify the JDBC DataSources that the JPA persistence provider is supposed * to use for accessing the database, resolving data source names in @@ -324,9 +351,6 @@ public class DefaultPersistenceUnitManager * VM agent specified on JVM startup, and ReflectiveLoadTimeWeaver, which interacts * with an underlying ClassLoader based on specific extended methods being available * on it (for example, interacting with Spring's TomcatInstrumentableClassLoader). - *

NOTE: As of Spring 2.5, the context's default LoadTimeWeaver (defined - * as bean with name "loadTimeWeaver") will be picked up automatically, if available, - * removing the need for LoadTimeWeaver configuration on each affected target bean. * Consider using the {@code context:load-time-weaver} XML tag for creating * such a shared LoadTimeWeaver (autodetecting the environment by default). * @see org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver @@ -383,6 +407,12 @@ public class DefaultPersistenceUnitManager if (pui.getNonJtaDataSource() == null) { pui.setNonJtaDataSource(this.defaultDataSource); } + if (this.sharedCacheMode != null) { + pui.setSharedCacheMode(this.sharedCacheMode); + } + if (this.validationMode != null) { + pui.setValidationMode(this.validationMode); + } if (this.loadTimeWeaver != null) { pui.init(this.loadTimeWeaver); }