zoukankan      html  css  js  c++  java
  • hibernate批量删除和更新数据

    转载自:http://blog.csdn.net/yuhua3272004/article/details/2909538

    Hibernate3.0 採用新的基于ANTLR的HQL/SQL查询翻译器,在Hibernate的配置文件里,hibernate.query.factory_class属性用来选择查询翻译器。
    (1)选择Hibernate3.0的查询翻译器:
    hibernate.query.factory_class= org.hibernate.hql.ast.ASTQueryTranslatorFactory
    (2)选择Hibernate2.1的查询翻译器
    hibernate.query.factory_class= org.hibernate.hql.classic.ClassicQueryTranslatorFactory
    为了使用3.0的批量更新和删除功能,仅仅能选择(1)否则不能解释批量更新的语句。选择(2)但没法解释批量更新语句了。

    大批量更新/删除(Bulk update/delete)

    就像已经讨论的那样,自己主动和透明的 对象/关系 映射(object/relational mapping)关注于管理对象的状态。 这就意味着对象的状态存在于内存,因此直接更新或者删除 (使用 SQL 语句 UPDATE 和 DELETE) 数据库中的数据将不会影响内存中的对象状态和对象数据。 只是,Hibernate提供通过Hibernate查询语言来运行大批 量SQL风格的(UPDATE)和(DELETE) 语句的方法。

    UPDATE 和 DELETE语句的语法为: ( UPDATE | DELETE ) FROM? ClassName (WHERE WHERE_CONDITIONS)?。 有几点说明:

    在FROM子句(from-clause)中,FROMkeyword是可选的

    在FROM子句(from-clause)中仅仅能有一个类名,而且它不能有别名

    不能在大批量HQL语句中使用连接(显式或者隐式的都不行)。只是在WHERE子句中能够使用子查询。

    整个WHERE子句是可选的。

    举个样例,使用Query.executeUpdate()方法运行一个HQL UPDATE语句: 

    Session session = sessionFactory.openSession();

    Transaction tx = session.beginTransaction();

    String hqlUpdate = "update Customer set name = :newName where name = :oldName";

    int updatedEntities = s.createQuery( hqlUpdate ) .setString( "newName", newName ) .setString( "oldName", oldName ) .executeUpdate(); tx.commit();

    session.close();


    运行一个HQL DELETE,相同使用 Query.executeUpdate() 方法 (此方法是为 那些熟悉JDBC PreparedStatement.executeUpdate() 的人们而设定的)

    Session session = sessionFactory.openSession();

    Transaction tx = session.beginTransaction();

    String hqlDelete = "delete Customer where name = :oldName";

    int deletedEntities = s.createQuery( hqlDelete ) .setString( "oldName", oldName ) .executeUpdate();

    tx.commit();

     session.close();


    由Query.executeUpdate()方法返回的整型值表明了受此操作影响的记录数量。 注意这个数值可能与数据库中被(最后一条SQL语句)影响了的“行”数有关,也可能没有。一个大批量HQL操作可能导致多条实际的SQL语句被运行, 举个样例,对joined-subclass映射方式的类进行的此类操作。这个返回值代表了实际被语句影响了的记录数量。在那个joined-subclass的样例中, 对一个子类的删除实际上可能不只会删除子类映射到的表并且会影响“根”表,还有可能影响与之有继承关系的joined-subclass映射方式的子类的表。

    ------------------------------------------------------------------------------------------------

    我在 spring + hibernate 中 使用

    String sql = "delete PlanPackageRelations  where ppfId = "+ppfId;
    int a = this.getHibernateTemplate().getSessionFactory().openSession().createQuery(sql).executeUpdate();
    结果控制台输出一下信息: 

    在本地事务包括边界中使用的资源 jdbc/cnas 的可分享连接 MCWrapper id 19911991  Managed connectioncom.ibm.ws.rsadapter.spi.WSRdbManagedConnectionImpl@12781278 State:STATE_TRAN_WRAPPER_INUSE

    -------------------------------------------------------------------------------------------------

    调用jdbc 处理依据非主键删除。

    /**
      * 依据ppfId ,删除PlanPackageRelations。
      * @param ppfId
      */
     public  void deletePlanPackageRelations(String ppfId){
         final String ppfIdFinal = ppfId;
         try {
         
             this.getHibernateTemplate().execute(new HibernateCallback(){
                   public Object doInHibernate(Session session) throws HibernateException, SQLException {
                    List result = new ArrayList();
                    String sql = "delete PlanPackageRelations  where ppfId = :ppfId";
                    Query query = session.createQuery(sql).setString("ppfId",ppfIdFinal);
                    result.add(new Integer(query.executeUpdate()));
                    return result;
                   }
                   
             });
             
    //         String sql = "delete PlanPackageRelations  where ppfId = "+ppfId;
    //         int a = this.getHibernateTemplate().getSessionFactory().openSession().createQuery(sql).executeUpdate();
    //         
             
            }catch(DataAccessException t){
       t.printStackTrace();
       throw t;
      }catch (Exception e) {
          e.printStackTrace();   
            }
     }

     --------------------------------------------------------------------------------------------

    使用HibernateTemplate批量删除数据

      使用spring + hibernate框架中,一般使用hibernateTemplate来使用Hibernate,但hibernateTemplate
    的 bulkUpdate()不能实现动态的批量删除,即使用bulkUplate时要事先确定下占位符”?“的个数,然后再使用其重载方法 bulkUpdate(queryString, Object[]),此时,Object[]内的元素个数就要跟queryString中的占位符“?”的个数相等,使用十分麻烦,因此能够使用 HibernateCallback回调函数来进行动态批量删除,即能够不考虑要删除元素的个数。详细用法例如以下例:
        public void bulkDelete(final Object[] ids) throws Exception {
            final String queryString = "delete PersistentModel where id in (:ids) ";
            super.execute(new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException, SQLException {
                    Query query = session.createQuery(queryString);
                    query.setParameterList("ids", ids);
                    return query.executeUpdate();
                }
            });
        }
    注:标红处的占位符要加上(),否则会抛出语法错误异常。

    -----------------------------------------------------------------------------------------

    bulkUpdate 使用:

    String updateSql = "update Rsceref ref set ref.rulecode = ? where ref.rscerefcode = ?";
    getHibernateTemplate().bulkUpdate(updateSql, new Object[]{chkObj.getBmcrcode(),listExistRuleSql.get(0)}); 

    this.getHibernateTemplate().bulkUpdate(sql);


  • 相关阅读:
    Numpy学习笔记练习代码 ——(二)
    Requests爬取表格数据并存入CSV中
    Numpy学习练习代码 ——(一)
    Requests爬取中文网站乱码问题
    Pycharm用Ctrl+鼠标滚轮控制字体大小
    一、Windows10下python3和python2同时安装
    inux下配置rsyncd服务
    shell 脚本中$$,$#,$?分别代表什么意思?
    linux shell awk 流程控制语句(if,for,while,do)详细介绍
    定时任务
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4552015.html
Copyright © 2011-2022 走看看