20

I'm praticing with JPA API. I got an error

java.lang.ClassNotFoundException: javax.persistence.Persistence cannot be found

My code below:

EntityManagerFactory emf;
emf = Persistence.createEntityManagerFactory("mail");
EntityManager em = emf.createEntityManager();
Query query = em.createQuery("SELECT v FROM Version v");
List<Version> versions = query.getResultList();

The error at line emf = Persistence.createEntityManagerFactory("mail");

Any solution?

Adi
  • 2,364
  • 1
  • 17
  • 23
gamo
  • 1,549
  • 7
  • 24
  • 36
  • check if all required jars are in classpath. – Adi Sep 03 '14 at 08:20
  • `Persistence` and `EntityManagerFactory` normally are provided by the same jar (e.g. javaee-api-7.0.jar), can you tell us which one you are using? – Thomas Sep 03 '14 at 08:23
  • Which JPA provider are you using? Are you running standalone or in a (JEE) container? If standalone, compare the (runtime) classpath to your build path to find missing .jars in the classpath. – JimmyB Sep 03 '14 at 08:25
  • 2
    OK. I missed `javax.persistence.jar` in my classpath. Added and problem solved now. Thanks you. – gamo Sep 03 '14 at 08:34

3 Answers3

31

You are trying to set up a standalone JPA project. In order to do so you need a JPA provider jars. The two more popular providers are Eclipselink and Hibernate. If you are using maven you can add dependencies to their implementations.

  • For Eclipselink

    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.5.1</version>
    </dependency>
    
  • For Hibernate

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.3.6.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.3.6.Final</version>
    </dependency>
    

If you are not using maven you can download their implementations from their sites and put it in your classpath.

Some JPA quickstarts are recommending to add only the JPA API (interface declarations only) dependencies with maven.

    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>javax.persistence</artifactId>
        <version>2.1.0</version>
    </dependency>

or

    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>

This approach will be successful only in server environment as the server will provide appropriate implementation at runtime.

zbig
  • 3,830
  • 2
  • 29
  • 37
3

Right Click on Project -> Properties -> Search "Deployment Assembly" -> Add Maven Dependencies.

Atul Jain
  • 1,035
  • 2
  • 16
  • 24
-1

if using maven , add below dependency in Pom.xml

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>

then clean install maven