1

I have a simple servlet project with Tomcat 7 and Hibernate 4.3, but I'm getting an error when I try to use Hibernate but don't known what is happening.

Basically, my servlet calls Persistence.createEntityManagerFactory(PERSISTENCE_UNIT), with the factory, it calls factory.createEntityManager() and with the entity manager, it will try to persist an object made with the parameters from Http request. The server is raising an exception of java.lang.ClassNotFoundException: javax.persistence.Persistence when I call the first method to get the factory from Persistence. Here is the stack:

(unit1 is my package)

java.lang.ClassNotFoundException: javax.persistence.Persistence
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    at unit1.dao_hibernate.HibernateDaoAluno.inserir(HibernateDaoAluno.java:16)
    at unit1.servlet.CadastroAluno.doPost(CadastroAluno.java:29)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

Here is HibernateDaoAluno.java:16 line:

EntityManagerFactory factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);

Consider

private static final String PERSISTENCE_UNIT = "lpoo2_unit1";

And here is my persistence.xml file, in META-INF folder:

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

    <persistence-unit name="lpoo2_unit1">
        <!-- provedor/implementacao do JPA -->
        <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <!-- entidade mapaeada -->
        <class>unit1.model.Aluno</class>
        <class>unit1.model.Disciplina</class>
        <class>unit1.model.Professor</class>
        <class>unit1.model.Turma</class>

        <properties>
            <!-- dados da conexao -->
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/lpoo2" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="root" />

            <!-- propriedades do hibernate -->
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.format_sql" value="true" />

            <!-- atualiza o banco, gera as tableas se for preciso -->
            <property name="hibernate.hbm2ddl.auto" value="update" />

        </properties>
    </persistence-unit>

</persistence>

What am I doing wrong? I need help. Thank you!

7
  • Have you looked into what a ClassNotFoundException is and when it's thrown? Commented Sep 29, 2013 at 18:22
  • @Sotirios Delimanolis I mean in this context Commented Sep 29, 2013 at 18:24
  • 1
    The context and even the class itself don't matter. The exception is thrown for one reason alone: a required class is not on the classpath. Commented Sep 29, 2013 at 18:25
  • But what class? If javax.persistence.Persistence weren't, the class won't even compile. Commented Sep 29, 2013 at 18:46
  • 1
    There are two commands, javac and java. They can each accept a -cp option to specify the classpath. If you specify it in javac, compilation should work, otherwise you will get some kind of cannot resolve symbol. For java, the program will fail when the application attempts to load a class that isn't specified in the classpath. Just check your deployment. Commented Sep 29, 2013 at 18:51

1 Answer 1

3

Your classpath misses the javax.persistence library. This is part of the Java EE SDK (which you might use when developing your application but it is not part of the Java SE SDK which you surely use to run your Tomcat application server.

Solution:

  • Add the javax.persistence Library to the classpath
  • (Or) Use a Java EE environment to run your tomcat.

Remember you can add libraries to either the classpath when calling java, set it as argument for your tomcat, add it to shared library or add it to your web-application's 'lib' directory.

Sign up to request clarification or add additional context in comments.

2 Comments

The problem was solved when I put Hibernate jars into lib folder. Thank you.
Hibernate has several library versions like hibernate-core, hibernate-annotations and a complete version. I for myself use hibernate-core without hibernate-annotations which results in not having no annotations that are incompatible with JPA (javax.persistence). In this scenario I have to add javax.persistence library myself.I am glad you found your solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.