Home > Mobile >  Spring JPA Query to get Sub-List of provided IDs not in Table
Spring JPA Query to get Sub-List of provided IDs not in Table

Time:08-09

Is there a way using a Spring JPA Repository Query to get a sub-list of the IDs that were not present in our table given a list of IDs?

Something like this:

@Query(value = "Some query returning a sublist of orderIds not in TABLE")
List<String> orderIdsNotInTable(@Param("orderIds") List<String> orderIds);

I found a link here but I cant think of how to make that a JPA Query.

EDIT: The goal here is to save on running memory so if there are thousands of ids and many calls happening at once I would like it to be handled without creating a second copy of all the ids potentially.

CodePudding user response:

As from what your questions asks, my solution would be:

  1. retrieve the list of IDs present in your database:
   @Query("select t.id from Table t")
    List<String> findAllIds();
  1. Loop through the list of IDs you have and look up if the list of IDs from the database table does not contain your id.
 List<String> idsNotContained= orderIds.stream()
    .filter(!findAllIds()::contains)
    .collect(Collectors.toList());

CodePudding user response:

@Query(value = "SELECT t.id FROM TABLE t WHERE t.id NOT IN :orderIds")
List<String> orderIdsNotInTable(@Param("orderIds") List<String> orderIds);

I don't know if I understood you correctly but can you try the solution above.

  • Related