gpt4 book ai didi

java - 使用 Hibernate 更新表

转载 作者:行者123 更新时间:2023-11-29 18:11:17 26 4
gpt4 key购买 nike

我正在一个项目中工作,该项目使用 Hibernate 更新 MySQL 表中的数据。每当我运行该项目时,都会出现如下异常。

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

服务实现

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

最佳答案

您尚未将employeeId 设置为EmployeeMaster 类对象。要更新任何实体都需要它的主键。您可以引用以下代码:

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

关于java - 使用 Hibernate 更新表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47385573/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com