hibernate對數據庫進行操作。以刪除為例我的操作步驟是:網頁傳到服務器要刪除的id----sturts文件對請求分配action方法----action內將id存在實體類里,然后將類傳給delete方法----delete方法成功后傳回action----action在返回sturts配置文件內尋找結果----返回網頁現在我想在action內寫一個判斷,以確定delete方法確實成功執行了我想要結果。確實刪除了我想刪除的語句該怎么寫?這是我寫的代碼。但是我發現這樣不行。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();//關閉 } return true; }```如果我刪除一個根本不存在的id,他還是會刪除成功。數據庫根本沒動。
添加回答
舉報
0/150
提交
取消