Jpa或实体管理器处理参数列表:List <long>

时间:2017-05-18 09:35:56

标签: java jpa hibernate-entitymanager

我试过了:

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

我明白了:

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

我也尝试将x放在括号中:(:x)

我也试过

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

还有:

   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();
}

我仍然拥有相同的堆栈跟踪。我使用postgres 9.4 有什么帮助吗?

1 个答案:

答案 0 :(得分:2)

当字符串列表为空时看起来它会失败。

尝试在查询调用之前添加检查。

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