多数据源使用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注解了的类、方法及属性

2)在应用中如有有多个persistence units,JBoss会校验所有的注解是否都有unitName

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的校验

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版本中修复

4.参考资料

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

2)PersistenceUnitSearch violates the JPA spec

你可能感兴趣的:(jboss,persistence,JBAS011470,unitName)