0

I am working under a project that is update the data's in MySQL table using Hibernate. Whenever I run the project, the exception is shown as below.

[Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 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();
    }
}
  • session.update() method parameter is an object of the model. if you have PK it will automatically update the row. – Yogesh H Shenoy Nov 20 '17 at 05:39
  • Have a look on this https://stackoverflow.com/questions/21124361/hibernate-delete-error-batch-update-returned-unexpected-row-count – Anptk Nov 20 '17 at 06:32
  • According to the specification `void update(String entityName, Object object)`, I suppose the `Id` `"E02"` shouldn't be the `entityName`. Can you show us your `EmployeeMaster` entity class? – Roger Ng Nov 20 '17 at 06:36
  • "E02" isn't entity, It's an employee ID. "employeeMaster " is Entity. –  Nov 20 '17 at 06:49
  • what's your primary in `EmployeeMaster`? the one that has `@Id` field? – msagala25 Nov 20 '17 at 06:54
  • `employeeId` is primary of my `EmployeeMaster`. –  Nov 20 '17 at 07:12

1 Answers1

0

You have't set employeeId to EmployeeMaster class object. to update any entity needs it's primary key. You can refer following code :

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());
    }
}
Ramesh Fadatare
  • 561
  • 4
  • 12