Spring CrudRepository findByInventoryIds(List inventoryIdList) - equivalent to IN clause

后端 未结 3 630
执念已碎
执念已碎 2020-11-29 17:35

In Spring CrudRepository, do we have support for \"IN clause\" for a field? ie something similar to the following?

 findByInventoryIds(List inve         


        
3条回答
  •  伪装坚强ぢ
    2020-11-29 18:05

    Yes, that is supported.

    Check the documentation provided here for the supported keywords inside method names.

    You can just define the method in the repository interface without using the @Query annotation and writing your custom query. In your case it would be as followed:

    List findByIdIn(List ids);
    

    I assume that you have the Inventory entity and the InventoryRepository interface. The code in your case should look like this:

    The Entity

    @Entity
    public class Inventory implements Serializable {
    
      private static final long serialVersionUID = 1L;
    
      private Long id;
    
      // other fields
      // getters/setters
    
    }
    

    The Repository

    @Repository
    @Transactional
    public interface InventoryRepository extends PagingAndSortingRepository {
    
      List findByIdIn(List ids);
    
    }
    

提交回复
热议问题