简体   繁体   English

使用Hibernate更新表

[英]Update table using Hibernate

I am working under a project that is update the data's in MySQL table using Hibernate. 我正在使用Hibernate更新MySQL表中数据的项目下。 Whenever I run the project, the exception is shown as below. 每当我运行项目时,异常都会显示如下。

[Batch update returned unexpected row count from update [0]; [批处理更新从更新[0]返回了意外的行数; actual row count: 0; 实际行数:0; expected: 1] 预期:1]

Controller 控制者

@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());
    }
}

Service Implementation 服务实施

@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();
    }
}

You have't set employeeId to EmployeeMaster class object. 您尚未将employeeId设置为EmployeeMaster类对象。 to update any entity needs it's primary key. 更新任何实体都需要它的主键。 You can refer following code : 您可以参考以下代码:

employeeMaster.setEmployeeId(employeeId); employeeMaster.setEmployeeId(employeeId);

Controller 控制者

 @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()); } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM