Search code examples
javamavenjpamodel-view-controllereclipse-jee

java.lang.ClassNotFoundException: javax.persistence.Persistence even though I added dependencies


Although I added the jpa dependencies as well as i created the persistence.xml file in the correct location, i can't help but get this error. Note that the database table was successfully created with a java application test class Connecting with the persistence in the controller

    private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("unit");
    private static EntityManager entityManager = emf.createEntityManager();
    private static EntityTransaction transactionObj = entityManager.getTransaction();

Here are the dependencies I used in the pom.xml

<dependencies>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.12</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>jakarta.servlet.jsp.jstl</artifactId>
            <version>1.2.6</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.5.1</version>
        </dependency>

    </dependencies>

Here is the project structure project structure


Solution

  • I tried this and I was able to run the project successfully.

    Right Click on your project -> select Properties -> select "Deployment Assembly" -> Add -> Select Java Build Path Entries -> Select Maven Dependencies -> Apply and Close

    Restart the server and it should work