JpaRepository Not supported for DML operations [delete query]

Cover Image for JpaRepository Not supported for DML operations [delete query]
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

πŸ“ Title: JpaRepository Not supported for DML operations [delete query]

πŸ‘‹ Hey there, tech enthusiasts! Have you ever encountered an exception when trying to delete objects using a delete query in an interface that extends JpaRepository? πŸ€” If so, I'm here to help you understand the issue and find a solution!

❓The Problem: The user had written a query to delete objects in an interface extending JpaRepository, but when executing the query, an exception was thrown. Let's take a look at the example query provided:

public interface LimitRepository extends JpaRepository<CLimit, Long> {

  @Query("delete from CLimit l where l.trader.id =:#{#trader.id}")
  void deleteLimitsByTrader(@Param("trader") CTrader trader);

}

And here's the exception thrown:

org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [delete from com.query.domain.CLimit l where l.trader.id =:__$synthetic$__1]
...

πŸ€” The Explanation: The exception message indicates that the usage of a delete query with JpaRepository is not supported for Data Manipulation Language (DML) operations, such as delete or update. JpaRepository is designed to perform CRUD (Create, Read, Update, Delete) operations using the default methods provided by the interface.

βš™οΈ The Solution: To overcome this issue, we can make use of the @Modifying annotation in combination with the @Query annotation. The @Modifying annotation indicates that the query will perform a DML operation and modify data in the database.

Here's how we can modify the code to resolve the issue:

public interface LimitRepository extends JpaRepository<CLimit, Long> {

  @Modifying
  @Query("delete from CLimit l where l.trader.id =:#{#trader.id}")
  void deleteLimitsByTrader(@Param("trader") CTrader trader);

}

By adding the @Modifying annotation before the @Query annotation, we inform Spring Data JPA that the query is expected to perform a modification operation.

🌟 Conclusion: Now that you understand why the exception was occurring and how to fix it, you can confidently delete objects using a delete query in your interface extending JpaRepository.

Feel free to reach out if you have any questions or need further assistance. Happy coding! πŸ’»

πŸ“’ Call-to-Action: Have you ever encountered any exceptions or issues while working with JpaRepository? Share your experiences and solutions in the comments below! Let's learn from each other and make our coding journey smoother. πŸš€


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

πŸ”₯ πŸ’» πŸ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! πŸš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings πŸ’₯βœ‚οΈ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide πŸš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? πŸ€” Well, my

Matheus Mello
Matheus Mello