【发布时间】:2018-07-10 10:31:28
【问题描述】:
我将下面的代码用于带有 JPA 标准查询的 PostgresDB
public static Specification<EventRecord> findEventRecords(final String id,
final Boolean status,
final Date createdTime, final Date updatedTime, final String userId,
final List<ChannelType> channelType, final List<String> eventType) {
return (root, query, builder) -> {
List<Predicate> predicates = new ArrayList<Predicate>();
predicates.add(builder.and(builder.equal(root.get( TestRecord_.id), id))); //1st param from Rest API
predicates.add(builder.and(builder.equal(root.get( TestRecord_.status),status))); //2nd param from Rest API
predicates.add(builder.and(builder.greaterThanOrEqualTo(root.get( TestRecord_.createdTime), createdTime))); //3rd param from Rest API
predicates.add(builder.and(builder.lessThanOrEqualTo(root.get( TestRecord_.updatedTime), updatedTime))); //4th param from Rest API
if (eventType != null && !eventType.isEmpty()) {
predicates.add(builder.and(builder.equal(root.get( TestRecord_.eventType), eventType))); // gives error saying No value specified for parameter 5.
}
Predicate[] predicatesArray = new Predicate[predicates.size()];
return builder.and(predicates.toArray(predicatesArray));
};
}
}
我尝试使用 in 和 isMember(),但仍然出现错误,如何使用 IN 子句使用谓词构建查询。
Query :
select * from test_record where (id=? and status = true and created_time = ? and updated_time = ? and event_type IN (value1, value2, value3);
更新
Query That i want to build with Predicates :
select * from test_record where (id=? and status = ? and created_time = ? and updated_time = ? and event_type IN ?;
所以当我传递任何列表值时,我都会遇到同样的错误,如何解决这些错误:
org.postgresql.util.PSQLException: No value specified for parameter 5.
at org.postgresql.core.v3.SimpleParameterList.checkAllParametersSet(SimpleParameterList.java:257) ~[postgresql-42.2.2.jar:42.2.2]
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:290) ~[postgresql-42.2.2.jar:42.2.2]
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:441) ~[postgresql-42.2.2.jar:42.2.2]
【问题讨论】:
-
PostgreSQL 仅在 SQL 中有 5 个参数槽并且您引用的参数槽有 3 个时才给出该消息。
-
有5个参数,我在查询中没有指定,现在更新一下
标签: java jpa spring-data-jpa jpa-criteria