Search code examples
mysqlspringhibernatehql

How to write update query using hibernate +Spring


Not supported for DML operations

List<LeadsRequest> updateEngg = getEntityManager().createQuery("UPDATE LeadsRequest l set l.status = :status where l.id = :id")
                                .setParameter("status", status).setParameter("id",id).getResultList();
java.lang.IllegalStateException:
org.hibernate.hql.internal.QueryExecutionRequestException: 
Not supported for DML operations

Solution

  • As clearly stated in the java doc for Query.getResultList this is only for SELECT statements. It will return the result for the given select query, and as such will not work for INSERT/UPDATE/DELETE queries.

    For INSERT/UPDATE/DELETE use the Query.executeUpdate method. Which will run the DML statement and return the number of rows affected.