Python中文网

一个关于 编程问题的解答网站.

有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法使用hibernate更新MySQL中的行

public class UpdateEmployee extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        PrintWriter out= response.getWriter();
        EmployeeDetails emp = new EmployeeDetails();
        SessionFactory fact=new Configuration().configure().buildSessionFactory();
        Session ses = fact.openSession();
        int emp_id = Integer.parseInt(request.getParameter("emp_id"));
        int new_emp_id = Integer.parseInt(request.getParameter("new_emp_id"));
        String emp_name = request.getParameter("name");
        String emp_designation_id = request.getParameter("designationid");
        String emp_skills = request.getParameter("skills");
        String emp_department_id = request.getParameter("departmentid");
        String emp_notes = request.getParameter("notes");
        String emp_email = request.getParameter("email");
        String emp_phone = request.getParameter("phone");
        String emp_username = request.getParameter("username");
        String emp_password = request.getParameter("password");
        emp.setEmp_department_id(emp_department_id);
        emp.setEmp_designation_id(emp_designation_id);
        emp.setEmp_email(emp_email);
        emp.setEmp_id(new_emp_id);
        emp.setEmp_join_date(null);
        emp.setEmp_name(emp_name);
        emp.setEmp_notes(emp_notes);
        emp.setEmp_password(emp_password);
        emp.setEmp_phone(emp_phone);
        emp.setEmp_skills(emp_skills);
        emp.setEmp_username(emp_username);
        Query query = ses.createQuery("update EmployeeDetails emp set emp_id=:new_emp_id,emp_name=:emp_name,emp_designation_id=:emp_designation_id,emp_skills=:emp_skills,emp_department_id=:emp_department_id,emp_notes=:emp_notes,emp_email=:emp_email,emp_phone=:emp_phone,emp_username=:emp_username,emp_password=:emp_password where emp_id=:emp_id");
        query.setParameter(emp_id, emp_id);
        query.setParameter(new_emp_id, new_emp_id);
        query.setParameter(emp_name, emp_name);
        query.setParameter(emp_designation_id, emp_designation_id);
        query.setParameter(emp_skills, emp_skills);
        query.setParameter(emp_department_id, emp_department_id);
        query.setParameter(emp_notes, emp_notes);
        query.setParameter(emp_email, emp_email);
        query.setParameter(emp_username, emp_username);
        query.setParameter(emp_password, emp_password);
        int result = query.executeUpdate();
        out.print(result);
    }

}

我尝试更新时的结果:

java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-based!
    org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterDescriptor(ParameterMetadata.java:55)
    org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterExpectedType(ParameterMetadata.java:61)
    org.hibernate.impl.AbstractQueryImpl.determineType(AbstractQueryImpl.java:382)
    org.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:362)
    com.unisys.servlets.UpdateEmployee.doPost(UpdateEmployee.java:59)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

我刚开始冬眠。我已经查阅了很多参考资料,任何关于更新的指导都将不胜感激。 我试图使用emp_id value更新行中的一个值,并更新数据库中的所有其他列


共 (0) 个答案