복붙노트

[SPRING] Spring CrudRepository findByInventoryIds (List <Long> inventoryIdList) - IN 절과 동일합니다.

SPRING

Spring CrudRepository findByInventoryIds (List inventoryIdList) - IN 절과 동일합니다.

Spring CrudRepository에서는 필드에 대해 "IN 절"을 지원합니까? ie 다음과 유사한 무엇인가?

 findByInventoryIds(List<Long> inventoryIdList) 

그러한 지원을 이용할 수없는 경우, 어떤 우아한 옵션을 고려할 수 있습니까? 각 ID에 대한 실행 쿼리가 최적이 아닐 수 있습니다.

해결법

  1. ==============================

    1.findByInventoryIdIn (List inventoryIdList)이 트릭을 수행해야합니다.

    findByInventoryIdIn (List inventoryIdList)이 트릭을 수행해야합니다.

    HTTP 요청 매개 변수 형식은 다음과 같습니다.

    Yes ?id=1,2,3
    No  ?id=1&id=2&id=3
    

    JPA 저장소 키워드의 전체 목록은 현재 문서 목록에서 찾을 수 있습니다. IsIn은 동사가 읽기 쉽도록 선호하는 경우에 해당하며 JPA는 NotIn 및 IsNotIn도 지원합니다.

  2. ==============================

    2.Spring CrudRepository의 모든 메소드에 대해 @Query를 직접 지정할 수 있어야합니다. 이 같은 것이 작동해야합니다 :

    Spring CrudRepository의 모든 메소드에 대해 @Query를 직접 지정할 수 있어야합니다. 이 같은 것이 작동해야합니다 :

    @Query( "select o from MyObject o where inventoryId in :ids" )
    List<MyObject> findByInventoryIds(@Param("ids") List<Long> inventoryIdList);
    
  3. ==============================

    3.예, 지원됩니다.

    예, 지원됩니다.

    메소드 이름 내에 지원되는 키워드는 여기 제공된 문서를 확인하십시오.

    @Query 어노테이션을 사용하거나 사용자 정의 쿼리를 작성하지 않고 저장소 인터페이스에서 메소드를 정의 할 수있다. 귀하의 경우 그것은 다음과 같이됩니다 :

    List<Inventory> findByIdIn(List<Long> ids);
    

    Inventory 엔티티와 InventoryRepository 인터페이스가 있다고 가정합니다. 케이스의 코드는 다음과 같아야합니다.

    엔티티

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

    저장소

    @Repository
    @Transactional
    public interface InventoryRepository extends PagingAndSortingRepository<Inventory, Long> {
    
      List<Inventory> findByIdIn(List<Long> ids);
    
    }
    
  4. from https://stackoverflow.com/questions/18987292/spring-crudrepository-findbyinventoryidslistlong-inventoryidlist-equivalen by cc-by-sa and MIT license