首页 > 解决方案 > 如何在 Spring Boot JPA 中按 Posgres 的 json 属性排序?

问题描述

我写了一个@QueryinJPA来按属性获取详细信息,json这会导致错误。

 @Query("SELECT t FROM Tcl order by t.equipment->>'eqpm_n' ASC")
 public List<Tcl> getEquipmentList();

引起:org.hibernate.hql.internal.ast.QuerySyntaxException:意外令牌

 SELECT t FROM com.target.mpe.models.Tcl order by t.equipment->>'eqpm_n' ASC

同样的查询在 Postgres 控制台中运行良好。我怎样才能让它在 SpringBoot JPA 中工作?我需要尝试原生查询吗?

标签: postgresqlspring-bootspring-data-jpa

解决方案


JPQL 不支持这种 PostgreSQL 语法。您必须使用 nativeQuery:

@Query(value = "SELECT * FROM Tcl t order by t.equipment->>'eqpm_n' ASC", nativeQuery = true)
public List<Tcl> getEquipmentList();

推荐阅读