Hibernate bulk Delete

Source: Internet
Author: User

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;

  1. The wording of a DELETE statement
  2. public void del (int[] selectflag) {
  3. The set of IDs is encapsulated in the//array;
  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 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;

  1. /**
  2. * Hibernate batch deletion;
  3. * Disadvantage: Deletion is a number of deletion statements, affecting 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 DAO layer;

DAO Layer:

    1. Public void del (list list) {
    2. this.gethibernatetemplate (). DELETEALL (list);
    3. }

This method emits multiple deletion statements, which can affect the efficiency greatly.

Hibernate bulk Delete

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.