gpt4 book ai didi

jakarta-ee - "JBAS016069: Error injecting persistence unit into CDI managed bean."部署依赖于 EJB-JAR 的 WAR 时

转载 作者:行者123 更新时间:2023-12-01 05:06:26 28 4
gpt4 key购买 nike

我有以下设置:

EJB-JAR:

  • 包含一些实体和 DAO。
  • 保存 DAO 中使用的 EntityManager 的 persistence.xml。

  • 测试战:
  • 包含注入(inject) EJB-JAR 的 DAO 之一的 servlet(使用 @Inject 或 @EJB)。

  • 问题:
  • 我目前正在将应用程序部署到 Wildfly 8.x
  • 当我使用部署后启动的单例部署 jar 时,我可以按预期使用 DAO。我正在从数据库中获取结果...
  • 但是,当我尝试使用设置为 jar 的依赖项(在 MANIFEST.MF 或 jboss-deployment-structure.xml 中)部署 war 时,我总是收到 Weld 无法将持久性单元注入(inject) DAO bean 的错误,因为它找不到具有我指定名称的持久性单元:

    JBAS016069:将持久性单元注入(inject) CDI 托管 bean 时出错。在部署 test.war
  • 中找不到名为 test 的持久性单元

    这是一个奇怪的错误,因为对于 war ,容器不应该需要 persistence.xml 文件,因为它位于加载 dao 类的 ejb-jar 中。

    那么,为什么容器会提示缺少持久性单元,尽管它存在于 jar 中?

    EntityManager (ejb-jar) 的注入(inject):
    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>

    DAO 注入(inject)(测试战):
    @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 [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

    最佳答案

    我刚刚移动了文件 持久性.xml 从 "...\src\main\java\META-INF"到 "...\src\main\webapp\META-INF"并且它起作用了。如果类文件夹中没有 META-INF/persistence.xml 文件,则 war 不起作用。

    关于jakarta-ee - "JBAS016069: Error injecting persistence unit into CDI managed bean."部署依赖于 EJB-JAR 的 WAR 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28149056/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com