Hearaman
Hearaman

Reputation: 8726

How to order by json property of Posgres in Spring boot JPA?

I wrote a @Query in JPA to fetch the details by json property which is resulting error.

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

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token

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

And same query is working well in Postgres console. How can i make it work in SpringBoot JPA? Do i need to try Native Query?

Upvotes: 1

Views: 641

Answers (1)

Simon Martinelli
Simon Martinelli

Reputation: 36223

JPQL does not support this PostgreSQL syntax. You have to use a nativeQuery:

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

Upvotes: 2

Related Questions