org.postgresql.util.PSQLException:错误:“:”或附近的语法错误

时间:2019-10-07 11:05:32

标签: java sql postgresql spring-boot spring-data-jpa

我正在从PgAdmin4运行一个SQL查询,该查询成功运行,但是当我从Spring Boot应用程序中使用该查询来运行它时,出现错误“ org.postgresql.util.PSQLException:错误:语法错误在或附近“:”“

这是SQL查询的样子:

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;

此操作从PgAdmin4成功运行。

这是我在存储库中的实现

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

当我的API调用此方法时,我会收到此错误消息

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

我不知道我在做什么错。

1 个答案:

答案 0 :(得分:2)

JPA无法处理Postgres专用的强制转换运算符cast()

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

 public static void switchToIFramesOfSearchPage() {
    wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector(GENERAL_IFRAME_CSS_LOCATOR_VALUE)));
    driver.switchTo().frame(SEARCH_PAGE_IFRAME1_ID_LOCATOR_VALUE);
    driver.switchTo().frame(SEARCH_PAGE_IFRAME2_ID_LOCATOR_VALUE);
    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public static void switchToIFrameOfToolbar() {
    wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector(GENERAL_IFRAME_CSS_LOCATOR_VALUE)));
    driver.switchTo().frame(TOOLBAR_IFRAME_ID_LOCATOR_VALUE);
    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public static void switchToIframeOfActivitiesPane(){
    wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector(GENERAL_IFRAME_CSS_LOCATOR_VALUE)));
    driver.switchTo().frame(ACTIVITIES_PANE_IFRAME_ID_LOCATOR_VALUE);
    try {
        Thread.sleep(750);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public static void switchToIFrameOfHistoryPane() {
    wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector(GENERAL_IFRAME_CSS_LOCATOR_VALUE)));
    driver.switchTo().frame(HISTORY_PANE_IFRAME_ID_LOCATOR_VALUE);
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public static void switchToIFrameOfImportantView(){
    wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector(GENERAL_IFRAME_CSS_LOCATOR_VALUE)));
    driver.switchTo().frame(IMPORTANT_VIEW_IFRAME_ID_LOCATOR_VALUE);
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public static void switchToIFrameOfAccountView(){
    wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector(GENERAL_IFRAME_CSS_LOCATOR_VALUE)));
    driver.switchTo().frame(ACCOUNT_VIEW_IFRAME_ID_LOCATOR_VALUE);
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}