问题是如何使我的本地查询接受空参数?
以下是我的查询:
以下是我的查询:
@Query(value = "SELECT " +
" summaries.event_type as \"eventType\", " +
" summaries.institution_id as \"institutionId\", " +
" identifiers.id as \"studentIdentifierId\", " +
" identifiers.name as \"studentIdentifierName\" " +
"FROM summaries " +
"LEFT JOIN roles ON summaries.student_id=roles.id " +
"LEFT JOIN identifiers ON roles.identifier_id=identifiers.id " +
"WHERE " +
" summaries.student_id IS NOT NULL AND " +
" summaries.event_type IN (:eventTypes) AND " +
" (:identifierId IS NULL OR roles.identifier_id=:identifierId) " +
"GROUP BY " +
" summaries.institution_id, " +
" summaries.student_id, " +
" identifiers.id, " +
" identifiers.name, " +
" summaries.event_type",
nativeQuery = true)
List<CustomProjection> findByCustomCondition(
@Param("identifierId") Long identifierId,
@Param("eventTypes") List<String> eventTypes);
现在当我将identifierId
传入null时,我收到以下错误:
InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet.
Caused by: org.postgresql.util.PSQLException: ERROR: operator does not exist: bigint = bytea
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
看起来这一行代码 (:identifierId IS NULL OR roles.identifier_id=:identifierId)
在这里引起了问题,但是我真的找不到一个好的解决办法,因为据我理解它应该正常工作的(当我直接在pgAdmin上使用这个查询时,它按照预期工作,所以我相当确定这是与映射相关的问题)。
我尝试将标识符强制转换为(cast(:identifierId as bigint)
),但没有帮助。
PS。这是一个有点陈旧的项目,所以我使用的是以下版本:
compile group: 'org.springframework.data', name: 'spring-data-jpa', version: '1.11.6.RELEASE'
compile group: 'org.postgresql', name: 'postgresql', version: '42.2.2'
compile group: 'org.hibernate', name: 'hibernate-core', version: '5.2.17.Final'
roles.identifier_id
中使用了什么类型? - SternKbigint
。 - degath0L
传递时,它不会中断,因此我不确定它是否与投影相关。 - degath