2

I am using Spring Data rest with Spring JPA. I have one Spring JPA derived delete query that should deletes the list of items but when I am executing it and noticing the console I found that it is executing select query instead that's very strange situation I have ever come across.

@RepositoryRestResource(collectionResourceRel="revision", path="revision")
interface RevisionRepository extends JpaRepository<Revision, Long> {

     List<Revision> deleteByContentId(long contentId)
}

I have even tried using Long instead of List<Revision> doesn't work and also have tried removeByContentId it is also doesn't work either and keeps executing the select query instead delete query.

when I am running this method this is what I got on my console

Hibernate: select revision0_.id as id1_2_, revision0_.body as body2_2_, revision0_.content_id as content_3_2_, revision0_.content_type as content_4_2_, revision0_.date_created as date_cre5_2_, revision0_.file_name as file_nam6_2_, revision0_.folder_id as folder_i7_2_, revision0_.force_ssl as force_ss8_2_, revision0_.is_active as is_activ9_2_, revision0_.lookup as lookup10_2_, revision0_.meta_description as meta_de11_2_, revision0_.meta_keywords as meta_ke12_2_, revision0_.meta_title as meta_ti13_2_, revision0_.nav_item as nav_ite14_2_, revision0_.nav_order as nav_ord15_2_, revision0_.regions_objects as regions16_2_, revision0_.summary as summary17_2_, revision0_.title as title18_2_, revision0_.updated_by as updated19_2_, revision0_.user_id as user_id20_2_ from revisions revision0_ where revision0_.content_id=?

does anyone having any idea why it is behaving strangely?

DaveyDaveDave
  • 9,821
  • 11
  • 64
  • 77
Suleman khan
  • 1,038
  • 4
  • 14
  • 34

2 Answers2

0

You need to add @Modifying annotation to your delete method. You will also need to make sure it is executed within a transaction, so you might need to add @Transactional annotation too, if you invoke this method not in a transaction.

Please see an example:

@Modifying
@Transactional
int deleteByFieldName( Long fieldValue );
Zilvinas
  • 5,518
  • 3
  • 26
  • 26
  • When I used this JPA query it is working fine already,,, I don't want to use JPA query when it can be done using JPA derived query `deleteByFieldName`. – Suleman khan Nov 12 '15 at 20:35
  • 1
    Let it be without the explicit JPA query... your issue is missing `@Modifying` annotation – Zilvinas Nov 12 '15 at 20:41
  • wow that works... I have another repository where in I am having delete method without these annotations still that methods are working but it was not working over here. – Suleman khan Nov 12 '15 at 20:48
  • This might be because of JPA version. See here: http://stackoverflow.com/questions/23723025/spring-data-delete-by-is-supported – jny Nov 12 '15 at 20:50
  • That may be because you are invoking the method in a transactional context already ( in your other place ) – Zilvinas Nov 12 '15 at 20:51
0

In latest Spring >=5 and Spring Boot >=2.

@Transactional
int deleteByFieldname( Long fieldValue );

Works fine.

Note 1: @Modifiyng annotation has absolutely no effect in this. It only work for @Query annotation. Without @Query, it is simply ignored.

Note 2: In naming convention, that depends on the configured NamingStrategy, the CamelCase might be interpreted and nested entity relation or "_" in field name. So "Fieldname" and "FieldName" mean very different things.

Note 3: Derived delete queries like this have a really nasty n+1 side effect. They ALWAYS first issue select for the rows then isses delete each row/entity one by one with separate delete stement. No way around this, except using @Query() with manual delete statement. And using @Query then requires the @Modifying for delete query.

user3852017
  • 190
  • 1
  • 9