为什么存在 persistence.xml 时持久性单元命名为 null

Why is persistence unit named null when persistence.xml exists

我将 Wildfly 8.1 与包含实体的 EJB 项目 (EJB 3.2) 一起使用。当试图将实体管理器注入我的一个 Bean 时,我得到以下信息:

JBAS011440: Can't find a persistence unit named null in deployment \"EntitiesProject.jar\""},
"JBAS014771: Services with missing/unavailable dependencies" => [
    "jboss.deployment.unit.\"EntitiesProject.jar\".weld.weldClassIntrospector is missing [jboss.deployment.unit.\"EntitiesProject.jar\".beanmanager]",
    "jboss.naming.context.java.comp.EntitiesProject.EntitiesProject.MitarbeiterDAO.HandleDelegate is missing [jboss.naming.context.java.comp.EntitiesProject.EntitiesProject.MitarbeiterDAO]",
    "jboss.naming.context.java.comp.EntitiesProject.EntitiesProject.MitarbeiterDAO.InstanceName is missing [jboss.naming.context.java.comp.EntitiesProject.EntitiesProject.MitarbeiterDAO]",
    "jboss.naming.context.java.comp.EntitiesProject.EntitiesProject.MitarbeiterDAO.InAppClientContainer is missing [jboss.naming.context.java.comp.EntitiesProject.EntitiesProject.MitarbeiterDAO]",
    "jboss.naming.context.java.comp.EntitiesProject.EntitiesProject.MitarbeiterDAO.Validator is missing [jboss.naming.context.java.comp.EntitiesProject.EntitiesProject.MitarbeiterDAO]",
    "jboss.naming.context.java.comp.EntitiesProject.EntitiesProject.MitarbeiterDAO.ORB is missing [jboss.naming.context.java.comp.EntitiesProject.EntitiesProject.MitarbeiterDAO]",
    "jboss.naming.context.java.comp.EntitiesProject.EntitiesProject.MitarbeiterDAO.ValidatorFactory is missing [jboss.naming.context.java.comp.EntitiesProject.EntitiesProject.MitarbeiterDAO]"
]

这是我尝试注入 EntityManager 的 SessionBean:

@Stateless
public class MitarbeiterDAO implements MitarbeiterDAORemote {

    @PersistenceContext
    private EntityManager em;

    public int writeMitarbeiterToDB(Mitarbeiter m)
    {
        em.persist(m);
        return m.getId();
    }
}

我指定了以下 persistence.xml 文件,我将其放入 "project-name"/ejbModule/META-INF.

<persistence-unit name="mitarbeiter">
   <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>java:jboss/datasources/MySqlDS</jta-data-source>
   <properties>
      <property-name="hibernate.hbm2ddl.auto" value="create-drop"/>
   </properties>
</persistence-unit>

类似问题及其解决方法

更新(见评论):
- 我尝试使用 @EntityManager(unitName="mitarbeiter") 代替 仅 @EntityManager - 上面显示的会话 Bean 是我尝试注入的唯一地方 EntityManager

更新 2(见答案评论):

非常感谢任何帮助和提示。

几个可能的问题:

  • persistence.xml 文件的位置不正确。请将其放在存档根目录的 META-INF/ 目录中。如果您使用 Maven,它将是 src/main/resources/META-INF/persistence.xml.
  • 无效的 persistence.xml 文件:您在行 <persistence-unit name="mitarbeiter"/> 中关闭了 persistence-unit 标签(请注意末尾的 /
  • <property-name="hibernate.hbm2ddl.auto" value="create-drop"/> 无效。我想你想要:<property name="hibernate.hbm2ddl.auto" value="create-drop" />
  • 您正在使用的提供程序也已弃用。

一般来说,在您的情况下,JPA 2.1 的正确 persistence.xml 内容应该如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
  version="2.1">

  <persistence-unit name="mitarbeiter">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <jta-data-source>java:jboss/datasources/MySqlDS</jta-data-source>

    <properties>
      <property name="hibernate.hbm2ddl.auto" value="create-drop" />
    </properties>
  </persistence-unit>
</persistence>

这个问题我已经追了好几个星期了。我在托管服务器环境 MassiveGrid 上使用 Wildfly 20、AdoptOpenJDK 14、Maven 和 MySql。最终对我有用的是我在 JBoss 上找到的 post,它解决了我的问题。我的测试环境是 Windows 10,在没有修复的情况下工作正常。解决方案原来是在名为 Web Pages/META-INF 的服务下添加一个文件夹,并放置一个名为 java.sql.Driver 的文件,其中包含以下内容:

<?xml version="1.0" encoding="UTF-8"?>

<!--
  ~ JBoss, Home of Professional Open Source.
  ~ Copyright 2010, Red Hat, Inc., and individual contributors
  ~ as indicated by the @author tags. See the copyright.txt file in the
  ~ distribution for a full listing of individual contributors.
  ~
  ~ This is free software; you can redistribute it and/or modify it
  ~ under the terms of the GNU Lesser General Public License as
  ~ published by the Free Software Foundation; either version 2.1 of
  ~ the License, or (at your option) any later version.
  ~
  ~ This software is distributed in the hope that it will be useful,
  ~ but WITHOUT ANY WARRANTY; without even the implied warranty of
  ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  ~ Lesser General Public License for more details.
  ~
  ~ You should have received a copy of the GNU Lesser General Public
  ~ License along with this software; if not, write to the Free
  ~ Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  ~ 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  -->

<module xmlns="urn:jboss:module:1.0" name="com.mysql">
  <resources>
    <resource-root path="mysql-connector-java-8.0.18.jar"/>
  </resources>
  <dependencies>
    <module name="javax.api"/>
  </dependencies>
</module>