HTTP状态500 - 内部服务器错误

时间:2017-03-29 17:05:28

标签: spring hibernate maven spring-mvc

我正在尝试使用主键更新我的数据库行。但面对此异常

异常

HTTP Status 500 – Internal Server Error

Type Exception Report

Message Request processing failed; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:981)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:871)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

控制器

@RequestMapping(value="edit/product/success" ,method=RequestMethod.POST)
public ModelAndView editProduct(@ModelAttribute ("prdt") Product p)
{   
ModelAndView model=new ModelAndView("pl");
model.addObject("Update","Updated Successfully");
pd.update(p);
return model;       
}

Dao Impl

public void update( Product p) {
Session session=sessionFactory.openSession();
session.update(p);
session.flush();
session.close();
}

JSP

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ include file="header.jsp"%>
<%@ page isELIgnored="false"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<%@ page isELIgnored="false" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
  $( function() {
    $( "#mfg" ).datepicker();
  } );
  </script>
</head>
<body>
<br>

<h2 align="center">PRODUCT FORM</h2><hr>
<div class="col-md-2 "></div>
<div align="center"><div class="container"><div class="col-md-8 ">


<form:form method="POST" action="${pageContext.request.contextPath}/edit/product/success" commandName="prdt" enctype="multipart/form-data" >
<table class="table table-hover">

<tr>
<td> <form:label  path="product_Name"> Enter Product Name</form:label></td>
<td><form:input type="text" path="product_Name" class="form-control" value="${d.product_Name }"/></td>
</tr>
<tr>
<td> <form:label path="descripction"> Enter Product Descripction</form:label></td>
<td><form:input type="text" path="descripction"  class="form-control" value="${d.descripction }"/></td>
</tr>
<tr>
 <td> <form:label  path="price"> Enter Product Price</form:label></td>
<td><form:input type="text" path="price"  class="form-control" value="${d.price }" />
</td></tr>
<tr>
<td> <form:label  path="mfg_Date"> Enter Manufacture Date</form:label></td>
<td><form:input type="text" id="mfg" path="mfg_Date" class="form-control" value ="${d.mfg_Date}"/></td>

</tr>
<tr>
<td> <label> Choose Image</label></td>
<td><form:input type="file" path="image" class="form-control" /></td>
</tr>

</table>
 <input type="submit" class="btn btn-primary btn-block" value="Save Changes" class="form-control"/>

</form:form>

</div></div></div></body>
</html>

请检查并让我知道我的错误..我已经查看了它。但没有发现任何不规则。

注意:

数据检索工作正常。仅在更新时出现问题

2 个答案:

答案 0 :(得分:0)

检查您尝试保存到数据库的产品实体。 Hibernate尝试查找具有ID(主键值)的实体,如果在实体中更改了pk值并且在该实体上调用了更新,则会发生上述错误。

检查实体是否具有相同的ID,或者id被替换或在某处设置为null。在您的情况下,id可能为null。

答案 1 :(得分:0)

尝试使用这种方法:

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.update(p);
session.flush();
session.close();
tx.commit();

当hibernate无法找到需要更新的所有行时,通常会导致此错误。这意味着当您尝试更新从数据库中提取的某些对象时,它们不再存在(或者从一开始就不存在)。

可能是因为另一个线程正在删除它们,或者DB的隔离模式被设置为read_uncommited,因此由另一个事务创建的行无法保存(由于事务失败)并且没有&#39;再也不存在了。