What is the fastest way to update multiple rows of a table

I have a table with a name messagesthat contains columns toUser, messageand status. I want to update all message statuses by a specific user.

So, I wrote such a request,

Session s = DB.getSession();
s.createSQLQuery("UPDATE `message` SET `status`='0' WHERE `toUser`='3'").executeUpdate();
s.close();

But then I was told that updating using pure hibernate methods is faster and more efficient (I think it should do something with the sleep pool), as shown below.

Session s = DB.getSession();
Transaction tr = s.beginTransaction();
Criteria cr = s.createCriteria(Connection.Pojo.Message.class);
cr.add(Restrictions.eq("toUser", 3));
List<Connection.Pojo.Message> l = cr.list();
for (Connection.Pojo.Message msg : l) {
    msg.setStatus((byte) 0);
    s.update(msg);
}
tr.commit();
s.close();

So my question is the fastest way to update these lines? Please provide a detailed answer if possible.

Thanks in any advice.

+6
source share
1

, " Hibernate" HQL update:

Session s = DB.getSession();
s.createQuery("UPDATE message m SET m.status=:newStatus WHERE toUser=:userId")
    .setInteger("newStatus", 0)
    .setInteger("userId", 3)
    .executeUpdate();
s.close();

, SQL.

+3

All Articles