-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Safe-navigation operators (myentity?.myToOne
) for implicit left joins
#697
Comments
Your examples also mention |
Ooops, in my head this was a |
@yrodiere Why are you writing: SELECT document.id, document.title, document?.documentType?.name,
document?.author?.name, document?.author?.manager?.name, document?.folder?.name
FROM Document document I would have expected this to be: SELECT document.id, document.title, document.documentType?.name,
document.author?.name, document.author?.manager?.name, document.folder?.name
FROM Document document Because |
Another possibility, which has the (arguable) advantage of (arguably) fitting in more naturally with the aesthetics of SQL would be something like this: SELECT document.id, document.title,
OUTER document.documentType.name,
OUTER document.author.name,
OUTER document.author.manager.name,
OUTER document.folder.name
FROM Document document where the keyword |
Because I'm hikacking the meaning of The difficulty here is that If JPQL had advanced typing similar to languages such as Ceylon or Kotlin, I guess we could say that Now we could move the operator one step to the right, like you suggest, but I believe this puts us in a difficult position for a query like this:
You can't express this with
I suppose that's a reasonable alternative, though it does lose some expression power (can't have inner join followed by outer join, e.g. Perhaps more importantly, I suspect it doesn't offer the same potential for integrating nicely with libraries building on JPA to offer "projection binding" -- e.g. Panache allows annotating DTOs with Still, at this point I agree it's a lot about tastes and -- like you said -- aestethics. |
Problem statement
The JPA spec defines the navigation operator (
.
) in JPQL queries as yielding inner joins when it triggers implicit joins:But there is a class of queries where a left join would be more convenient, e.g. here if
documentType
is optional, we have to write this:... in order to get this SQL:
Whereas we'd love to be able to write just this:
... but obviously this is incorrect and will yield this SQL:
Proposal
This behavior is what it is, but changing it would be challenging or would involve workarounds (hints), so it'll very likely stay that way.
We could, however, offer an alternative to the navigation operator (
.
) which would yield left joins instead of inner joins. This behavior is, in a way, similar to the safe-navigation operator (?.
) used in various programming languages, so that operator would be a decent choice.So we would be able to write this:
And get the SQL we want, with an implicit left join:
In some extensions to JPQL, e.g in Hibernate, this could even be shortened to just
SELECT document.id, document?.documentType?.name
, without aFROM
clause due to that clause being inferred from context -- but this is obviously out of scope for this issue.This could remove a lot of verbosity in queries that -- all things considered -- are rather simple in their objectives, but get weighted down by the need for explicit joins.
For example this JPQL:
... would become simply:
... or even (HQL with context-inferred
FROM
clause):Further considerations
For example,
name
being mapped to avarchar
column, should we mandate that developers writedocument?.documentType?.name
, ordocument?.documentType.name
? Should we allow both?[]
operator, it could make sense to define a "safe" version there too, i.e.?[]
. That is, depending on whether this operator implies inner joins (??) and whether we decide to allow?.
on non-association attribute access.The text was updated successfully, but these errors were encountered: