Bulk updating rows using hibernate Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Bulk updating rows using hibernate without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am using hibernate 4.3.10. According to hibernate documentation (13.4) DML style updates should be supported. Can anyone point out what am I doing wrong? I have the following code:

Session session = CurrentJobContextUtil.getCurrentContext().getSession();
Query query = session.createQuery("update Survey s set s.state = :newState where s.endDate < :time and s.state != :newState");
query.setParameter("newState",newState);
query.setDate("time", time);
int count = (int)query.uniqueResult();

I am getting the exception:

org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [update com.customerconnect.jobmanager.data.model.Survey s set s.state = :newState where s.endDate < :time]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.errorIfDML(QueryTranslatorImpl.java:318)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:369)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:236)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1300)
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
    at org.hibernate.internal.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:966)
    at com.customerconnect.jobmanager.data.dao.SurveyDAOImpl.updateSurveyState(SurveyDAOImpl.java:26)
    at com.customerconnect.jobmanager.CCJobDataSvc.expireSurveys(CCJobDataSvc.java:510)
    at com.customerconnect.jobmanager.jobs.SurveyJobs.expireSurveys(SurveyJobs.java:30)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
    at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(Unknown Source)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

Answer

Change (int)query.uniqueResult() to query.executeUpdate(); it’ll return the number of rows affected.

We are here to answer your question about Bulk updating rows using hibernate - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji