Skip to content

Add LocalContainerEntityManagerFactoryBean missing methods to easily replace elements in persistence.xml [SPR-10764] #15390

@spring-projects-issues

Description

@spring-projects-issues

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

Metadata

Metadata

Assignees

Labels

in: dataIssues in data modules (jdbc, orm, oxm, tx)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions