hibernate對數(shù)據(jù)庫進(jìn)行操作。以刪除為例我的操作步驟是:網(wǎng)頁傳到服務(wù)器要刪除的id----sturts文件對請求分配action方法----action內(nèi)將id存在實(shí)體類里,然后將類傳給delete方法----delete方法成功后傳回action----action在返回sturts配置文件內(nèi)尋找結(jié)果----返回網(wǎng)頁現(xiàn)在我想在action內(nèi)寫一個判斷,以確定delete方法確實(shí)成功執(zhí)行了我想要結(jié)果。確實(shí)刪除了我想刪除的語句該怎么寫?這是我寫的代碼。但是我發(fā)現(xiàn)這樣不行。action部分代碼``` public String execute() throws Exception { // TODO 自動生成的方法存根 HibernateModel hm=new HibernateModel(); User u=new User(); u.setId(getId()); boolean i=hm.delete(u); if(i==true) { return SUCCESS; }else { return ERROR; } }```delete方法```//刪除 public boolean delete(User u)throws Exception{ try { tx =session.beginTransaction();//開始 //持久化操作 session.delete(u); tx.commit();//提交 }catch (Exception e) { if(tx!=null) { tx.rollback();//回滾 } e.printStackTrace(); }finally { session.close();//關(guān)閉 } return true; }```如果我刪除一個根本不存在的id,他還是會刪除成功。數(shù)據(jù)庫根本沒動。
添加回答
舉報
0/150
提交
取消