当前位置: 首页 > 知识库问答 >
问题:

EntityManager的持久性提供程序没有命名为empDb

谢善
2023-03-14

我得到了这个错误:

线程“main”javax中出现异常。坚持不懈PersistenceException:在javax上没有名为EmployeeDb的EntityManager的持久性提供程序。坚持不懈坚持不懈javax上的createEntityManagerFactory(Persistence.java:85)。坚持不懈坚持不懈staffManagement的createEntityManagerFactory(Persistence.java:54)。测验测试线束。main(TestHarness.java:14)

但我就是不明白为什么——我在我的家用电脑上做了同样的事情,没有问题。这是我的测试文件代码:

public class TestHarness {

public static void main(String[] args) {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("EmpDb");
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    tx.begin();

    Employee employee1 = new Employee("Brad", "Pitt", "Actor", 10000);
    em.persist(employee1);

    tx.commit();
    em.close();
}

}

还有我的坚持。xml文件:

<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_2_0.xsd"
version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence">

  <persistence-unit name="EmpDb"   transaction-type="RESOURCE_LOCAL">

  <class>staffManagement.domain.Employee</class>

  <properties>                    
        <property name="javax.persistence.jdbc.driver"   value="org.apache.derby.jdbc.ClientDriver" />
        <property name="javax.persistence.jdbc.url"      value="jdbc:derby://localhost:1527/DbName" />

        <property name="javax.persistence.jdbc.user"     value="APP" />
        <property name="javax.persistence.jdbc.password" value="APP" />

        <property name="eclipselink.logging.level"              value="FINE" /> 
        <property name="eclipselink.ddl-generation"             value="create-tables" />
        <property name="eclipselink.ddl-generation.output-mode" value="database" />
   </properties>

请帮帮我!我要把头发扯下来。我正在使用Eclipse和Derby db。我有毅力。meta inf文件夹中的xml文件

共有3个答案

澹台宾白
2023-03-14

奇怪的是,我甚至不用再加上这个。

持久性提供程序已自动完成。

我所做的是将这一行移到持久性单元标记上方,然后重新编译和重新部署,然后将其移回,重新编译和重新部署,结果成功了。

不知道为什么它会起作用,但它确实起了作用

单于承
2023-03-14

你需要移动持久性。将xml文件保存到适当的位置。在你的情况下,它应该是你的消息来源的兄弟姐妹。基本上是根包的兄弟,例如packageecho

以下来自JPA规范:

A persistence.xml file defines a persistence unit. The persistence.xml file is 
located in the META-INF directory of the root of the persistence unit. 

持久化单元的根是这里的关键。

如果你是非Java EE应用程序

The jar file or directory whose META-INF directory contains the persistence.xml 
file is termed the root of the persistence unit.

如果您在JavaEE应用程序中,则以下内容有效

In Java EE environments, the root of a persistence unit must be one of the following:
• an EJB-JAR file
• the WEB-INF/classes directory of a WAR file[80]
• a jar file in the WEB-INF/lib directory of a WAR file
• a jar file in the EAR library directory
• an application client jar file
楚骞尧
2023-03-14

你需要用持久性单元标记将所有内容包装在persistence.xml中。

    <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_2_0.xsd"
    version="2.0"
    xmlns="http://java.sun.com/xml/ns/persistence">

    <persistence-unit name="EmpDb">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> 
      <class>staffManagement.domain.Employee</class>

      <properties>                    
            <property name="javax.persistence.jdbc.driver"   value="org.apache.derby.jdbc.ClientDriver" />
            <property name="javax.persistence.jdbc.url"      value="jdbc:derby://localhost:1527/DbName" />
            <property name="eclipselink.target-database" value="Derby"/>            


            <property name="javax.persistence.jdbc.user"     value="APP" />
            <property name="javax.persistence.jdbc.password" value="APP" />

            <property name="eclipselink.logging.level"              value="FINE" /> 
            <property name="eclipselink.ddl-generation"             value="create-tables" />
            <property name="eclipselink.ddl-generation.output-mode" value="database" />
       </properties>
    </persistence-unit>

另外,请指定持久性提供程序和目标数据库:

<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<property name="eclipselink.target-database" value="Derby"/>            
 类似资料:
  • 问题内容: 我在目录下使用相同的名字。然后,我用以下代码调用它: 但是,我收到以下错误消息: 这是: 它应该在类路径中。但是,我得到了上面的错误。 问题答案: 在之后,定义持久性提供程序名称:

  • 我在REST服务器上工作,同时学习EJB\Hibernate。当服务调用DAO时,我面临一个问题,即它找不到我的持久性单元。 在本例中,我得到“无法为unitName PersistenceUnit检索EntityManagerFactory” 然后我试试这个: null pom.xml

  • 我试图在Eclipse中测试我的hibernate maven应用程序,当我运行获取enttity类名称的方法时,我得到了以下异常: 这是persistence.xml: 这是使用以下方法的类: 波姆。xml: 我尝试更改持久性的版本号、更改xmlns、提供程序的名称,但仍然是例外。

  • 我正在尝试调用persistence.xml来显示Netbean IDE中数据库中的一些数据。我已经查看并尝试了以前用户提出的不同方法,但我仍然无法解决这个问题。 我用这个来称呼持久性单元, 这是我试图运行文件时返回的结果, 这是第805行,错误似乎就是从这里产生的 如前所述, 我已经在持久化单元名称下添加了提供者, 我已经确保持久性是单元在META-INF文件夹中, 它在类路径中,所以我不确定错

  • 我正在使用JPA开发一个JavaSE应用程序。不幸的是,调用后我得到了: 下面你会发现: 调用并意外返回 我的坚持。xml文件 我的项目结构 我的代码片段: 我的坚持。xml: 我的项目结构:

  • 在drools workbench中,我创建了java类来获取entitymanager,并在drl文件中使用它,但我的问题是 当我在workbench中创建新项目时,我会像maven一样创建项目结构 当我打电话给全班的时候,像贝娄一样 persistence.xml像贝娄 还有pom。类xml 当我试图调用工厂类时,我没有为名为XXX的持久性提供程序 新工厂类