我收到此错误:java.lang.ClassNotFoundException: javax.persistence.Persistence
这是我的项目:
持久性.xml
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="Forum">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>Model.Section</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/Forum" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="root" />
</properties>
</persistence-unit>
</persistence>
NewSectionController.java
package Controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Model.Section;
public class NewSectionController extends HttpServlet {
private static final long serialVersionUID = 1L;
public NewSectionController() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher rs = request.getRequestDispatcher("newSection.jsp");
rs.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String title = request.getParameter("titulo");
String description = request.getParameter("descricao");
Section section = new Section();
section.insertNewSection(title, description);
}
}
节.java
包装型号;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Persistence;
@Entity
public class Section {
@Id
@GeneratedValue
int idSection;
@Column
String titleSection;
@Column
String descriptionSection;
public void insertNewSection(String title, String description) {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("Forum");
EntityManager em = factory.createEntityManager();
Section section = new Section();
section.setTitleSection(title);
section.setDescriptionSection(description);
em.getTransaction().begin();
em.persist(section);
em.getTransaction().commit();
}
public int getIdSection() {
return idSection;
}
public void setIdSection(int idSection) {
this.idSection = idSection;
}
public String getTitleSection() {
return titleSection;
}
public void setTitleSection(String titleSection) {
this.titleSection = titleSection;
}
public String getDescriptionSection() {
return descriptionSection;
}
public void setDescriptionSection(String descriptionSection) {
this.descriptionSection = descriptionSection;
}
}
这些文件是这样组织的:
src/Controller/NewSectionController.java
src/Model/Section.java
src/META-INF/persistence.xml
resources/log4j.properties
lib/
WebContent/
我已经导入并添加了所有这些库的构建路径:
antlr-2.7.7.jar
c3p0-0.9.1.jar
dom4j-1.6.1.jar
ehcache-core-2.4.3.jar
hibernate-c3p0-4.2.2.Final.jar
hibernate-commons-annotations-4.0.2.Final.jar
hibernate-core-4.2.2.Final.jar
hibernate-ehcache-4.2.2.Final.jar
hibernate-entitymanager-4.2.2.Final.jar
hibernate-envers-4.2.2.Final.jar
hibernate-infinispan-4.2.2.Final-tests.jar
hibernate-infinispan-4.2.2.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
hibernate-proxool-4.2.2.Final.jar
infinispan-core-5.2.0.Beta3.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.GA.jar
jboss-logging-3.1.1.GA.jar
jboss-marshalling-1.3.15.GA.jar
jboss-marshalling-river-1.3.15.GA.jar
jboss-transaction-api_1.1_spec-1.0.1.Final.jar
jgroups-3.2.0.CR1.jar
proxool-0.8.3.jar
rhq-pluginAnnotations-3.0.4.jar
slf4j-api-1.6.1.jar
stax2-api-3.1.1.jar
staxmapper-1.1.0.Final.jar
woodstox-core-asl-4.1.1.jar
而且我仍然收到 java.lang.ClassNotFoundException: javax.persistence.Persistence 错误。
我应该怎么办?