title: Spring data JPA delete query
body: <p>I am trying to see if this is the right way to write delete query using spring JPA. I have a table with primary key of <code>id</code> and foreign key of <code>study_id</code>. I want to delete a row based on <code>study_id</code>. I wasn't able to just use <code>delete</code> method in JPA because <code>study_id</code> is not unique key. So I wrote new method:</p>
  <pre><code>    @Modifying
      @Transactional
      @Query(value = &quot;DELETE FROM MY_TABLE WHERE STUDY_ID = :studyId&quot;, nativeQuery = true)
      int deleteByStudyId(@Param(&quot;studyId&quot;) long studyId);
  </code></pre>
  <p>I found this from <a href="https://javadeveloperzone.com/spring-boot/spring-data-jpa-delete-query/#3_Delete_Operation_using_Custom_Query" rel="nofollow noreferrer">https://javadeveloperzone.com/spring-boot/spring-data-jpa-delete-query/#3_Delete_Operation_using_Custom_Query</a>. However, I am not sure why @Transactional and <code>nativeQuery</code> is necessary in order to process it correctly. Without them, I get errors. I am also wondering if this is right way to delete from the table. This works, but I am very new to spring JPA.</p>