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:
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named databaseTest
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 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 :
<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.
my database is also up and running and I can query from the oracle developer tool
First of all check if you have Maven Libraries on the build path. In Eclipse you would go to
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):
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
or change eclipselink dependency to
<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? I see only that you are creating instance of EntityManagerFactory only. 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. 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 em = emf.createEntityManager();
Or you can use JNDI lookup.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.