日期:2014-05-20  浏览次数:20745 次

奇怪的问题,Hibernate的update操作,提示成功,数据库没变化..
大家帮帮忙..~

public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
int id = Integer.parseInt(request.getParameter("id"));
SelfProjectActionForm selfProjectActionForm=(SelfProjectActionForm)form;
String labadvice=new String(selfProjectActionForm.getLabadvice().getBytes("ISO-8859-1"),"GB2312");
String tutoradvice=new String(selfProjectActionForm.getTutoradvice().getBytes("ISO-8859-1"),"GB2312");
selfDao=this.getSelfProjectDAO();
Selfproject selfproject=selfDao.getSelfProjectByID(id);
selfproject.setTutoradvice(tutoradvice);
selfproject.setLabadvice(labadvice);
try{
if(!selfDao.updateSelfProject(selfproject)){
return mapping.findForward("failure");
}else{
return mapping.findForward("success");  

}catch(HibernateException e){
e.printStackTrace();
return mapping.findForward("failure");
}
}


  其中呢 selfproject是有20个字段的,先前的A页面先插入15个字段,当时未插入的为空,
然后到现在的B页面,显示那15个字段,并提供另外2个字段的输入框,即Tutoradvice和Labadvice..
然后提交到 贴出代码的 Action里, 
可以执行到else{
return mapping.findForward("success");

  也就是说selfDao.updateSelfProject(selfproject)是已经成功执行了

 但是 数据库中 tutoradvice 和 labadvice仍然为空.

 不知道是怎么回事呢?

 好像是没有分数了 不好意思啊 麻烦大家给看看啊 拜托了......

------解决方案--------------------
首先,最基本的 看一下你的程序连接数据库和你自己查看的数据库是不是一个数据库(还真别小看这个问题)

如果不是上述,那么用调试看当你执行updateSelfProject方法时,selfproject的数据状态,在该对象中是否有两个值

如果对象的值也没问题,看一下你updateSelfProject()方法执行时,最后有没有提交数据库,也就是有没有commit();有时候如果你吧事务设置了手动提交,但是有没有commit数据库是不会有值的。

我才疏学浅,只能想到这么多了
------解决方案--------------------
public class TeacherTest {
@Test
public void update(){
Session session = HibernateUitl.getSessionFactory().getCurrentSession();
session.beginTransaction();
Teacher t = (Teacher) session.get(Teacher.class, 3);
t.setName("yangtb2");
session.update(t);

session.getTransaction().commit();
}
}
------解决方案--------------------
这样试试update方法呢