Skip to content

Software Development at Program Tom LTD

Place for coding, programming, development and software in general.

Menu
  • Blog
  • PDF Booklets
  • Dev Utils & Content
  • Java Spring Boot Or Web Apps
  • English
    • български
    • English
    • Español
    • Português
    • हिन्दी
    • Русский
    • Deutsch
    • Français
    • Italiano
    • العربية
  • About Us
Menu
How to pass a List in Spring Repository Query

How to pass a List in Spring Repository Query

Posted on April 5, 2025 by Toma Velev

To pass a List (or any Collection) into a Spring Data JPA repository query, you can use the IN clause in JPQL or native SQL, and Spring will automatically bind the list elements.

✅ Example 1: Using @Query with JPQL

@Query("SELECT p FROM Product p WHERE p.id IN :ids")
List<Product> findByIds(@Param("ids") List<Long> ids);

You can then call this method like:

List<Long> ids = Arrays.asList(1L, 2L, 3L);
List<Product> products = productRepository.findByIds(ids);

✅ Example 2: Derived Query Method (No @Query)

You can even use Spring Data’s method naming:

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

This does the same thing — it will generate a query like:

SELECT * FROM product WHERE id IN (?, ?, ?)

🧠 Notes:

  • List, Set, or any Collection works fine.
  • Avoid passing a huge list (thousands of items), it might hit SQL limits.
  • If your collection is empty, be careful — some databases (like Oracle) may throw errors. You might want to handle the empty case separately.

I’m developing some Spring Apps (most of them sitting on top of Vaadin) – you could check here: https://programtom.com/dev/product-category/technologies/spring-boot-framework/?orderby=date-desc

  • Jenkins SCP File Upload to Remote Server
  • Essential Programming Books – Principles & Flutter
  • Social Media Platforms 🌍
  • Strategies to prevent review regressions
  • How to set Google Map Theme in a Flutter App

Categories

  • Apps (22)
  • ChatGPT (23)
  • Choosing a Framework (38)
  • Flutter (269)
  • Graphical User Interface (14)
  • Marketing (117)
  • Software Development (286)
  • Spring (45)
  • StartUp (22)
  • Uncategorized (14)
  • Uncategorized (4)
  • Vaadin (15)

Tags

Algorithms (9) crypto (29) flutterdev (39) General (86) Java (7) QR & Bar Codes (3) Software Dev Choices (33) Spring Boot (1) standards (1) Theme (3) User Authentication & Authorization (9) User Experience (10) Utilities (19) WordPress (11)

Product categories

  • All Technologies (84)
    • Flutter Apps (24)
    • GPT (4)
    • Java (38)
    • Native Android (3)
    • PHP (9)
    • Spring (Boot) / Quarkus (35)
    • Utils (15)
    • Vaadin 24+ (27)
    • Vaadin 8 (1)
  • Apps (18)
    • Employees DB (1)
    • Notes (6)
    • Personal Budget (1)
    • Recipes Book (1)
    • Stuff Organizer (1)
    • To-Do (2)
  • PDF Books (3)
  • Source Code Generators (8)

Recent Posts

  • Jenkins SCP File Upload to Remote Server
  • Essential Programming Books – Principles & Flutter
  • Social Media Platforms 🌍
  • Strategies to prevent review regressions
  • How to set Google Map Theme in a Flutter App

Post Categories

  • Apps (22)
  • ChatGPT (23)
  • Choosing a Framework (38)
  • Flutter (269)
  • Graphical User Interface (14)
  • Marketing (117)
  • Software Development (286)
  • Spring (45)
  • StartUp (22)
  • Uncategorized (14)
  • Uncategorized (4)
  • Vaadin (15)