Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Not supported for DML operations" with simple UPDATE query

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.

like image 668
Webnet Avatar asked Jul 12 '13 18:07

Webnet


People also ask

What can we do by using @query JPA annotation?

The @Query annotation can only be used to annotate repository interface methods. The call of the annotated methods will trigger the execution of the statement found in it, and their usage is pretty straightforward. The @Query annotation supports both native SQL and JPQL.

Which interface should you implement to let Spring manage the underlying queries?

The central interface in Spring Data repository abstraction is Repository (probably not that much of a surprise). It takes the domain class to manage as well as the id type of the domain class as type arguments.


3 Answers

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();
like image 175
Ajay Bhojak Avatar answered Oct 15 '22 07:10

Ajay Bhojak


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);```
like image 11
srp Avatar answered Oct 15 '22 06:10

srp


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.

like image 1
Jacek Sawko Avatar answered Oct 15 '22 07:10

Jacek Sawko