Hibernate bulk Delete in two ways

Source: Internet
Author: User

First: Encapsulate an object using the Hibernate mapping class

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

@Override
public boolean Deletetrainee (long[] id) {
try {
Session session = Hibernatesessionfactory.getsession ();
Transaction ts = session.begintransaction ();

Delete using Hibernate mapping class method
for (long Idlist:id) {
Trainee T = (Trainee) session.load (Trainee.class, idlist);
if (t! = null)
Session.delete (t);
}
Ts.commit ();
return true;
} catch (Exception e) {
E.printstacktrace ();
return false;
} finally {
Hibernatesessionfactory.closesession ();
}
}

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

The second type: using the HQL statement, stitching the string to set the placeholder way to operate

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

@Override
public boolean deletesupporttracking (long[] id) {
try {
Session session = Hibernatesessionfactory.getsession ();
Transaction ts = session.begintransaction ();

Set the number of placeholders
String sql = "";
for (int i = 0; i < id.length; i++) {
if (i = = 0) {
sql = SQL + "?";
} else {
sql = SQL + "," + "?";
}
}
Query query = Session.createquery ("DELETE from supporttracking WHERE ID in (" + SQL + ")");
To set parameters in a placeholder
long[] ints = new Long[id.length];
for (int i=0; i<id.length; i++) {
Ints[i] = Id[i];
Query.setparameter (i, ints[i]);
}
int k = Query.executeupdate ();
Ts.commit ();
return true;
} catch (Exception e) {
E.printstacktrace ();
return false;
} finally {
Hibernatesessionfactory.closesession ();
}
}

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

2.1 Online copy of the wording, not tested (first put this, have time to try again)

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

public void Deletes (list<integer> idlist) {
String hql = "";
for (int i = 0; i < idlist.size (); i++) {
if (i==0) {
HQL = "id=" +idlist.get (i);
} else {
HQL =hql + "or id=" +idlist.get (i);
}
Session session= this.getsession ();
Query q= session.createquery ("Delete from Timeliftinfo where" +hql);
Q.executeupdate ();
}

}

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

Hibernate bulk Delete in two ways

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.