I have such a requirement.
protected Integer[] updateFullTable(final Class clazz){
final ProjectionList projectionList=Projections.projectionList().add(Projections.property("id"),"id");
final Criteria criteria=session.createCriteria(clazz)
.add(Restrictions.eq("typeOfOperation",1))
.add(Restrictions.eq("performUpdate",true));
criteria.setProjection(projectionList);
final List idsList=criteria.list();
final Integer[]ids = transformObjectArrayIntoIntegerArray(idList);
final Query query=session.createQuery("update "+clazz.getName()+" set activeRegister=true and updateTime=:updateTime where id in (:ids)")
.setParameter("updateTime",new Date())
.setParameterList("ids",ids);
query.executeUpdate();
return transform;
}
As you guys can see that I need to update all the rows in the table, once I request all the row identifiers, and then apply the update to these identifiers in a separate query, but there are a lot of records in the tables, sometimes it takes between 30 seconds to 10 minutes depends on the table .
I changed this code to only one update like this.
final Query query=session.createQuery("update "+clazz.getName()+" set activeRegister=true and updateTime=:updateTime where typeOfOperation=1 and performUpdate=true");
And with this single request, I avoid the first request, but I can no longer return the affected identifiers. But later change a was required
final StringBuilder logRevert;
Added option.
To store updated identifiers for applying direct reverse update to the database, if necessary.
. , ids - , , .
.
- HQL.
- namedQuery
- SqlQuery
- , []
- .
-
query.executeUpdate();
......
, .
UPDATE
@dmitry-senkovich rawSQL, , .
https://stackoverflow.com/questions/44641851/java-hibernate-org-hibernate-exception-sqlgrammarexception-could-not-extract-re