编程语言
首页 > 编程语言> > java – 带有“IN”表达式的JPA @namedQuery

java – 带有“IN”表达式的JPA @namedQuery

作者:互联网

我有以下namedQuery:

@NamedQuery(name = Department.getDepartmentsByIds, query = "SELECT tbl FROM Department tbl where tbl.id in (:departmentsIds)") 

我想传递参数:departmentsIds =“1,2,3”,如下所示:

query.setParameter("departmentsIds","1,2,3");

但我收到一个错误:

java.lang.IllegalArgumentException: Parameter value [1,2,3] was not matching type [java.lang.Long]

任何想法为什么?

解决方法:

将List传递给setParameter方法而不是String. List的泛型类型参数应与departmentIds字段的类型相对应.

List<Integer> ids = new ArrayList<Integer>(); //this should be your id column's type
ids.add(1);
ids.add(2);
ids.add(3);
query.setParameter("departmentsIds",ids);

标签:java,jpa,hibernate,named-query
来源: https://codeday.me/bug/20190529/1177156.html