Как передать аргумент JPA при использовании Postgres LTREE? - PullRequest
0 голосов
/ 04 января 2019

У меня есть приложение Spring Boot, использующее JPA и таблицу Postgres со структурой данных ltree.

Работает, когда я запрашиваю оператор sql, но не передаю аргумент.

Контроллер

@GetMapping("/path")
ResponseEntity<?> findAllByPath() {

    List<Tree> allByPath = treeRepository.findAllByPath("A");

    return ResponseEntity.ok().body(allByPath);
}

Рабочий код в репозитории

@Query(value = "SELECT * FROM Tree WHERE path <@ 'A'", nativeQuery = true)
List<Tree> findAllByPath(@Param("pathToSearch") String pathToSearch);

* Не Рабочий код в репозитории

@Query(value = "SELECT * FROM Tree WHERE path <@ '" + ":pathToSearch" +"'", nativeQuery = true)
List<Tree> findAllByPath(@Param("pathToSearch") String pathToSearch);

Error

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Fri Jan 04 17:41:43 CET 2019
There was an unexpected error (type=Internal Server Error, status=500). could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet

StackTrace

2019-01-04 17:41:43.357 DEBUG 33826 --- [nio-8080-exec-1] org.hibernate.SQL                        : 
    SELECT
        * 
    FROM
        Tree 
    WHERE
        path <@ ':pathToSearch'
Hibernate: 
    SELECT
        * 
    FROM
        Tree 
    WHERE
        path <@ ':pathToSearch'
2019-01-04 17:41:43.362  WARN 33826 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 42601
2019-01-04 17:41:43.363 ERROR 33826 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: syntax error at position 0
  Position: 34
2019-01-04 17:41:43.381 ERROR 33826 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause

org.postgresql.util.PSQLException: ERROR: syntax error at position 0
  Position: 34
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2440) ~[postgresql-42.2.5.jar:42.2.5]

EDIT: Когда я пытаюсь удалить кавычки в Запросе, происходит сбой с другой ошибкой.

@Query(value = "SELECT * FROM Tree WHERE path <@ :pathToSearch", nativeQuery = true)
List<Tree> findAllByPath(@Param("pathToSearch") String pathToSearch);

2019-01-08 12:52:25.213 DEBUG 18840 --- [nio-8080-exec-1] org.hibernate.SQL                        : 
SELECT
    * 
FROM
    Tree 
WHERE
    path <@ ?
Hibernate: 
    SELECT
        * 
    FROM
        Tree 
    WHERE
        path <@ ?
2019-01-08 12:52:25.222 TRACE 18840 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [A]
2019-01-08 12:52:25.240  WARN 18840 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 42883
2019-01-08 12:52:25.240 ERROR 18840 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: operator does not exist: ltree <@ character varying
  Hint: No operator matches the given name and argument types. You might need to add explicit type casts.
  Position: 31
2019-01-08 12:52:25.266 ERROR 18840 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause

org.postgresql.util.PSQLException: ERROR: operator does not exist: ltree <@ character varying
  Hint: No operator matches the given name and argument types. You might need to add explicit type casts.
  Position: 31

1 Ответ

0 голосов
/ 08 января 2019

Я исправил это, приведя параметр к ltree. Модифицированный метод и запрос в Repository:

@Query(value = "SELECT * FROM Tree WHERE path <@ CAST(:pathToSearch AS ltree)", nativeQuery = true)
List<Tree> findAllByPath(@Param("pathToSearch") String pathToSearch);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...