Wednesday, October 19, 2011

No JPQL order by query parameters?

So after multiple debugging session checking my Hibernate query and the database it appears that JPQL doesn't allow you to add the order by statement as a query parameter, such as:


@NamedQuery(name = "Review.getAllReviewsForObject", 
            query = "SELECT r FROM Review r where objectType.id = ?1 and objectId = ?2 and
                     status.code = 'ACTIVE' order by ?3 desc"
)


The work around I found was to create two named queries with the two options for order by in my case. This hardly seems ideal for situations when numerous multiple order by will be needed. 

No comments:

Post a Comment