先代码:
Action: public String updateSubmit() throws Exception{
String n = "1";
try{
entityService.update(filterProductId,n);
return RELOAD;
}catch (Exception e) {
e.printStackTrace();
return ERROR;
}
}Service: public void update(String pId,String n){
entityDao.update(pId,n);

}Dao: public void update(String pId,String n){
try{
String hql ="update Product p set resv1 = '"+ n +"' where p.productId = ?";
getHibernateTemplate().bulkUpdate(hql, pId);
getHibernateTemplate().flush();
}catch (Exception e) {
e.printStackTrace();
}
}Jsp页面:<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>产品明细</title>
    <script type="text/javascript">
    
     function back(){
history.go(-1);
}

function findByIdFrom(form,id,typeId){
document.getElementById('filterProductId').value=id;
document.getElementById('filterProductTypeId').value=typeId;
var url='<s:url action="updateSubmit" namespace="/product"/>';
form.action = url;
form.submit();
}

function findByIdresv(form,id,resv,a){
document.getElementById('filterProductId').value=id;
document.getElementById('filterResv').value=resv;
document.getElementById('a').value=a

var proId = "";
var proIds = "";
var proName = "";
var productIdObj = document.getElementsByName('productId');
var proNameObj = document.getElementsByName('proName');
for(var i = 0; i < productIdObj.length; i++){
proIds = productIdObj[i].value + ",";
proName = proNameObj[i].value; + ",";
proId += proIds + proName + ";";
}
var url='<s:url action="toAdd" namespace="/sellorder"/>';
document.getElementById('proId').value = proId;
form.action = url;
form.submit();
}










function showfileter(){
var field = document.getElementById("fieldDiv").style.display;
if(field == "none"){
document.getElementById("fieldDiv").style.display="block";
}else{
document.getElementById("fieldDiv").style.display="none";
}
}


</script>

  </head>
  
  <body>
  <!--<button onclick="back();">返回</button>
  --><s:form action="updateSubmit" namespace="/quote" method="post">
  <s:hidden name="filterProductId" id="filterProductId" value=""/>
  <s:hidden name="filterProductTypeId" id="filterProductTypeId" value=""/>
  <s:hidden name="filterResv" id="filterResv" value=""/>
  <s:hidden id="a" name="a" value=""/>
  <s:hidden id="proId" name="proId" value=""/>
  <s:token/>
  
    <table align="center" border="0px">
    
   <tr>
   <td><s:text name="产品名称"/>&nbsp;&nbsp;&nbsp;&nbsp;</td>
   <td><s:text name="成本价(元)"/>&nbsp;&nbsp;&nbsp;&nbsp;</td>
   <td><s:text name="核算价(元)"/>&nbsp;&nbsp;&nbsp;&nbsp;</td>
   <td><s:text name="周期"/>&nbsp;&nbsp;&nbsp;&nbsp;</td>
   <td><s:text name="库存"/>&nbsp;&nbsp;&nbsp;&nbsp;</td>
   <td>&nbsp;<s:text name="操作"></s:text></td>
   </tr>
   <s:if test="listpro.size()>0">
   <s:iterator value="listpro">
   <tr>
   <td><s:property value="productName"/>&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td><s:property value="productCost" />&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td><s:property value="productPrice" />&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td><s:property value="productCycle" />&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td><s:property value="productStock" />&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td>
<a href="javascript:findByIdFrom(document.forms[0],'${productId}','${productTypeId}');">添加</a>
<!--<a href="javascript:findByIdresv(document.forms[0],'${productId}','${resv1}','${a}');">确定</a>
--></td>
   </tr>
   </s:iterator>
  
   </s:if>
   <s:else>
   <tr>
   <td colspan="6" align="center"><h4>暂无相关数据</h4></td>
   </tr>
   </s:else>
  </table>
  
  <br/><br/><br/><br/><br/><br/><br/><br/>
  <hr/>
  <div style="display:block" id="fieldDiv">
  <table align="center" border="0px">
   <tr>
   <td align="center" colspan="5"><h3>以下是你选择的产品</h3></td>
   </tr>
   <tr>
   <td><s:text name="产品名称"/>&nbsp;&nbsp;&nbsp;&nbsp;</td>
   <td><s:text name="成本价(元)"/>&nbsp;&nbsp;&nbsp;&nbsp;</td>
   <td><s:text name="核算价(元)"/>&nbsp;&nbsp;&nbsp;&nbsp;</td>
   <td><s:text name="周期"/>&nbsp;&nbsp;&nbsp;&nbsp;</td>
   <td><s:text name="库存"/>&nbsp;&nbsp;&nbsp;&nbsp;</td>
   </tr>
   <s:if test="list.size()>0">
   <s:iterator value="list" status="sta">
    <tr>
   <td>
   <input type="hidden" id="productId" name="productId" value="${productId}"/>
   <input type="hidden" id="proName" name="proName" value="${productName}"/>
   </td>
   </tr>
  
   <tr>
   <td>${productName}</td>
   <td>${productCost}</td>
   <td>${productPrice}</td>
   <td>${productCycle}</td>
   <td>${productStock}</td>
   </tr>
   </s:iterator>
   <tr>
   <td colspan="5" align="right"><a href="javascript:findByIdresv(document.forms[0],'${filterProductId}','${productresv}','${a}');">确定</a></td>
   </tr>
   </s:if>
   <s:else>
   <tr>
   <td colspan="5" align="center">
   <h4>你还没有选择任何产品</h4>
   </td>
   </tr>
   </s:else>
  </table>
  </div>
  </s:form>
  </body>
</html>功能描述:在这个页面分为上下两部分。 类似购物车   上面是产品列表   产品信息有个添加  我点击一下 调用 updateSubmit 修改起产品的一个字段 作为标识   点击一条产品添加没有问题 。 但是当我在点击另外产品(根据产品的ID,也就是修改标识位)添加的时候 就报错 。以下是错误信息 : 希望高手能帮忙解决下。  我每完成一个功能模块都备份。 在上一份中这一部分没有任何问题。org.springframework.orm.hibernate3.HibernateSystemException: identifier of an instance of com.shhelian.app.model.Product was altered from 2 to 3; nested exception is org.hibernate.HibernateException: identifier of an instance of com.shhelian.app.model.Product was altered from 2 to 3 at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:676)
at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.bulkUpdate(HibernateTemplate.java:1150)
at org.springframework.orm.hibernate3.HibernateTemplate.bulkUpdate(HibernateTemplate.java:1146)
at com.shhelian.app.dao.ProductDao.update(ProductDao.java:94)
at com.shhelian.app.service.chance.ProductService.update(ProductService.java:87)
at com.shhelian.app.service.chance.ProductService$$FastClassByCGLIB$$efa6aff5.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(
错误信息大概就是这些。 希望高手能帮忙解决下

解决方案 »

  1.   

    检查一下你的主键属性是否封装  例如:Integer,Long ……。不能是int,long等数据类型。
      

  2.   


    我是用MyEclpise自动生成的实体类   我点击产品第一次可以  。 其实我这个添加按钮 相当与我点击了  根据这个产品的ID修改其表中的一个字段 。    第一个次修改成功 。 但是第二次当我点击另外一条产品的时候就会报上面我发的错误 。 修改不了 。
      

  3.   


    我这是根据Id修改  不是添加 。  根据产品的ID修改其 数据库中的一个字段(标识位), 第一次可以修改成功。 但当我点击另外一条产品 时 。  就报上面的错误 。   找不到原因 。  
      

  4.   

    identifier of an instance of com.shhelian.app.model.Product was altered from 2 to 3;
    意思是你不能对实体product中的主键从2修改到3
      

  5.   

    如果是使用hibernate注释的话,检查一下你的action中有没有添加@Scope("prototype")这个注释,如果是基本xml配置的话,就检查一下你的action中的代码是不是先修改了对象,还没有关闭session的时候后又修改这个数据的主键id,但是修改的这个主键id已经在数据库已经存在。
      

  6.   

    修改一个实体怎么会去出触碰他的id呢,这个逻辑上有点不正确吧。
    我个人见解:先查询出这个实体,然后将这个除了ID以外的值重新set一次,set的值保证要么和以前一样,要么是你获取到的修改值,不然就是空的。然后再通过update实体,这样就能更新了!