How do you handle with bulk deleting by an array of IDs in Spring Data JPA?

前端 未结 4 826
星月不相逢
星月不相逢 2021-02-04 05:15

Now I have a class User, I get a request data of an array from the jsp or html.

list this Integer[] arr=[5,6,9,10,62,52,21]

相关标签:
4条回答
  • 2021-02-04 05:25

    if your repository interface extends CrudRepository, you can simply use deleteAll(List< User > entities). No such query is required.

    1. get List< User > by using findAllById(ids)

    2. deleteAll(List< User > entities)

    0 讨论(0)
  • 2021-02-04 05:27

    Just add the following to your user repository interface

    void deleteByIdIn(List<Integer> ids);
    

    Spring will automatically generate the appropriate query via method name derivation.

    https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods

    EDIT: A litte more detail on this

    Using Springs Repository interfaces like CrudRepository, JpaRespository brings the basic set of database operations, like create, read, update, delete, paging, sorting and so on.

    To manually add some simple queries like searching for a users name or mail address spring provides a fine mechnanism without annotating any string based HQL queries or similar.

    Spring just analyses your method names, searching for keywords. Read the documentation link above which keywords are provided.

    Example methods for a CrudRepository<User>:

    Iterable<User> findByNameLike(String search) resolves to select * from user where name like '<search>'

    void deleteByIdIn(List<Integer> ids) resolves to delete from user where id in ([ids])

    0 讨论(0)
  • 2021-02-04 05:31

    Suppose you have a UserRepository like:

    public interface UserRepository extends JpaRepository<User, Integer> {}
    

    Then you can add a modifying query method like following into your UserRepository:

    /**
     * Delete all user with ids specified in {@code ids} parameter
     * 
     * @param ids List of user ids
     */
    @Modifying
    @Query("delete from User u where u.id in ?1")
    void deleteUsersWithIds(List<Integer> ids);
    

    Finally you can change your bulk deletion service like following:

    @Transactional
    @Override
    public void deleteSomeUser(Integer[] ids) {
        oneRepository.deleteUsersWithIds(Arrays.asList(ids));
    }
    

    This will generate a delete query like:

    Hibernate: delete from users where id in (? , ? , ?)
    

    Also be aware of Self Invocation issues when you calling one public advised method from another one.

    0 讨论(0)
  • 2021-02-04 05:37

    Thanks rayrayj92 for the solution. You don't need to write any custom query, just get a list of Objects and delete all objects by that list.

    @DeleteMapping("/deleteproduct")
    public ResponseEntity<?> deleteProduct(@Valid @RequestBody Map<String,Object> userMap){
    
        List<String> idList=(List<String>) userMap.get("id_list");
    
        List<Product> productList=(List<Product>) productRepository.findAllById(idList);
        productRepository.deleteAll(productList);
        return ResponseEntity.status(HttpStatus.OK).body("Deleted item : "+productList);
    
    }
    
    0 讨论(0)
提交回复
热议问题