gpt4 book ai didi

java - hibernate get id=0 批量更新从更新 [0] 返回了意外的行数;实际行数 : 0; expected: 1

转载 作者:太空宇宙 更新时间:2023-11-03 14:47:51 25 4
gpt4 key购买 nike

我在使用 hibernate + spring 进行编辑时遇到错误。我已经在搜索了。但仍然找不到解决方案。我编写 C R U D 代码,一切正常,如添加/删除,但在编辑/更新数据库时出现错误。

请问您能帮我找到解决方案吗?
谢谢。

这是我的代码:

模态/实体 EmployeStatus.java

@Entity
@Table
public class EmployeeStatus {

@Id
@Column
@GeneratedValue(strategy= GenerationType.AUTO)
private int id;

@Column
private String name;

public EmployeeStatus() {
}

public EmployeeStatus(int id, String name) {
super();
this.id = id;
this.name = name;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "EmployeeStatus [id=" + id + ", name=" + name + "]";
}

}

Dao impl EmployeeStatusDaoImpl.java

@Repository
public class EmployeeStatusDaoImpl implements EmployeeStatusDao {

private static final Logger logger = LoggerFactory.getLogger(EmployeeStatusDaoImpl.class);

@Autowired
private SessionFactory sessionFactory;

public EmployeeStatus findById(int id) {

return (EmployeeStatus)sessionFactory.getCurrentSession().get(EmployeeStatus.class, id);
}

@SuppressWarnings("unchecked")
public List<EmployeeStatus> findAll() {
Session session = this.sessionFactory.getCurrentSession();
List<EmployeeStatus> employeeList = session.createQuery("from EmployeeStatus").list();
return employeeList;
}


public void save(EmployeeStatus es) {
//sessionFactory.getCurrentSession().save(es);
Session session = this.sessionFactory.getCurrentSession();
session.persist(es);
}


public void update(EmployeeStatus es) {
//Session session = this.sessionFactory.getCurrentSession();
//session.update(es);
sessionFactory.getCurrentSession().update(es);
sessionFactory.getCurrentSession().flush();
//sessions.getCurrentSession().update(es);
logger.info("Employee Status updated successfully, Employee Status Details="+es);
}


public void delete(int id) {
sessionFactory.getCurrentSession().delete(findById(id));

}


}

服务实现EmployeeStatusServiceImpl.java

@Service
public class EmployeeStatusServiceImpl implements EmployeeStatusService{

@Autowired
private EmployeeStatusDao employeeStatusDao;

@Transactional
public EmployeeStatus findById(int id) {
return employeeStatusDao.findById(id);
}

@Transactional
public List<EmployeeStatus> findAll() {
return employeeStatusDao.findAll();
}

@Transactional
public void save(EmployeeStatus es) {
employeeStatusDao.save(es);

}

@Transactional
public void update(EmployeeStatus es) {
employeeStatusDao.update(es);

}

@Transactional
public void delete(int id) {
employeeStatusDao.delete(id);

}


}

Controller EmployeeStatusController.java

@Controller
public class EmployeeStatusController {

@Autowired
private EmployeeStatusService employeeStatusService;

@RequestMapping(value="employeeStatus", method = RequestMethod.GET)
public String list(Model model) {
model.addAttribute("empStat", new EmployeeStatus());
model.addAttribute("list", this.employeeStatusService.findAll());
return "empStat";
}



//For add
@RequestMapping(value= "/add", method = RequestMethod.POST)
public String addEmployeeStatus(@ModelAttribute("empStat") EmployeeStatus es){
//new employee status, call save
this.employeeStatusService.save(es);
return "redirect:/employeeStatus";

}

//for edit
@RequestMapping(value= "/update", method = RequestMethod.POST)
public String updateEmployeeStatus(@ModelAttribute("empStat") EmployeeStatus es){

//existing Employee status, call update function from service

employeeStatusService.update(es);
return "redirect:/employeeStatus";

}

@RequestMapping("/remove/{id}")
public String removeEmployeeStatus(@PathVariable("id") int id){

this.employeeStatusService.delete(id);
return "redirect:/employeeStatus";
}

//for call edit in list
@RequestMapping("/edit/{id}")
public String editEmployeeStatus(@PathVariable("id") int id, Model model){

model.addAttribute("list", this.employeeStatusService.findAll());
model.addAttribute("empStat", this.employeeStatusService.findById(id));
return "empStat";
}


}

jsp 员工状态.jsp

   ` <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>


<div id="page-wrapper">
<div class="container-fluid">

<!-- Page Heading -->
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Data Master</h1>
<ol class="breadcrumb">
<li><i class="fa fa-dashboard"></i> <a href="/">Dashboard</a>
</li>
<li class="active"><i class="fa fa-table"></i> Employee Status List</li>
</ol>
</div>
</div>
<!-- /.row -->
<div class="page-content">
<div class="col-md-9">
<div class="panel panel-green">
<div class="panel-heading">Employee Status</div>
<div class="panel-body">
<table class="table table-hover table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Operation</th>
</tr>
</thead>
<tbody>
<c:forEach items="${list}" var="empStatlist">
<tr>
<td><a><c:out value="${empStatlist.id}" /></a></td>
<td><a><c:out value="${empStatlist.name}" /></a></td>
<td align="center">
<a href="<c:url value='/edit/${empStatlist.id}' />">
<span class="glyphicon glyphicon-edit"></span>
</a>
   
<a href="<c:url value='/remove/${empStatlist.id}' />">
<span class="glyphicon glyphicon-trash"></span>
</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>

<br>
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">Add Employee Status</h3>
</div>
<div class="panel-body">
<c:url var="addAction" value="/add"></c:url>

<form:form action="${addAction}" commandName="empStat">
<table>
<tr>
<td><form:label path="name">
<spring:message text="Name" />
</form:label>
</td>
<td>
<form:input path="name" />
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="<spring:message text="Add"/>" />
</td>
</tr>
</table>
</form:form>
</div>
</div>

<br>
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">Edit Employee Status</h3>
</div>
<div class="panel-body">
<c:url var="editAction" value="/update"></c:url>

<form:form action="${editAction}" commandName="empStat">
<table>

<tr>
<td>
<form:label path="id" >
<spring:message text="ID" />
</form:label>
</td>
<td>
<!-- <form:input path="id" /> -->
<form:input path="id" readonly="true" size="8" disabled="true" />
</td>
</tr>

<tr>
<td><form:label path="name">
<spring:message text="Name" />
</form:label>
</td>
<td>
<form:input path="name" />
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="<spring:message text="Update"/>" />
</td>
</tr>
</table>
</form:form>
</div>
</div>
</div>
</div>
</div>
</div>

当我点击 simbol 编辑按钮时,我得到了数据,比如 id 和 name get id

但是当我点击编辑按钮时我得到了这个错误

15:48:18,574 DEBUG AnnotationTransactionAttributeSource:108 - Adding     transactional method 'EmployeeStatusServiceImpl.update' with attribute:     PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
Sat May 14 15:48:18 WIB 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
15:48:18,587 INFO EmployeeStatusDaoImpl:50 - Employee Status updated successfully, Employee Status Details=EmployeeStatus [id=0, name=Semi Permanent]
Hibernate:
update
EmployeeStatus
set
name=?
where
id=?
15:48:18,603 ERROR AbstractBatcher:73 - Exception executing batch:
org.hibernate.StaleStateException: Batch update returned unexpected row</code>

为什么id=0?

我该如何解决这个问题?

最佳答案

如果您的 jsp 生成的 HTML 在 id 输入中包含 disabled ,则您的答案是

Elements with Disabled attribute are not submitted or you can say their values are not posted.

更多信息 here

关于java - hibernate get id=0 批量更新从更新 [0] 返回了意外的行数;实际行数 : 0; expected: 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37225544/

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