這工作時注漿JSON參數 - 春天JPA
@Query(value = "SELECT * FROM person WHERE school = :schoolId AND details @> '{\"id\":\"1234\",\"name\":\"John\"}'", nativeQuery = true)
我傳遞@Param( 「schoolId」)字符串schoolId
但是當我通過JSON作爲PARAM,它失敗
"org.springframework.dao.InvalidDataAccessResourceUsageException", could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
org.postgresql.util.PSQLException: ERROR: operator does not exist: jsonb @> character varying Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
@Query(value = "SELECT * FROM person WHERE school = :schoolId AND details @> :details", nativeQuery = true)
@Param("schoolId") String schoolId, @Param("details") String details
請向我們展示真正的異常,這是''SQLGrammarException'(它在原因鏈的末尾)造成的。我們只能猜測沒有它。 - 但我最好的選擇是spring + jdbc將'@Param(「details」)字符串細節參數綁定爲'VARCHAR'。如果你想使用一些非常規的類型,比如'uuid'或'json [b]',通常很難使用JDBC。只需在連接字符串中添加'stringtype = unspecified' [JDBC DSN參數](https://jdbc.postgresql.org/documentation/head/connect.html#connection-parameters)就可以避免很多麻煩。 – pozs
......或者你可以使用明確的轉換,比如'details @> CAST(:details AS json [b])'',但這是很不愉快的恕我直言。 – pozs
@pozs你是正確的,它確實工作後,它被稱爲CAST(:細節AS JSONB)。如果您可以發佈相同的答案,我會接受並解決問題。 –