“JBAS016069:将持久性单元注入CDI托管bean时出错。”部署WAR时依赖于EJB-JAR [英] "JBAS016069: Error injecting persistence unit into CDI managed bean." When deploying WAR with dependency to EJB-JAR

查看:187
本文介绍了“JBAS016069:将持久性单元注入CDI托管bean时出错。”部署WAR时依赖于EJB-JAR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下设置:

EJB-JAR:


  • 包含一些实体和DAO。

  • 保存DAO中使用的EntityManager的persistence.xml。

测试-WAR:


  • 包含一个注入其中一个的servlet EJB-JAR的DAO(使用@Inject或@EJB)。

问题:


  • 我目前正在将应用程序部署到Wildfly 8.x

  • 当我部署jar时部署后启动的单例,我可以按预期使用DAO。我从DB获得结果...

  • 但是当我尝试使用依赖集设置到jar时(在MANIFEST.MF或jboss-deployment-中) structure.xml)我总是得到Weld无法将持久性单元注入DAO bean的错误,因为它找不到具有我指定名称的持久性单元:

  • 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:将持久性单元注入CDI托管bean时出错。在部署test.war中找不到名为test的持久性单元

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

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.

那么为什么容器抱怨缺少持久性单元虽然它存在于jar中?

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

注入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>

注入DAO(试战):

@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


推荐答案

我刚从... \ src \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\ METET-INFto...... \ src \\\\\\\\\\\\\\\\\\\\如果在classes文件夹中没有META-INF / persistence.xml文件,则war不起作用。

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.

这篇关于“JBAS016069:将持久性单元注入CDI托管bean时出错。”部署WAR时依赖于EJB-JAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆