簡體   English   中英

使用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表。

以下查詢:

@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無法理解->。 有沒有解決方法?

這應該是一條評論,但要指出您的代碼/ 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM