Hibernate和Eclipse错误

时间:2018-06-28 16:05:21

标签: eclipse hibernate tomcat

我正在使用注册表单创建一个简单的Web应用程序,并且我使用了Hibernate。但是我有这种错误:

这是代码: AddUser.java

import java.io.IOException;

import javax.imageio.spi.ServiceRegistry;
import javax.security.auth.login.Configuration;
import javax.servlet.RequestDispatcher;
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 org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;


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


    public AddUser() {
        super();
        // TODO Auto-generated constructor stub
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Configuration config = new Configuration().configure("hibernate.cfg.xml");
        ServiceRegistry servReg = new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();
        SessionFactory factory = config.buildSessionFactory(servReg);

        Session session = factory.openSession();
        session.beginTransaction();
        User u = new User(request.getParameter("firstname"), request.getParameter("lastname"), request.getParameter("login"), request.getParameter("password"));
        session.save(u);
        session.getTransaction().commit();
        session.close();

        RequestDispatcher view = request.getRequestDispatcher("useradd.jsp");
        view.forward(request, response);

    }

}

这是错误:ERROR 如何解决这个问题?谢谢!

@LuisMuñoz

如果有人想直接看到它,我还会添加错误。

HTTP Status 500 – Internal Server Error

Type Exception Report

Message Servlet execution threw an exception

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

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

java.lang.Error: Unresolved compilation problems: 
    Cannot instantiate the type Configuration
    The method configure() is undefined for the type Configuration
    The method getProperties() is undefined for the type Configuration
    The method buildSessionFactory(ServiceRegistry) is undefined for the type Configuration

    AddUser.doPost(AddUser.java:32)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Note The full stack trace of the root cause is available in the server logs.

我的Eclipse页面: ECLIPSE

1 个答案:

答案 0 :(得分:-1)

从该错误看来,没有为Configuration对象配置方法。检查javadocs https://docs.oracle.com/javase/9/docs/api/index.html?javax/security/auth/login/Configuration.html