使用Hibernate更新表

时间:2017-11-20 05:29:40

标签: java mysql hibernate

我正在使用Hibernate更新MySQL表中数据的项目下工作。每当我运行项目时,异常如下所示。

  

[批量更新从更新[0]返回意外的行数;实际   行数:0;预期:1]

控制器

@RequestMapping(value = "/disableEmployeeMaster", method = RequestMethod.POST)
public @ResponseBody void disableEmployee(HttpServletRequest request)
{
    EmployeeMaster employeeMaster = new EmployeeMaster();
    try
    {
        String employeeId = request.getParameter("employeeId");
        employeeMaster.setIsDel("Y");
        mainService.disableEmployee(employeeId , employeeMaster);
    }
    catch(Exception e)
    {
        logger.error(e.getMessage());
    }
}

服务实施

@Override
public void disableEmployee(String Id, EmployeeMaster employeeMaster) {
    Session session = null;
    Transaction transaction = null;
    try
    {
        session = sessionFactory.openSession();
        transaction = session.beginTransaction();
        session.update(Id, employeeMaster);
        transaction.commit();
    }
    catch(Exception e)
    {
        System.out.println(e.getMessage());
    }
    finally
    {
        session.close();
    }
}

1 个答案:

答案 0 :(得分:0)

您没有将employeeId设置为EmployeeMaster类对象。 更新任何实体都需要它的主键。 您可以参考以下代码:

employeeMaster.setEmployeeId(EMPLOYEEID);

<强>控制器

&#13;
&#13;
@RequestMapping(value = "/disableEmployeeMaster", method = RequestMethod.POST)
public @ResponseBody void disableEmployee(HttpServletRequest request)
{
    EmployeeMaster employeeMaster = new EmployeeMaster();
    try
    {
        String employeeId = request.getParameter("employeeId");
        employeeMaster.setEmployeeId(employeeId);
        employeeMaster.setIsDel("Y");
        mainService.disableEmployee(employeeId , employeeMaster);
    }
    catch(Exception e)
    {
        logger.error(e.getMessage());
    }
}
&#13;
&#13;
&#13;