免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1402 | 回复: 0
打印 上一主题 下一主题

Hibernate批量更新与批理删除 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-09-26 16:04 |只看该作者 |倒序浏览
Hibernate批理更新与批量删除
批理修改:
场景:如有一个学生表Student,现有一属性[学院]改名,从"计算机学院"改为"计算机工程学院"[不考虑学院表].
用Hibernate实现这种批理更新的方法一DML(数据操作语言)操作。代码如下:
public void updateUser(String newName,String oldName) {
  Session session = null;
  try{
   session = this.getSession();
   Transaction tc = session.beginTransaction();
   
  String hqlUpdate = "update Student set deptName=:newName where     deptName= :oldName";   

int updatedEntities = s.createQuery( hqlUpdate )   
.setString( "newName", newName )   
.setString( "oldName", oldName )  
.executeUpdate();  
   
   tc.commit();
  }catch(RuntimeException re){
   log.debug(re.getMessage(),re);
   throw re;
  }finally{
   if (session != null){
    session.close();
   }
  }
}
方法二绕过Hibernate API,用JDBC实现。
public void UpdateUser(String newName,String oldName) {
  Session session = null;
  try{
   session = this.getSession();
   Transaction tc = session.beginTransaction();
   
   Connection connection = session.connection();
   
   PreparedStatement ps = connection.prepareStatement("update Student set deptName='"+newName+"' where  deptName= '"+oldName+"'");
   
   ps.execute();
   
   tc.commit();
   
  }catch(RuntimeException re){
   log.debug(re.getMessage(),re);
   throw re;
  }catch(SQLException e){
   log.debug(e.getMessage(),e);
  }finally{
   if (session != null){
    session.close();
   }
  }
}
批量删除
场景:如有一个学生表Student,现要删除学院为"技术学院"的学生.
用Hibernate实现这种批理删除的方法一DML(数据操作语言)操作。代码如下:
public void deleteUser(String deptName) {
  Session session = null;
  try{
   session = this.getSession();
   Transaction tc = session.beginTransaction();
   
  String hqlUpdate = "delete from Student where  deptName= :deptName";   
int updatedEntities = s.createQuery( hqlUpdate )   
.setString( "deptName", deptName)   
.executeUpdate();  
   
   tc.commit();
  }catch(RuntimeException re){
   log.debug(re.getMessage(),re);
   throw re;
  }finally{
   if (session != null){
    session.close();
   }
  }
}
方法二绕过Hibernate API,用JDBC实现。
public void deleteUser(String deptName) {
  Session session = null;
  try{
   session = this.getSession();
   Transaction tc = session.beginTransaction();
   
   Connection connection = session.connection();
   
   PreparedStatement ps = connection.prepareStatement("delete from Student where  deptName= '"+deptName+"'");
   
   ps.execute();
   
   tc.commit();
   
  }catch(RuntimeException re){
   log.debug(re.getMessage(),re);
   throw re;
  }catch(SQLException e){
   log.debug(e.getMessage(),e);
  }finally{
   if (session != null){
    session.close();
   }
  }
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/78176/showart_1226403.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP