使用 jpql 查询 jdbc:列索引超出范围:0,列数:1

querying jdbc using jpql: The column index is out of range: 0, number of columns: 1

我正在尝试使用 org.springframework.data.jpa.repository.Query 注释查询包含来自 Java 的 jdbc 列的 postgresql table。

以下查询:

@Query (value=SELECT o FROM my_table o where trim(o.my_field_1 = ?1)`, nativeQuery = true)

失败:

FailedObject: SELECT o FROM my_table o where trim(o.my_field_1) = ? and (o.my_jsonb_field is null or (o.my_jsonb_field->'my_jsonb_inner_field') is null or (o.my_jsonb_field->'my_jsonb_inner_field') = cast('""' as jsonb)) [java.lang.String]] with root cause

org.postgresql.util.PSQLException: The column index is out of range: 0, number of columns: 1.

(最后的连词and (o.my_jsonb_field is null ...)没有区别)

问题1.这个错误是什么意思?我做错了什么?

同时,以下非本机查询有效:

@Query(value = "SELECT o FROM MyJavaObject o where trim(o.my_field_1) = :my_field_1")

但是,如果我添加 jsonb -> 运算符,它会失败:

@Query(value = "SELECT o FROM MyJavaObject o where trim(o.my_field_1) = :my_field_1 and (o.my_jsonb_field is null or (o.my_jsonb_field->'my_jsonb_inner_field') is null or (o.my_jsonb_field->'my_jsonb_inner_field') = cast('\"\"' as jsonb))")

与:

Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.lang.Iterable com.my.company.etc.project.model.repo.Repo.getRecordByAB(java.lang.String)! "Encountered "o . my_jsonb_field - >" at character 88, but expected: ["(", ")", "*", "+", "-", ".", "/", ":", "<", "<=", "<>", "=", ">", ">=", "?", "ABS", "ALL", "AND", "ANY", "AS", "ASC", "AVG", "BETWEEN", "BOTH", "BY", "CASE", "COALESCE", "CONCAT", "COUNT", "CURRENT_DATE", "CURRENT_TIME", "CURRENT_TIMESTAMP", "DELETE", "DESC", "DISTINCT", "EMPTY", "ESCAPE", "EXISTS", "FETCH", "FROM", "GROUP", "HAVING", "IN", "INDEX", "INNER", "IS", "JOIN", "KEY", "LEADING", "LEFT", "LENGTH", "LIKE", "LOCATE", "LOWER", "MAX", "MEMBER", "MIN", "MOD", "NEW", "NOT", "NULL", "NULLIF", "OBJECT", "OF", "OR", "ORDER", "OUTER", "SELECT", "SET", "SIZE", "SOME", "SQRT", "SUBSTRING", "SUM", "TRAILING", "TRIM", "TYPE", "UPDATE", "UPPER", "VALUE", "WHERE", <DATE_LITERAL>, <DECIMAL_LITERAL>, <IDENTIFIER>, <INTEGER_LITERAL>, <STRING_LITERAL2>, <STRING_LITERAL>, <TIMESTAMP_LITERAL>, <TIME_LITERAL>]." while parsing JPQL "SELECT o FROM MyJavaObject o where trim(o.my_field_1) = :my_field_1 and (o.my_jsonb_field is null or (o.my_jsonb_field->'my_jsonb_inner_field') is null or (o.my_jsonb_field->'my_jsonb_inner_field') = cast('""' as jsonb))". See nested stack trace for original parse error. -> [Help 1]

问题2,jpql好像没看懂->。有解决办法吗?

这应该是评论,但想指出您 code/SQL

中有问题的地方
@Query (value=SELECT o FROM my_table o where trim(o.my_field_1 = ?1)`, nativeQuery = true)

声明为本机查询,但使用 SELECT o 和 ?1 等 JPA 符号。 这应该像下面

@Query (value=SELECT * FROM my_table o where trim(o.my_field_1 = :myFieldFilter), nativeQuery = true)

现在第二次查询

@Query(value = "SELECT o FROM MyJavaObject o where trim(o.my_field_1)
= :my_field_1 and (o.my_jsonb_field is null or (o.my_jsonb_field->'my_jsonb_inner_field') is null or (o.my_jsonb_field->'my_jsonb_inner_field') = cast('\"\"' as jsonb))")

JPA/Spring数据不明白运算符->是什么意思。这是 Postgres 特有的。

尝试使用下面的

@Query(value = "SELECT * FROM my_table o where trim(o.my_field_1)
    = :my_field_1 and (o.my_jsonb_field is null or (o.my_jsonb_field->'my_jsonb_inner_field') is null or (o.my_jsonb_field->'my_jsonb_inner_field') = cast('\"\"' as jsonb))", nativeQuery=true)