Turn from: Hibernate bulk Delete
In general, there are two types of batch deletions in Hibernate, one of which is Hibernate's built-in bulk deletion, but his bulk deletion is the deletion of each record by one generation
statement, its efficiency is very low, of course, we can use the crawl strategy to optimize it, but this is only a way to mend, the efficiency of the upgrade still can not satisfy us, is not recommended to use;
The other is a HQL statement formed by "Scrabble", which can form a statement, thus maximizing the efficiency.
Let's start with the word "Scrabble":
Personal use is SSH, so use the spring template, if you use hibernate alone, please increase the transaction and close the session;
- The wording of a DELETE statement
- public void del (int[] selectflag) {
- The set of IDs is encapsulated in the//array;
- String hql = "";
- For (int i=0;i<selectflag.length;i++) {
- if (i==0) {
- HQL = "id=" +selectflag[i];
- } Else {
- HQL =hql + "or id=" +selectflag[i];
- }
- }
- Session session= this.getsession ();
- Query q= session.createquery ("Delete from User where" +hql);
- Q.executeupdate ();
- }
This writing will form a HQL statement, get the maximum ascension;
Then we say that hibernate is built in bulk delete:
Call the DAO layer and pass the container;
- /**
- * Hibernate batch deletion;
- * Disadvantage: Deletion is a number of deletion statements, affecting efficiency;
- */
- List List = new ArrayList ();
- For (int i=0;i<selectflag.length;i++) {
- User u= New user ();
- U.setid (Selectflag[i]);
- List.add (U);
- }
- Dao.del (list);
- //Call the Delete method of DAO layer;
DAO Layer:
- Public void del (list list) {
- this.gethibernatetemplate (). DELETEALL (list);
- }
This method emits multiple deletion statements, which can affect the efficiency greatly.
Hibernate bulk Delete