Hibernate批处理添加数据SQL

来源:互联网 发布:淘宝直播系统抽奖连击 编辑:程序博客网 时间:2024/05/21 04:22

Hibernate3执行SQL语句
当想通过hibernate进行批量增加记录的时候,或者到数据的时候会用到SQL语句。处理如下:
public Integer batchSave(final String sql){
  Session session = this.getSession();
  Transaction tx = session.beginTransaction();
  Integer result = -1;
  try {
   tx.begin();
   result = session.createSQLQuery(sql).executeUpdate();
   session.flush();
   tx.commit();
  } catch (DataAccessException e) {
   e.printStackTrace();
   if (tx != null) {
    tx.rollback();
   }
  } finally {
   session.close();
  }
  return result;
 }

原创粉丝点击