Anana Aristotle
Anana Aristotle

Reputation: 344

org.postgresql.util.PSQLException: ERROR: syntax error at or near ":"

I am running an SQL query from PgAdmin4 which runs successfully, but when i take that same query to run it from my Spring Boot application i have an error " org.postgresql.util.PSQLException: ERROR: syntax error at or near ":" "

Here is what the SQL query looks like:

SELECT
   student.surname, student.first_name, student.last_name,
   jsonb_pretty(elems)
FROM assessment, jsonb_array_elements(assessment.assessment) AS elems
JOIN student ON student.id = (elems->>'student_id')::uuid
WHERE subject_id = 2
AND academy_year_id =3
AND form_id = 2
AND term_id = 1
AND section_id = 4;

And this run successfully from PgAdmin4.

This is my implementation in my Repository

@Query(nativeQuery = true, value = "SELECT\n" +
        "       student.surname, student.first_name, student.last_name,\n" +
        "       jsonb_pretty(elems)\n" +
        "FROM assessment, jsonb_array_elements(assessment.assessment) AS elems\n" +
        "JOIN student ON student.id = (elems->>'student_id')::uuid\n" +
        "WHERE subject_id = 2\n" +
        "AND academy_year_id =3\n" +
        "AND form_id = 2\n" +
        "  AND term_id = 1\n" +
        "AND section_id = 4;")
Object[] getSubjectAssessments();

And when my API calls this method i get this error message

org.postgresql.util.PSQLException: ERROR: syntax error at or near ":" 

I don't know what I am doing wrong.

Upvotes: 1

Views: 2767

Answers (1)

user330315
user330315

Reputation:

JPA can't handle the Postgres specific cast operator ::.

Use cast() instead:

JOIN student ON student.id = cast(elems->>'student_id' as uuid)

Upvotes: 4

Related Questions