将列表传递给in子句但不能在jpql中工作

时间:2015-01-09 08:04:53

标签: java eclipse jpa jpql

请解决我的问题我正在尝试运行这些jpql

代码就是那个

List<Jewellery> jewelleries;
List<Seller> searchedSellers;  
List<Integer> jewelleryIds;
jewelleries = entityManager
                .createQuery(
                   "SELECT j FROM Jewellery j WHERE j.id = (SELECT jt.id FROM  JewelleryType jt WHERE jt.type = :type)",
        Jewellery.class)
        .setParameter("type", jewelleryType).getResultList();
for (Jewellery j : jewelleries) {
    jewelleryIds.add(j.getId());
}
searchedSellers = entityManager
    .createQuery(
        "SELECT s FROM Seller s WHERE (s.city.id = (SELECT c.id FROM City c WHERE c.city = :parameter1 )) AND (s.jewellery.id in (:parameter2))",
        Seller.class)
        .setParameter("parameter1", city)
        .setParameter("parameter2", jewelleryIds).getResultList();

我在console.log文件中发现以下错误

java.lang.IllegalArgumentException: You have attempted to set a value of type class java.util.ArrayList for parameter
 parameter2 with expected type of class java.lang.Integer from query 
 string SELECT s FROM Seller s WHERE (s.city.id = (SELECT c.id FROM City c WHERE c.city = :parameter1 )) AND (s.jewellery.id in (:parameter2))

请解决问题。 提前谢谢..

1 个答案:

答案 0 :(得分:1)

我认为您的JPQL查询无效,您必须删除IN子句的括号,如:

SELECT s FROM Seller s WHERE (s.city.id = (SELECT c.id FROM City c WHERE c.city = :parameter1 )) AND (s.jewellery.id in :parameter2)