我有錯誤編輯與休眠+春天。我已經在這裏搜索。但仍然無法找到解決方案。我寫C R U D代碼,所有的工作都很好,比如添加/刪除,但是當我編輯/更新數據庫時出現錯誤。hibernate get id = 0批量更新從update [0]返回意外的行數;實際行數:0;預計:1
請問你能幫我找到解決辦法嗎?
謝謝。
這裏是我的代碼:
莫代爾/實體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 + "]";
}
}
道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));
}
}
服務IMPL 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);
}
}
控制器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 employeeStatus.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>
當我打辛博爾編輯按鈕,我得到的數據,如ID和名稱 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爲什麼?
我該如何解決這個問題?
噢,我的壞。謝謝你的解決方案先生。所以我只需要將禁用屬性更改爲只讀。再次感謝 – adi