Hibernate4 更新删除

来源:互联网 发布:淘宝女装货源 编辑:程序博客网 时间:2024/05/16 18:36
       /**        * HQL 更新或者删除        */       @Test       public  void updateOrDelete(){              Transaction tx=null;              Session session=null;              try{                     session=HibernateUtils.getSession();                     tx=session.beginTransaction();                     //String hql="update from Account set password='123456' where id=:id";//larger5:可以省略 from                     String hql="update Account set password='123456' where id=:id";                     Query query=session.createQuery(hql);                     query.setInteger("id", 1);                     int i=query.executeUpdate();                     System.out.println(i);                     if(i>0){                           System.out.println("成功");                     }else{                           System.out.println("失败");                     }                     tx.commit();              }catch(HibernateException he){                     if(tx!=null){                           tx.rollback();                     }                     he.printStackTrace();              }finally{                     HibernateUtils.closeSession(session);              }                     }       

原创粉丝点击