简体   繁体   English

线程“ main”中的异常javax.persistence.PersistenceException:没有名为EntityTest的EntityManager的持久性提供程序

[英]Exception in thread “main” javax.persistence.PersistenceException: No Persistence provider for EntityManager named databaseTest

I am trying to use oracle database in my java web app, but I keep getting the error when I run my code as a java application: 我正在尝试在Java Web应用程序中使用oracle数据库,但是当我将代码作为Java应用程序运行时,却不断收到错误消息:

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named databaseTest 线程“主”中的异常javax.persistence.PersistenceException:没有名为EntityTest的EntityManager的持久性提供程序

here is my code just a simple code to create an entity manager: 这是我的代码,只是创建实体管理器的简单代码:

 package com.sabir.test;

import javax.persistence.Persistence;

public class DatabaseService {

    public static void main(String[] args) {
        System.out.println("test");
        Persistence.createEntityManagerFactory("databaseTest");
    }

}

and here is my persistence.xml file 这是我的persistence.xml文件

<persistence 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 persistence_1_0.xsd" version="1.0">
    <persistence-unit name= "databaseTest" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
            <property name="javax.persistence.jdbc.user" value="HR"/>
            <property name="javax.persistence.jdbc.password" value="password"/>
        </properties>
    </persistence-unit>
</persistence>

and if required here is my pom.xml file : 如果需要,这里是我的pom.xml文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.sabir.test</groupId>
  <artifactId>databaseTest</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>databaseTest Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
     <dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc6</artifactId>
    <version>11.2.0.3</version>
</dependency>
<dependency>
   <groupId>org.eclipse.persistence</groupId>
   <artifactId>eclipselink</artifactId>
   <version>2.5.0-RC1</version>
   <scope>compile</scope>
</dependency>

  </dependencies>

   <repositories>
    <repository>
      <id>codelds</id>
      <url>https://code.lds.org/nexus/content/groups/main-repo</url>
    </repository>
    <repository>
    <id>oss.sonatype.org</id>
    <name>OSS Sonatype Staging</name>
    <url>https://oss.sonatype.org/content/groups/staging</url>
</repository>
<repository>
   <id>EclipseLink</id>
   <url>http://download.eclipse.org/rt/eclipselink/maven.repo</url>
</repository>
  </repositories>

  <build>
    <finalName>databaseTest</finalName>
  </build>
</project>

I don't know why I am getting the error! 我不知道为什么我得到了错误! here is also my files structure : 这也是我的文件结构:

[在此处输入图片说明

I am suspecting the file structure but after I checked many websites, I believe this is the correct one. 我怀疑文件结构,但是在检查了许多网站之后,我认为这是正确的。 I also imported the eclipslink in maven so I believe that is all I need, I also added the repository or oracle. 我也在maven中导入了eclipslink,所以我相信这就是我所需要的,我还添加了存储库或oracle。

my database is also up and running and I can query from the oracle developer tool 我的数据库也已启动并且正在运行,我可以从oracle开发人员工具中查询

First of all check if you have Maven Libraries on the build path. 首先检查构建路径上是否有Maven库。 In Eclipse you would go to 在Eclipse中,您将转到

Project Properties -> Java Build Path -> Libraries.

If you do, check below: 如果这样做,请检查以下内容:

You don't have provider you are referring to on your classpath. 您在类路径中没有要引用的提供程序。

<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

I suggest to add the following to your dependencies(and remove eclipselink dependency): 我建议将以下内容添加到您的依赖项中(并删除eclipselink依赖项):

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>7.0</version>
    <scope>provided</scope>
</dependency>

or change eclipselink dependency to 或将eclipselink依赖项更改为

<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>org.eclipse.persistence.jpa</artifactId>
    <version>2.6.4</version>
</dependency>

How and where do you obtain EntityManager from EntityManagerFactory? 您如何以及在何处从EntityManagerFactory获取EntityManager? I see only that you are creating instance of EntityManagerFactory only. 我只看到您仅在创建EntityManagerFactory实例。 It should be 它应该是

EntityManagerFactory emf = Persistence.createEntityManagerFactory("databaseTest");
EntityManager em = emf.createEntityManager();

In general this approach is useful only if you want to bootstrap in Java SE environment. 通常,仅当您要在Java SE环境中进行引导时,此方法才有用。 Instead you might use dependency injection: 相反,您可以使用依赖项注入:

@PersistenceContext
EntityManager em;

or 要么

@PersistenceUnit(unitName="myUnit")
EntityMangerFactory emf;

then in your methods you can get EntityManager again as follows 然后在您的方法中,您可以再次获得EntityManager,如下所示

EntityManager em = emf.createEntityManager();

Or you can use JNDI lookup. 或者,您可以使用JNDI查找。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 线程“ main”中的异常javax.persistence.PersistenceException:名为lanceurApplication的EntityManager的持久性提供程序没有 - Exception in thread “main” javax.persistence.PersistenceException: No Persistence provider for EntityManager named lanceurApplication 线程“主”中的异常javax.persistence.PersistenceException:名为jpa-test的EntityManager的持久性提供程序没有 - Exception in thread “main” javax.persistence.PersistenceException: No Persistence provider for EntityManager named jpa-test javax.persistence.PersistenceException:名为aramis的EntityManager的持久性提供程序 - javax.persistence.PersistenceException: No Persistence provider for EntityManager named aramis javax.persistence.PersistenceException:没有名为customerManager的EntityManager的持久性提供程序 - javax.persistence.PersistenceException: No Persistence provider for EntityManager named customerManager javax.persistence.PersistenceException:没有名为EntityManager的持久性提供程序 - javax.persistence.PersistenceException: No Persistence provider for EntityManager named javax.persistence.PersistenceException:没有名为EntityManager的持久性提供程序 - javax.persistence.PersistenceException: No Persistence provider for EntityManager named javax.persistence.PersistenceException:EntityManager 没有持久性提供程序 - javax.persistence.PersistenceException: No Persistence provider for EntityManager 持久性单元错误:javax.persistence.PersistenceException:没有命名 EntityManager 的持久性提供程序 - Persistence unit error: javax.persistence.PersistenceException: No Persistence provider for EntityManager named 具有Java SE的JPA:javax.persistence.PersistenceException:EntityManager没有持久性提供程序 - JPA with Java SE: javax.persistence.PersistenceException: No Persistence provider for EntityManager jboss 7.0.1休眠javax.persistence.PersistenceException:EntityManager没有持久性提供程序 - jboss 7.0.1 hibernate javax.persistence.PersistenceException: No Persistence provider for EntityManager
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM