4 Replies Latest reply: May 4, 2012 10:25 AM by Alexander Kiselyov RSS

    PersistenceUnitSearch violates the JPA spec?

    Alexander Kiselyov Newbie

      Hi,

       

      I'm not sure whether I should post in on the JIRA or this is not a bug and just my misunderstanding.

       

      Recently I've ran into the issue with multiple persistence units (AS7-2275): I have the EAR that comprise of several EJB-JAR modules, two of which contains persistence.xml files describing persistence unit.

      No persistence units are defined on EAR level. In each module I got EJBs that declare EntityManager references using @PersistenceContext annotations with no attributes (particulary, without unitName attr).

       

      As I understood, code that insures this behaviour, resides in org.jboss.as.jpa.container.PersistenceUnitSearch:

       

                  
      PersistenceUnitsInApplication persistenceUnitsInApplication = DeploymentUtils.getTopDeploymentUnit(deploymentUnit).getAttachment(PersistenceUnitsInApplication.PERSISTENCE_UNITS_IN_APPLICATION);
      
      
                  
      if (persistenceUnitsInApplication.getCount() > 1) {
                      
      // AS7-2275 no unitName and there is more than one persistence unit;
                      
      throw MESSAGES.noPUnitNameSpecifiedAndMultiplePersistenceUnits(persistenceUnitsInApplication.getCount(),DeploymentUtils.getTopDeploymentUnit(deploymentUnit));
                  
      }
      
      
      

       

      But as JPA 2.0 specification says (chapter 8.2.2 Persistence Unit Scope):

       

      When referencing a persistence unit using the unitName annotation element or persistence-
      unit-name deployment descriptor element, the visibility scope of the persistence unit is
      determined by its point of definition:
      A persistence unit that is defined at the level of an EJB-JAR, WAR, or application client jar is
      
             scoped to that EJB-JAR, WAR, or application jar respectively and is visible to the components
             defined in that jar or war.
      

       

      So, why should we take into account irrelevant persistence units (declared in other modules of EAR) while the specification clearly says otherwise? I guess, fixing it would be the "one small step" towards suggested "default persistence unit" enhancement

       

      (BTW, Oracle's OC4J supports such issue by default; it can be configured in AS-specific deployment descriptor orion-ejb-jar.xml. Maybe JBoss should introduce some JPA AS-specific descriptor too?)