Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-9741

Default PersistenceUnit is configured with wrong name, resulting in duplicates & causes RTE.

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Complete
    • Affects Version/s: 3.1.2
    • Fix Version/s: 3.1.3, 3.2 M2
    • Component/s: Data:ORM
    • Labels:
      None

      Description

      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:

      {{
      @Override
      public void setPersistenceUnitName(String persistenceUnitName)

      { super.setPersistenceUnitName(persistenceUnitName); this.internalPersistenceUnitManager.setDefaultPersistenceUnitName(persistenceUnitName); }

      }}

      This changes the internal defaultPersistenceUnitName from default. Later, when DefaultPersistenceUnit.buildDefaultPersistenceUnitInfo() is called, a new persistence unit is generated using this name:

      {{
      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:

      {{
      <?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:

      {{
      <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.

        Attachments

          Activity

            People

            • Assignee:
              juergen.hoeller Juergen Hoeller
              Reporter:
              martypitt Marty Pitt
              Last updater:
              Chris Beams
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                5 years, 37 weeks, 4 days ago