多數據源使用spring-data-jpa沒法部署到JBoss As Server

1.異常信息

Caused by: java.lang.IllegalArgumentException: JBAS011470: Persistence unitName was not specified and there are 2 persistence unit definitions in application deployment "".  Either change the application to have only one persistence unit definition or specify the unitName for each reference to a persistence unit.
        at org.jboss.as.jpa.container.PersistenceUnitSearch.resolvePersistenceUnitSupplier(PersistenceUnitSearch.java:69)
        at org.jboss.as.jpa.processor.JPAAnnotationParseProcessor.getPersistenceUnit(JPAAnnotationParseProcessor.java:284)
        at org.jboss.as.jpa.processor.JPAAnnotationParseProcessor.getBindingSource(JPAAnnotationParseProcessor.java:220)
        at org.jboss.as.jpa.processor.JPAAnnotationParseProcessor.processMethod(JPAAnnotationParseProcessor.java:186)
        at org.jboss.as.jpa.processor.JPAAnnotationParseProcessor.processPersistenceAnnotations(JPAAnnotationParseProcessor.java:123)
        at org.jboss.as.jpa.processor.JPAAnnotationParseProcessor.deploy(JPAAnnotationParseProcessor.java:90)
        at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:113) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]

2.問題緣由

1)JBoss在部署應用時,會校驗全部被@PersistenceContext、@PersistenceUnit註解了的類、方法及屬性 java

2)在應用中若有有多個persistence units,JBoss會校驗全部的註解是否都有unitName spring

public void deploy(DeploymentPhaseContext phaseContext)
    throws DeploymentUnitProcessingException
  {
    DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    EEModuleDescription eeModuleDescription = (EEModuleDescription)deploymentUnit.getAttachment(org.jboss.as.ee.component.Attachments.EE_MODULE_DESCRIPTION);
    CompositeIndex index = (CompositeIndex)deploymentUnit.getAttachment(org.jboss.as.server.deployment.Attachments.COMPOSITE_ANNOTATION_INDEX);
    EEApplicationClasses applicationClasses = (EEApplicationClasses)deploymentUnit.getAttachment(org.jboss.as.ee.component.Attachments.EE_APPLICATION_CLASSES_DESCRIPTION);

    List persistenceContexts = index.getAnnotations(PERSISTENCE_CONTEXT_ANNOTATION_NAME);

    processPersistenceAnnotations(deploymentUnit, eeModuleDescription, persistenceContexts, applicationClasses);

    List persistenceUnits = index.getAnnotations(PERSISTENCE_UNIT_ANNOTATION_NAME);

    processPersistenceAnnotations(deploymentUnit, eeModuleDescription, persistenceUnits, applicationClasses);

    if ((!persistenceContexts.isEmpty()) || (!persistenceUnits.isEmpty()))
      JPADeploymentMarker.mark(deploymentUnit);
  }

3)spring-data-jpa的jar包中的JpaRepositoryFactoryBean類中有個表有@PersistenceContext註解的方法,且未指明unitName,因此沒法經過JBoss的校驗 app

public class JpaRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable> extends TransactionalRepositoryFactoryBeanSupport<T, S, ID>
{
  private EntityManager entityManager;

  @PersistenceContext
  public void setEntityManager(EntityManager entityManager)
  {
    this.entityManager = entityManager;
  }

3.問題處理

在JBoss7.1版本中,以上問題沒法解決,根據JBoss的JIRA上的信息,這個問題在7.2.0.Final版本中修復 this

4.參考資料

1)multilpe persistence units with spring-data-jpa will not deploy to JBoss AS Server spa

2)PersistenceUnitSearch violates the JPA spec code

相關文章
相關標籤/搜索