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