我有一个带有 Tomcat 7 和 Hibernate 4.3 的简单 servlet 项目,但是当我尝试使用 Hibernate 但不知道发生了什么时出现错误。
基本上,我的 servlet 调用Persistence.createEntityManagerFactory(PERSISTENCE_UNIT)
工厂,它调用factory.createEntityManager()
实体管理器,它将尝试持久化使用来自 Http 请求的参数创建的对象。当我调用java.lang.ClassNotFoundException: javax.persistence.Persistence
第一个方法从Persistence
. 这是堆栈:
(unit1
是我的包裹)
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)
这是 HibernateDaoAluno.java:16 行:
EntityManagerFactory factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
考虑
private static final String PERSISTENCE_UNIT = "lpoo2_unit1";
这是我的persistence.xml
文件,在 META-INF 文件夹中:
<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>
我究竟做错了什么?我需要帮助。谢谢!