-
Notifications
You must be signed in to change notification settings - Fork 38.8k
Description
Nick Williams opened SPR-10764 and commented
As of Spring 4.0, LocalContainerEntityManagerFactoryBean
requires JPA 2.0 or higher. One of the great things about LocalContainerEntityManagerFactoryBean
is its ability to completely replace the need for a persistence.xml
file, thus resulting in a pure Java JPA configuration.
However, LocalContainerEntityManagerFactoryBean
is lacking several methods that replace elements in persistence.xml
. Because of this oversight, users have to do something like this to fully configure a persistence unit without persistence.xml
:
...
Map<String, Object> properties = new Hashtable<>();
properties.put("javax.persistence.transactionType",
PersistenceUnitTransactionType.RESOURCE_LOCAL);
properties.put("javax.persistence.sharedCache.mode",
SharedCacheMode.ENABLE_SELECTIVE);
properties.put("javax.persistence.validation.mode",
ValidationMode.NONE);
...
factory.setJpaPropertyMap(properties);
...
This is error prone, and even finding these properties names short of reading the spec is non-trivial. As such, many users simply create a persistence.xml
file anyway, and use it to specify the values that you can't specify with LocalContainerEntityManagerFactoryBean
. I propose adding methods to LocalContainerEntityManagerFactoryBean
to make up this gap. All of the elements indicated that we're replacing exist in JPA 2.0.
...
/** Replaces {@code <shared-cache-mode>} element */
public void setSharedCacheMode(javax.persistence.SharedCacheMode sharedCacheMode) { }
/** Replaces {@code transaction-type} attribute in {@code <persistence-unit>} element */
public void setTransactionType(javax.persistence.spi.PersistenceUnitTransactionType transactionType) { }
/** Replaces {@code <validation-mode>} element */
public void setValidationMode(javax.persistence.ValidationMode validationMode) { }
/** Shortcut for setting property javax.persistence.schema-generation.database.action */
public void setSchemaGenerationAction(String schemaGenerationAction) { }
...
I threw in a method for javax.persistence.schema-generation.database.action
as well since it is a commonly-needed persistence property.
There should, of course, be getters for these properties as well.
Affects: 4.0 M1
Referenced from: commits 219eeb2