35

I'm getting the error Not supported for DML operations when I use the following HQL...

@Query("UPDATE WorkstationEntity w SET w.lastActivity = :timestamp WHERE w.uuid = :uuid")
void updateLastActivity(@Param("uuid") String uuid, @Param("timestamp") Timestamp timestamp);

What could be causing the issue? It doesn't seem to be a common error given the few results I've found in Google.

tk_
  • 13,042
  • 6
  • 71
  • 81
Webnet
  • 55,814
  • 100
  • 278
  • 454
  • can you provide code full listing and the stack trace. – Ajay Bhojak Jul 30 '13 at 09:11
  • Look at the accepted solution for this http://stackoverflow.com/a/12426144/172306 looks similar. – fn. Jul 30 '13 at 16:15
  • 2
    +1 for "@Modifying" (from your own comment in the answer of Ajay Bhojak) Your should add this as the default answer, as this is supposedly a spring JPA Repository question, you were questioning – minni Aug 10 '15 at 07:58

5 Answers5

39

Check the post hibernate hql ERROR: Not supported for DML operations in the hibernate users forum.

Most likely you called

querySt.list();

for your UPDATE query. Instead you should call

querySt.executeUpdate();
BetaRide
  • 14,756
  • 26
  • 84
  • 153
Ajay Bhojak
  • 1,161
  • 9
  • 17
  • 2
    I just solve the same problem by adding the Modifying annotation. One more hint: the function return type must be void, because I copied the function from a normal query function so the return type was List, then I have the same error even after I added Modifying annotation. – cn123h Aug 15 '15 at 14:48
  • @cn123h had the same problem, copy paste had the return type as List instead of void – Vishnoo Rath Jul 14 '17 at 07:30
9

I was also having the same problem with annotations.After searching and doing some tricks I was able to solve it. There are some below steps which you need to verify while using DML operation with JPA.

  1. Use anotation @Modifying(org.springframework.data.jpa.repository.Modifying) and @Transactional(org.springframework.transaction.annotation.Transactional) on required method.

  2. Use void as return type of method.

e.g:-

@Modifying
@Query("UPDATE ProcedureDTO o SET o.isSelectedByUser =?1")
@Transactional
public void getListOfProcedureBasedOnSelection(Boolean isSelected);```
Paulin Amougou
  • 129
  • 1
  • 9
srp
  • 373
  • 3
  • 14
1

I had exact same problem, in my case I had to only add @Modifying annotation. According to documentation:

Indicates a query method should be considered as modifying query as that changes the way it needs to be executed. This annotation is only considered if used on query methods defined through a Query annotation. It's not applied on custom implementation methods or queries derived from the method name as they already have control over the underlying data access APIs or specify if they are modifying by their name. Queries that require a @Modifying annotation include INSERT, UPDATE, DELETE, and DDL statements.

0

The same happened to me because, being q an object of class Query, q.list() is not to be used for updates or deletes, but q.executeUpdate()

Weslor
  • 21,424
  • 2
  • 17
  • 31
0

Make sure your service class method which calls updateLastActivity has @Transactional(org.springframework.transaction.annotation.Transactional) annotation. and modify the repository method to below,

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
...
@Modifying
@Query("UPDATE WorkstationEntity w SET w.lastActivity = :timestamp WHERE w.uuid = :uuid")
void updateLastActivity(@Param("uuid") String uuid, @Param("timestamp") Timestamp timestamp);

For more insights please use this answer.

tk_
  • 13,042
  • 6
  • 71
  • 81