“Not supported for DML operations” with simple UPD

2019-06-14 21:20发布

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.

4条回答
地球回转人心会变
2楼-- · 2019-06-14 21:36

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);

查看更多
Animai°情兽
3楼-- · 2019-06-14 21:39

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.

查看更多
成全新的幸福
4楼-- · 2019-06-14 21:42

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();
查看更多
贪生不怕死
5楼-- · 2019-06-14 21:43

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()

查看更多
登录 后发表回答