brainydexter
brainydexter

Reputation: 20356

how can i specify operator in order by clause?

I have the following query (using jpa 2.0):

String query = "
SELECT p.id, p.name 
FROM package p
ORDER BY (p.id = :idPackage) DESC, (p.mPrice+p.vPrice) DESC 
LIMIT 10 ";

query.setParameter("idPackage", idPackage);
query.getResultList();

where package has the following attributes:

Package
 - id
 - name
 - mPrice
 - vPrice
 - duration

In the JPA query, when I try to execute it, it complains about the "=" operator in ORDER BY clause. Is there any way to get around it ??

This is the exception I get:

org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node:

Upvotes: 1

Views: 201

Answers (2)

Omesh
Omesh

Reputation: 29101

You need to use = which is a comparison operator:

SELECT p.id, p.name 
FROM package p
ORDER BY (p.id = idPackage) DESC, (p.mPrice+p.vPrice) DESC 
LIMIT 10;

EDIT: try:

String query = "
SELECT p.id, p.name 
FROM package p
ORDER BY (p.id = idPackage) DESC, (p.mPrice+p.vPrice) DESC 
LIMIT 10 ";

Upvotes: 0

tibtof
tibtof

Reputation: 7957

You cannot use '=' in order by clause with JPA Queries. If you really need that, you can use createNativeQuery instead createQuery.

Upvotes: 1

Related Questions