Social Media
  • You are here:
  • Home »
  • Java »

Spring Data JPA – @Query – Not supported for DML operations

I was wanting to update a database table status with a list of ID’s so decided the simplest way was to use @Query and JPAQL –

public interface MyJPAObjectRepository extends BaseRepository<MyJPAObject, String> {
    @Query("update MyJPAObject m set m.status = ?1 where m.id in ?2")
    void updateMyJPAObjectStatus(Status status, List<String> idList);
}

When I ran the code I got this exception –

Caused by: org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [update com.glenware.MyJPAObject m set m.status = ?1 where m.id in (:x2_0_)]

The reason is the code is calling execute, and we need executeUpdate. The fix is fairly simple – you add the @Modifying annotation –

public interface MyJPAObjectRepository extends BaseRepository<MyJPAObject, String> {
    @Modifying
    @Query("update MyJPAObject m set m.status = ?1 where m.id in ?2")
    void updateMyJPAObjectStatus(Status status, List<String> idList);
}

The @Modifying annotaion is used to instruct spring-data that this is a “modifying-query”, and allow DML operations

Reference

http://docs.spring.io/spring-data/jpa/docs/1.10.6.RELEASE/reference/html/#jpa.modifying-queries

About the Author Martin Farrell

My name is Martin Farrell. I have almost 20 years Java experience. I specialize inthe Spring Framework and JEE. I’ve consulted to a range of businesses, and have provide Java and Spring mentoring and training. You can learn more at About or on my consultancy website Glendevon Software

follow me on:

Leave a Comment:

1 comment
Add Your Reply