itsource

Spring CrudRepository 검색 기준InventoryIds(List inventoryIdList) - IN 절에 해당합니다.

mycopycode 2022. 8. 16. 23:36
반응형

Spring CrudRepository 검색 기준InventoryIds(List inventoryIdList) - IN 절에 해당합니다.

Spring Crud Repository에서는 필드의 "IN 절"이 지원됩니까?즉, 다음과 같은 것이 있습니까?

 findByInventoryIds(List<Long> inventoryIdList) 

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

findByInventoryIdIn(List<Long> inventoryIdList)효과가 있을 거야

HTTP 요청 파라미터 형식은 다음과 같습니다.

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

JPA 저장소 키워드의 전체 목록은 현재 문서 목록에 있습니다.라는 것을 알 수 있다IsIn가독성을 위해 동사를 사용하는 경우 등가성이며 JPA도 이 기능을 지원합니다.NotIn그리고.IsNotIn.

Spring Crud Repository의 모든 메서드에 대해 @Query를 직접 지정할 수 있어야 합니다.다음과 같은 것이 동작합니다.

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

네, 그것은 지원되고 있습니다.

메서드명 내에서 지원되는 키워드에 대해서는, 여기서 제공하는 메뉴얼을 참조해 주세요.

@Query 주석을 사용하거나 사용자 지정 쿼리를 쓰지 않고 리포지토리 인터페이스에서 메서드를 정의할 수 있습니다.고객님의 경우 다음과 같습니다.

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

인벤토리 엔티티와 Inventory Repository 인터페이스가 있을 것입니다.이 경우의 코드는 다음과 같습니다.

엔티티

@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);

}

언급URL : https://stackoverflow.com/questions/18987292/spring-crudrepository-findbyinventoryidslistlong-inventoryidlist-equivalen

반응형