我是Hibernate的servlet新手。我正在尝试使用html注册表格中的hibernate将数据存储在mysql数据库中。但是我一直将此错误作为根本原因。
这是弹出的两个根本原因。

HTTP status 500 Internal server error.

Exception

javax.servlet.ServletException: Servlet execution threw an exception
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause

java.lang.NoClassDefFoundError: javax/persistence/PersistenceException
    java.base/java.lang.ClassLoader.defineClass1(Native Method)
    java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1007)
    java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
    org.apache.catalina.loader.WebappClassLoaderBase.findClassInternal(WebappClassLoaderBase.java:2339)
    org.apache.catalina.loader.WebappClassLoaderBase.findClass(WebappClassLoaderBase.java:829)
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1278)
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1137)
    kish.hibernate.servletController.registerData.doGet(registerData.java:30)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause

java.lang.ClassNotFoundException: javax.persistence.PersistenceException
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1309)
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1137)
    java.base/java.lang.ClassLoader.defineClass1(Native Method)
    java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1007)
    java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
    org.apache.catalina.loader.WebappClassLoaderBase.findClassInternal(WebappClassLoaderBase.java:2339)
    org.apache.catalina.loader.WebappClassLoaderBase.findClass(WebappClassLoaderBase.java:829)
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1278)
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1137)
    kish.hibernate.servletController.registerData.doGet(registerData.java:30)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)


这是我的hibernate.cfg.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configurationn>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="hibernate.connection.password">Kish</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/demo</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="hbm2ddl.auto">create</property> <!-- create table every time -->
        <property name="show_sql">true</property>
        <mapping class="kish.hibernate.Pojo.Student"></mapping>

    </session-factory>

</hibernate-configurationn>


这是我的html文件

<html>
<head>
<title> RegistrationForm</title>
<link rel = "stylesheet" type = "text/css" href = "style1.css">
<body>


  <div class= "registrationBox">
        <img src= "avatar1.png" class= "avatar">
     <h1> enter your details below form</h1>

<form action = "registerData" method ="get">
    <p>Firstname</p>
    <input type="text" name="first_name" placeholder="Enter your firstname">
   <p>Lastname</p>
   <input type = "text" name= "last_name" placeholder="Your lastname ">
   <p>Branch</p>
   <input type = "text" name="branch" placeholder="your branch">
   <p>Id</p>
   <input type = "text" name = "Id" placeholder="your id">
   <p>password</p>
   <input type="password" name="password" placeholder="create your password">
   <input type="submit" name="submit" value = "confirm">
 </form>
</div>

</body>
</head>
</html>


servlet文件registerData.java

[package kish.hibernate.servletController;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import kish.hibernate.DAO.MainController;

@WebServlet("/registerData")
public class registerData extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public registerData() {
        super();
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String first_name = request.getParameter("first_name");
        String last_name = request.getParameter("last_name");
        String branch = request.getParameter("branch");
        int Id = Integer.parseInt(request.getParameter("Id"));
        String password = request.getParameter("password");


        MainController mc = new MainController();
        mc.addUserDeatails(first_name, last_name, branch, Id, password);
        response.sendRedirect("success");

    }

}


如果需要,我将发布更多数据。

任何建议表示赞赏。
  谢谢。

编辑:

“ javax.persistence.jar”

它显然包含“ javax.persistence.PersistenceException.class”

添加了这个罐子,仍然显示相同的错误。

updated jar file in classpath

最佳答案

将您的jar放在WEB-INF / lib文件夹下。建立您的专案。

关于java - java.lang.ClassNotFoundException:javax.persistence.PersistenceException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51864229/

10-13 21:58