jakartaee / persistence

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

JPQL optional identification variable #452

Closed gavinking closed 3 months ago

gavinking commented 11 months ago

I propose that we bless queries like this one:

select title from Book where publicationDate > :date order by title, publicationDate

This is something that SQL lets you do, of course.

Hibernate has tolerated stuff like this for quite a long time, and now supports it robustly. I would be surprised if other providers don't also allow it. (But I don't know if EclipseLink supports it.)

Of course, we would probably want to impose some restrictions on this. It's most useful when there's only one entity in the query, and therefore no risk of field name collision, so the "minimal" functionality would be to allow it only in that case.

This isn't a super-high priority, but it would be a good way to improve portability of queries between providers.

gavinking commented 4 months ago

As a result of activity in Jakarta Data, this is now a much higher priority than I previously rated it.

@lukasj let's chat about this one soon.

gavinking commented 4 months ago

Summary of current proposal:

  1. When there is a FROM element and no JOINs, the identification variable is optional and defaults to this.
  2. The SELECT clause becomes optional, and defaults to SELECT this.
  3. If the first element of a path expression is not an identification variable, it is interpreted as if it began with this..

Thus, the query:

from Book where title like :title

Is equivalent to:

select this from Book as this where this.title like :title

This significantly streamlines the syntax of simple queries, without doing violence to the semantics of JPQL.