Spring Data: “delete by” is supported?

后端 未结 9 2272
长情又很酷
长情又很酷 2020-11-28 20:57

I am using Spring JPA for database access. I am able to find examples such as findByName and countByName, for which I dont have to write any method implementation. I am hopi

9条回答
  •  情话喂你
    2020-11-28 21:39

    2 ways:-

    1st one Custom Query

    @Modifying
    @Query("delete from User where firstName = :firstName")
    void deleteUsersByFirstName(@Param("firstName") String firstName);
    

    2nd one JPA Query by method

    List deleteByLastname(String lastname);
    

    When you go with query by method (2nd way) it will first do a get call

    select * from user where last_name = :firstName
    

    Then it will load it in a List Then it will call delete id one by one

    delete from user where id = 18
    delete from user where id = 19
    

    First fetch list of object, then for loop to delete id one by one

    But, the 1st option (custom query),

    It's just a single query It will delete wherever the value exists.

    Go through this link too https://www.baeldung.com/spring-data-jpa-deleteby

提交回复
热议问题