我想在 Spring Boot 應用程序中使用休眠從 PostgreSql 數據庫的查詢返回值的 JsonArray 中檢索一個對象。但是我面臨一個異常,“>”是意外標記,盡管我的查詢在 pgAdmin4 查詢工具中運行良好,以下是我的代碼片段。@PersistenceContextprivate EntityManager entityManager;@Transactionalpublic String getItemById(Long id) { String result = (String) entityManager.createQuery("SELECT JSON_AGG(items)->>0 AS item FROM items WHERE id=:id").setParameter("id", id) .getResultList().get(0); System.out.println(result); return result;}異常如下:org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: > near line 1, column 24 [SELECT JSON_AGG(items)->>0 AS item FROM items WHERE id=:id]; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: > near line 1, column 24 [SELECT JSON_AGG(items)->>0 AS item FROM items WHERE id=:id] at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:367)請幫助實現這一目標或建議我在不更改查詢的情況下解決問題。提前致謝。
1 回答

慕容3067478
TA貢獻1773條經驗 獲得超3個贊
您使用的 API 方法 ( entityManager.createQuery(String)
) 是執行 javadoc 中所述的 JPQL 查詢:
創建 Query 實例以執行 Java Persistence 查詢語言語句。
您要執行的查詢是 SQL 查詢。您應該使用 上的createNativeQuery(String)
方法EntityManager
。
添加回答
舉報
0/150
提交
取消