yunye 的 JavaBlog

@see codemouse

统计

留言簿(1)

阅读排行榜

评论排行榜

求救Hibernate 问题,急,请帮我看下我的代码,好么!

 

我想在 JSP 页面中向数据库表 staff 插入一条记录:页面能跳转到 addStaffSuccess.jsp

刷新页面时,添加的记录能在页面中显示出来,我还以为成功了,于是我想通过企业管理器打开数据库表看是不是数据库表更新了,发现数据库表不显示数据,我插入的是第二条记录,于是我让它返回首行,首行正常显示。我用查询分析器查询 Staff 表的数据,一直是“正在执行批查询状态 ……… ”说明我对数据库的操作影响到了数据库,但是出了我不知道的异常或错误,但是又不报错,我不知道怎么去找原因。忘了,当我停止 tomcat 时,数据表正常显示,但插入的数据没有显示在表里,说明没有插入。难道是我没停止 tomcat 前,数据库一直在更新么?

我用的环境是, eclipse3.2+Myeclipse5.5 + MS SQL server2000+ tomcat5.5

我添加了 struts hibernate 支持,各个持久化类和 DAO , 是通过映射后 hibernate 自动生成的。 Action 代码是我写的,不足道有错没,请指正。

    添加后,返回原页面,显示了添加的信息:(但数据库没更新,郁闷!

 

数据库:

staff (staffid,// 主键

staffname,//not null

password,//not null

departmentname;// 允许空

sex;// 允许空

email;// 允许空

mobile;// 允许空 )

StaffDAO 里面:(这是 hibernate 映射数据库表后自动生成的方法)

     public void save(Staff transientInstance) {

       log .debug( "saving Staff instance" );

       try {

           getSession().save(transientInstance);

           log .debug( "save successful" );

       } catch ( RuntimeException re) {

           log .error( "save failed" , re);

           throw re;

       }

    }

AddStaffAction 里面:

   public ActionForward execute(ActionMapping mapping, ActionForm form,

           HttpServletRequest request, HttpServletResponse response) {

       // 获得表单信息;

       AddStaffForm addStaffForm = (AddStaffForm) form; // TODO Auto-generated method stub

       String staffid = addStaffForm.getStaffid();

       String staffname = addStaffForm.getStaffname();

       String departmentname = addStaffForm.getDepartmentname();

       String sex = addStaffForm.getSex();

       String password = new String( "000000" );

      

       // 转换字符格式

       try {

            staffid = new String(staffid.getBytes( "ISO-8859-1" ), "GB2312" );

            staffname = new String(staffname.getBytes( "ISO-8859-1" ), "GB2312" );

            departmentname = new String(departmentname.getBytes( "ISO-8859-1" ), "GB2312" );

            sex = new String(sex.getBytes( "ISO-8859-1" ), "GB2312" );

            password = new String(password.getBytes( "ISO-8859-1" ), "GB2312" );        

           } catch (UnsupportedEncodingException e) {

             // TODO Auto-generated catch block

              e.printStackTrace();

           }

           Staff staff = new Staff();

             staff.setStaffid(staffid);

             staff.setStaffname(staffname);

             staff.setPassword(password);

             staff.setDepartmentname(departmentname);

             staff.setSex(sex);                                                              

           try {

              StaffDAO dao = new StaffDAO();

                       dao.save(staff);

           } catch (Exception e) {

              // TODO Auto-generated catch block

              e.printStackTrace();           

           }

                 return mapping.findForward( "addStaffSuccess" );            }

posted on 2008-03-25 20:58 yunye 阅读(578) 评论(5)  编辑  收藏

评论

# re: 求救Hibernate 问题,急,请帮我看下我的代码,好么![未登录] 2008-03-25 21:58 flustar

你action里写那么多try...catch干什么
把你的日志记录贴出来,我帮你看看
很有可能是的你的Dao类写的有问题
顺便问一下:
“添加后,返回原页面,显示了添加的信息”
你这个显示了添加的信息是怎么得到的?  回复  更多评论   

# re: 求救Hibernate 问题,急,请帮我看下我的代码,好么! 2008-03-26 21:36 云野

字符转换的时候,不扑捉异常,它报错,以前在做JSP页面时,字符转换不用捕捉异常的,不知道是不是在Hibernate环境下的原因
进行数据库操作时,我看到书籍上都进行异常捕捉的,我就照做了。
在页面显示数据,我用了一个Action的
public class UserListAction extends Action {
/*
* Generated Methods
*/

/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
List stafflist = null;
try {
StaffDAO dao = new StaffDAO();

stafflist = (List)dao.findAll();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

request.setAttribute("staffall", stafflist);


return mapping.findForward("userlistmanage");
}
JSP页面:
<body>
<html:errors/>
<logic:present name="staffall" scope="request">

<table cellspacing="0" cellpadding="0" border="1" align="center">
<tbody>
<tr><td colspan="10" align="right"><html:link href="/FileManage/addStaffStep1.do">添加职员信息</html:link></td> </tr>
<tr><td colspan="10" align="center">职员信息</td> </tr>
<tr><td>职员号</td><td>姓名</td><td>部门</td><td>性别</td><td>电子邮件</td>
<td>地址</td><td>手机号码</td><td colspan="3">操作</td></tr>
<logic:iterate id="stafflist" name="staffall">
<tr>
<td> <bean:write name="stafflist" property="staffid" /> </td>
<td> <bean:write name="stafflist" property="staffname"/></td>
<td> <bean:write name="stafflist" property="departmentname"/></td>
<td> <bean:write name="stafflist" property="sex"/> </td>
<td> <bean:write name="stafflist" property="email"/></td>
<td> <bean:write name="stafflist" property="address"/></td>
<td> <bean:write name="stafflist" property="mobile"/></td>
<td><html:link href="/FileManage/lookFileList.do">文件目录</html:link></td>
<td><html:link href="/FileManage/modifyStaff.do">修改</html:link></td>
<TD><html:link href="/FileManage/deleteStaff.do">删除</html:link></TD>
</tr>
</logic:iterate>
</tbody></table>
</logic:present>
</body>


原来自动生成的DAO方法有点问题,我改动了一下,好象可以了:
public void save(Staff transientInstance) {
log.debug("saving Staff instance");
org.hibernate.Transaction tx = null;
tx=getSession().beginTransaction();
try {
getSession().save(transientInstance);
tx.commit();
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
if(tx!=null){
tx.rollback();
}
throw re;
}
}

  回复  更多评论   

# re: 求救Hibernate 问题,急,请帮我看下我的代码,好么! 2008-03-26 22:50 flustar

自己写个转换编码的Filter,在web.xml配置一下就行了, 就没有必要写那些转换的代码了。你看得是谁写的书,作者的水平似乎不怎么行啊,Action里直接调用DAO合适吗,还有在action里捕获异常,用try...catch也不合适,不是可以用异常声明吗,在xml里配置一下不就行了。  回复  更多评论   

# re: 求救Hibernate 问题,急,请帮我看下我的代码,好么! 2008-03-28 16:03 wjywilliam

问你一句,你的Transaction怎么处理的,处理了没有,在哪里?  回复  更多评论   

# re: 求救Hibernate 问题,急,请帮我看下我的代码,好么! 2008-03-30 23:53 Robin's Java World

问题应该就出在你的transaction上.  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: