jakartaee / persistence

https://jakartaee.github.io/persistence/
Other
186 stars 55 forks source link

JPQL functions in 'order by' #597

Closed gavinking closed 3 months ago

gavinking commented 3 months ago

Both Hibernate and EclipseLink, and I imagine other JPQL implementations—and AFAIK all SQL databases—allow the use of functions in the order by clause. This is especially useful if you want case-insensitive sorting, for example:

from Book order by upper(title)

JPQL is unfortunately a bit stuck in the past here, and in principle requires me to write:

select b, upper(title) as t from Book order by t

But I highly doubt that any users actually write this. And so the spec is simply lying about how JPQL is actually used in the real world.

Crazily enough, the criteria API already allows arbitrary expressions in asc() and desc(), even though JPQL itself supposedly doesn't have this feature!

I think it's very clear that we should at least bless the use of upper() and lower() here. But I would say we can simply allow arbitrary scalar expressions, perhaps with some weasel words to say it's not 100% required if the database doesn't support it.