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

时间:2016-06-08 22:45:18

标签: java spring postgresql jdbc jsonb

我尝试使用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并不理解 - &gt;。有解决方法吗?

1 个答案:

答案 0 :(得分:0)

这应该是一个评论,但是想要指出代码/ SQL中有问题的区域

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

声明为本机查询但使用JPO表示法,如SELECT o和?1。 这应该如下所示

@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数据不明白运算符是什么 - &gt;手段。这对Po​​stgres非常具体。

尝试使用下面的

@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)