“JBAS016069: Error injecting persistence unit into CDI managed bean.” When deploying WAR with dependency to EJB-JAR

放肆的年华 提交于 2020-01-03 17:42:53

问题


I have the following setup:

EJB-JAR:

  • Contains some entities and DAOs.
  • Holds the persistence.xml for the EntityManager used in the DAOs.

Test-WAR:

  • Contains a servlet that injects one of the DAOs of the EJB-JAR (either using @Inject or @EJB).

The Problem:

  • I am currently deploying the applications to Wildfly 8.x
  • When I deploy the jar with a singleton that is started after deployment, I'm able to use the DAOs as intended. I'm getting results from the DB ...
  • But when I try to deploy the war with a dependency set to the jar (in MANIFEST.MF or jboss-deployment-structure.xml) I always get the error that Weld cannot inject the persistence unit into the DAO bean since it cannot find the persistence unit with the name I have specified:

    JBAS016069: Error injecting persistence unit into CDI managed bean. Can't find a persistence unit named test in deployment test.war

This is a strange error since for the war the container shouldn't need the persistence.xml file because it is located in the ejb-jar where the dao classes are loaded.

So why is the container complaining about that missing persistence unit although it is present in the jar?


Injection of EntityManager (ejb-jar):

public class ShopDAO {
  @PersistenceContext(unitName="test")
  private EntityManager entityManager;
...

persistence.xml (ejb-jar):

<?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="test">
    <properties>
      <property name="hibernate.hbm2ddl.auto" value="validate" />
      <property name="hibernate.show_sql" value="true" />
      <property name="hibernate.format_sql" value="true" />
      <property name="hibernate.generate_statistics" value="false" />
      <property name="hibernate.use_sql_comments" value="false" />
      <property name="hibernate.bytecode.use_reflection_optimizer" value="false" />
      <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
      <property name="hibernate.connection.password" value="password"/>
      <property name="hibernate.connection.url" value="jdbc:oracle:thin:@dbms:1523:DEV11"/>
      <property name="hibernate.connection.username" value="testuser"/>
      <property name="hibernate.default_schema" value="TEST"/>
    </properties>
  </persistence-unit>
</persistence>

Injection of DAO (test-war):

@WebServlet("/status")
public class TestServlet extends HttpServlet {
  @Inject
  private ShopDAO shopDAO;

  protected void doGet(HttpServletRequest rq, HttpServletResponse rp) throws ServletException, IOException {
    //do something with the shop dao!
  }
}

Error log:

    ERROR [fail] MSC000001: Failed to start service jboss.deployment.unit."test.war".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."test.war".WeldStartService: Failed to start service
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1904)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: JBAS016069: Error injecting persistence unit into CDI managed bean. Can't find a persistence unit named test in deployment test.war
    at org.jboss.as.weld.services.bootstrap.WeldJpaInjectionServices.getScopedPUName(WeldJpaInjectionServices.java:110)
    at org.jboss.as.weld.services.bootstrap.WeldJpaInjectionServices.registerPersistenceContextInjectionPoint(WeldJpaInjectionServices.java:73)
    at org.jboss.weld.injection.ResourceInjectionFactory$PersistenceContextResourceInjectionProcessor.getResourceReferenceFactory(ResourceInjectionFactory.java:312)
    at org.jboss.weld.injection.ResourceInjectionFactory$PersistenceContextResourceInjectionProcessor.getResourceReferenceFactory(ResourceInjectionFactory.java:301)
    at org.jboss.weld.injection.ResourceInjectionFactory$ResourceInjectionProcessor.createFieldResourceInjection(ResourceInjectionFactory.java:206)
    at org.jboss.weld.injection.ResourceInjectionFactory$ResourceInjectionProcessor.createResourceInjections(ResourceInjectionFactory.java:182)
    at org.jboss.weld.injection.ResourceInjectionFactory.discoverType(ResourceInjectionFactory.java:405)
    at org.jboss.weld.injection.ResourceInjectionFactory.getResourceInjections(ResourceInjectionFactory.java:92)
    at org.jboss.weld.injection.producer.ResourceInjector.<init>(ResourceInjector.java:52)
    at org.jboss.weld.injection.producer.BeanInjectionTarget.initInjector(BeanInjectionTarget.java:55)
    at org.jboss.weld.injection.producer.BasicInjectionTarget.<init>(BasicInjectionTarget.java:68)
    at org.jboss.weld.injection.producer.BeanInjectionTarget.<init>(BeanInjectionTarget.java:49)
    at org.jboss.weld.manager.InjectionTargetFactoryImpl.chooseInjectionTarget(InjectionTargetFactoryImpl.java:126)
    at org.jboss.weld.manager.InjectionTargetFactoryImpl.createInjectionTarget(InjectionTargetFactoryImpl.java:88)
    at org.jboss.weld.bean.ManagedBean.<init>(ManagedBean.java:91)
    at org.jboss.weld.bean.ManagedBean.of(ManagedBean.java:71)
    at org.jboss.weld.bootstrap.AbstractBeanDeployer.createManagedBean(AbstractBeanDeployer.java:264)
    at org.jboss.weld.bootstrap.BeanDeployer.createClassBean(BeanDeployer.java:248)
    at org.jboss.weld.bootstrap.ConcurrentBeanDeployer$2.doWork(ConcurrentBeanDeployer.java:74)
    at org.jboss.weld.bootstrap.ConcurrentBeanDeployer$2.doWork(ConcurrentBeanDeployer.java:72)
    at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:60)
    at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:53)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    ... 3 more

回答1:


I just moved the file persistence.xml from "...\src\main\java\META-INF" to "...\src\main\webapp\META-INF" and it worked. The war does not work if in the classes folder there is not META-INF/persistence.xml file.



来源:https://stackoverflow.com/questions/28149056/jbas016069-error-injecting-persistence-unit-into-cdi-managed-bean-when-deplo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!