batch delete in hibernate

From: hibernate's batch delete
Generally speaking, there are two ways to write hibernate's batch delete, one is the built-in batch delete of hibernate, but his batch delete is to generate delete
statements for each record one by one, which is extremely inefficient, of course We can use the
grabbing strategy to optimize it, but this is just a way to remedy the situation. The efficiency improvement is still not satisfactory to us, and it is not recommended to use it; Statement, which is the biggest improvement in efficiency;

let's talk about the "split string" form of writing:
 personal use is ssh, so the spring template is used, if hibernate is used alone, please increase the transaction and close the session;
   

  1. //Writing a delete statement
  2.     public void del(int[] selectFlag) {
  3.     //The array is encapsulated in a collection of IDs;
  4.         String hql = "";
  5.         for(int i=0;i<selectFlag.length;i++) {
  6.             if(i==0) {
  7.                 hql = "id="+selectFlag[i];
  8.             } else {
  9.                 hql =hql + " or id="+selectFlag[i];
  10.             }
  11.         }   
  12.         Session session= this.getSession();
  13.         Query q= session.createQuery("delete from User where "+hql);
  14.         q.executeUpdate();
  15.     }

    This writing method will form an HQL statement and get the biggest improvement;


then we say that the built-in batch deletion of hibernate:

     call the dao layer, and pass the container;

  1.         /**
  2.          * batch delete of hibernate;
  3.          * Disadvantage: There are multiple delete statements when deleting, which affects efficiency;
  4.          */
  5.         List list = new ArrayList();
  6.         for(int i=0;i<selectFlag.length;i++){
  7.             User u= new User();
  8.             u.setId(selectFlag[i]);
  9.             list.add(u);    
  10.         }
  11.         dao.del(list);
  12.         //Call the delete method of the DAO layer;

    DAO layers:

  1. public void del(List list) {
  2.         this.getHibernateTemplate().deleteAll(list);
  3.     }

    This method will issue multiple delete statements, which greatly affects the efficiency;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324938111&siteId=291194637