java.lang.Classnotfoundexception: javax.persistence.Persistence

Asked

Viewed 12 times

0

I am in error when using Entitymanagerfactory, is a web project with Hibernate + JPA

This is the mistake:

java.lang.Classnotfoundexception: javax.persistence.Persistence at org.apache.Catalina.loader.Webappclassloaderbase.loadClass(Webappclassloaderbase.java:1407) at org.apache.Catalina.loader.Webappclassloaderbase.loadClass(Webappclassloaderbase.java:1215) at br.com.Antony.util.Jpautil.(Jpautil.java:9) at br.com.Antony.controller.Cadastrousuario.doPost(Cadastrousuario.java:47) at javax.servlet.http.HttpServlet.service(Httpservlet.java:681) at javax.servlet.http.HttpServlet.service(Httpservlet.java:764) at org.apache.Applicationfilterchain.internalDoFilter(Applicationfilterchain.java:228) at org.apache.Catalina.core.Applicationfilterchain.doFilter(Applicationfilterchain.java:163) at org.apache.Tomcat.websocket.server.WsFilter.doFilter(Wsfilter.java:53) at org.apache.Applicationfilterchain.internalDoFilter(Applicationfilterchain.java:190) at org.apache.Catalina.core.Applicationfilterchain.doFilter(Applicationfilterchain.java:163) at org.apache.Catalina.core.Standardwrappervalve.invoke(Standardwrappervalve.java:202) at org.apache.Catalina.core.Standardcontextvalve.invoke(Standardcontextvalve.java:97) at org.apache.Catalina.authenticator.Authenticatorbase.invoke(Authenticatorbase.java:542) at org.apache.Catalina.core.Standardhostvalve.invoke(Standardhostvalve.java:143) at org.apache.Catalina.valves.Errorreportvalve.invoke(Errorreportvalve.java:92) at org.apache.Catalina.valves.Abstractaccesslogvalve.invoke(Abstractaccesslogvalve.java:687) at org.apache.Catalina.core.Standardenginevalve.invoke(Standardenginevalve.java:78) at org.apache.Catalina.connector.Coyoteadapter.service(Coyoteadapter.java:357) at org.apache.Coyote.http11.Http11processor.service(Http11processor.java:382) at org.apache.Coyote.AbstractProcessorLight.process(Abstractprocessorlight.java:65) at org.apache.Coyote.Abstractprotocol$Connectionhandler.process(Abstractprotocol.java:893) at org.apache.Tomcat.util.net.Nioendpoint$Socketprocessor.doRun(Nioendpoint.java:1723) at org.apache.Tomcat.util.net.SocketProcessorBase.run(Socketprocessorbase.java:49) at java.base/java.util.Concurrent.ThreadPoolExecutor.runWorker(Threadpoolexecutor.java:1128) at java.base/java.util.Concurrent.Threadpoolexecutor$Worker.run(Threadpoolexecutor.java:628) at org.apache.Tomcat.util.threads.Taskthread$Wrappingrunnable.run(Taskthread.java:61) at java.base/java.lang.Thread.run(Thread.java:834)

This is my Jpautil class:

public class JPAUtil {

    private static final EntityManagerFactory FACTORY = Persistence.createEntityManagerFactory("desafio");      public static EntityManager getEntityManager() {

Return FACTORY.createEntityManager(); } }

My Servlet:

@WebServlet("/cadastrousuario")
public class CadastroUsuario extends HttpServlet {

    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();

        Usuario usuario = new Usuario();
        usuario.setNome(request.getParameter("nome"));
        usuario.setEmail(request.getParameter("email"));
        usuario.setSenha(request.getParameter("senha"));

        Telefone telefone1 = new Telefone();
        // telefone1.setDdd(Integer.parseInt(request.getParameter("dddTelefone1")));
        telefone1.setNumero(request.getParameter("numeroTelefone1"));
        telefone1.setTipo(request.getParameter("tipoTelefone1"));

        Telefone telefone2 = new Telefone();
        // telefone2.setDdd(Integer.parseInt(request.getParameter("dddTelefone2")));
        telefone2.setNumero(request.getParameter("numeroTelefone2"));
        telefone2.setTipo(request.getParameter("tipoTelefone2"));

        List<Telefone> telefones = Arrays.asList(telefone1, telefone2);
        usuario.setTelefones(telefones);

        EntityManager entityManager = JPAUtil.getEntityManager();

        UsuarioDao dao = new UsuarioDao(entityManager);

        try {
            entityManager.getTransaction().begin();
            dao.cadastrar(usuario);
            entityManager.getTransaction().commit();
            entityManager.close();
            out.println("Cadastrado com Sucesso !!");
        } catch (Exception e) {
            out.println("Erro ao Cadastrar !!" + e.getMessage());
        }

    }
}

Persistence file:

 <?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
    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 http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

    <persistence-unit name="desafio" transaction-type="RESOURCE_LOCAL">
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test"/>
            <property name="javax.persistence.jdbc.user" value="sa" />
            <property name="javax.persistence.jdbc.password" value="" />
            
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="update"/>
        </properties>
    </persistence-unit>
</persistence>

And my 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>br.com.antony.app</groupId>
    <artifactId>desafio</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-pligun</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>11</source>
                    <release>11</release>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.2.Final</version>
        </dependency>
        
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.200</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.