Announcement Announcement Module
Collapse
No announcement yet.
[SPR-9741] : Default PersistenceUnit is configured with wrong name Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • [SPR-9741] : Default PersistenceUnit is configured with wrong name

    Hi

    I raised this defect over in JIRA, but was wondering if anyone is having the same issue, or could suggest work-arounds?

    It's a blocker for us, preventing us from upgrading to Spring 3.1.2, or the 1.x release train for spring-data projects.

    I'm fairly certain it's a bug, but would be thrilled to find out I've screwed something up in my config somewhere.

    Here's the deets:

    When specifying the name of a persistence unit to use for LocalContainerEntityManagerFactoryBean (ie., assuming there are multiple persistence units defined in persistence.xml), this name is now applied to the persistence unit from scanned pacakges.

    In 3.1.0, the name applied to the generated package was 'default'. However, now, the name applied is the one defined in the entityManager configuration.

    This results in an exception "Conflicting persistence unit definitions for name 'xxx'".

    This is a breaking change in behaviour from 3.1.0 to 3.1.2.

    The change is caused by new setter behaviour introduced in LocalContainerEntityManagerFactoryBean in 3.1.2, specifically this code:

    Code:
    @Override
    public void setPersistenceUnitName(String persistenceUnitName) {    
          super.setPersistenceUnitName(persistenceUnitName);     
          this.internalPersistenceUnitManager.setDefaultPersistenceUnitName(persistenceUnitName); 
    }
    This changes the internal defaultPersistenceUnitName from default. Later, when DefaultPersistenceUnit.buildDefaultPersistenceUnit Info() is called, a new persistence unit is generated using this name:

    Code:
    private SpringPersistenceUnitInfo buildDefaultPersistenceUnitInfo() {
           SpringPersistenceUnitInfo scannedUnit = new SpringPersistenceUnitInfo();
           scannedUnit.setPersistenceUnitName(this.defaultPersistenceUnitName);
           scannedUnit.setExcludeUnlistedClasses(true);
    This then results in an exception being thrown.
    As an example, here's the defined Persistence.xml, which contains both a prod config, and a test config:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="1.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    <persistence-unit name="production">
    <properties>
    <!-- JPA Configuration goes here -->
    </properties>
    </persistence-unit>
    <persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
    <!-- JPA Configuration goes here -->
    </properties>
    </persistence-unit>
    </persistence>
    Then, the appropriate config is selected in the EntityManager configration:

    Code:
    <bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan">
    <list>
    <value>aa.bb.cc</value>
    </list>
    </property>
    <property name="persistenceUnitName" value="test" />
    <property name="jpaVendorAdapter">
    <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="showSql" value="true" />
    <property name="generateDdl" value="true" />
    <property name="databasePlatform" value="org.hibernate.dialect.H2Dialect" />
    </bean>
    </property>
    </bean>
    This causes the exception.

    Any help greatly appreciated.
Working...
X