Francis Francis - 2 months ago 25
Java Question

org.springframework.orm.hibernate4.HibernateQueryException: Not supported for DML operations [UPDATE]

I am trying to update a record in the database and I getting this error

org.springframework.orm.hibernate4.HibernateQueryException: Not supported for DML operations [UPDATE com.xxx.models.User u set u.notifiable = true WHERE u.emailAccess = :emailAccess AND u.isAdmin = false]; nested exception is org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [UPDATE com.xxx.models.User u set u.notifiable = true WHERE u.emailAccess = :emailAccess AND u.isAdmin = false]


This is my hql attempt

@Modifying
public User updateUser(String emailAccess) {

String hql = "UPDATE User u set u.notifiable = true WHERE u.emailAccess = :emailAccess AND u.isAdmin = false";
return (User) _sessionFactory.getCurrentSession().createQuery(hql).setParameter("emailAccess", emailAccess).list();

}


After researching, I added the @Modifying annotation to the top of the method but the error still persists. Please what could be wrong?

Answer

@Modifying is a Spring Data annotation and you do not appear to be using Spring Data so that is of no use to you.

You need to call executeUpdate() and make the return method void as executeUpdate

Returns: The number of entities updated or deleted..

https://docs.jboss.org/hibernate/core/3.6/javadocs/org/hibernate/Query.html#executeUpdate()

or return the result of a second query.

public void updateUser(String emailAccess) {

    String hql = "UPDATE User u set u.notifiable = true WHERE u.emailAccess = :emailAccess AND u.isAdmin = false";
    _sessionFactory.getCurrentSession().createQuery(hql).setParameter("emailAccess", emailAccess).executeUpdate();

}
Comments