Search code examples
javajpahibernate-entitymanager

Jpa or entity manager deal with list of parameter : List<Long>


I have tried :

@Query("Select m.id from Cars m where m.id not in :x")
List<Long> findNotChoosenCars(@Param("x") List<Long> CarsId);

I get :

org.postgresql.util.PSQLException: ERREUR: erreur de syntaxe sur ou près de « ) »
Translation: syntax error near << ) >>

I have also tried to put :x in brackets like : (:x)

I have also tried

@Query(value = "Select id from Cars where id not in (?1)",nativeQuery = true)
List<Long> findNotChoosenCars(List<Long> CarsId);

And also:

   private EntityManagerFactory emf;
   private List<Long> getNotSelectedCarsIds(List<Long> selectedIds){

    List<String>strings=selectedIds.stream().map(Object::toString).collect(Collectors.toList());
    final String notSelectedCarIdsSql= "Select id from Car where id not in (:strings)";
    return emf.createEntityManager().createNativeQuery(notSelectedMarkerIdsSql)
            .setParameter("strings",strings)
            .getResultList();
}

I still have the same stacktrace. I'm using postgres 9.4 Any help please ?


Solution

  • Looks like it fails when the list of string is empty.

    Try to add a check before the query call.

    if (strings == null || strings.size() == 0) {
        return new ArrayList();
    }