-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
HHH-19096 Adjust SelectionQuery#setEntityGraph(..)
to accept entity graphs of supertypes
#9690
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @marko-bekhta! Would be great if we could add a test for this though, just to ensure that 1. setEntityGraph
accepts graphs of subtypes of the queried entity and 2. said graphs work as expected.
hibernate-core/src/main/java/org/hibernate/query/spi/AbstractSelectionQuery.java
Outdated
Show resolved
Hide resolved
Are you sure this makes sense? Usually an |
c9c5b1c
to
4929218
Compare
default SelectionQuery<R> setNamedEntityGraph(String graphName, GraphSemantic semantic) { | ||
EntityGraph<? super R> namedEntityGraph = getNamedEntityGraph( graphName ); | ||
if ( namedEntityGraph == null ) { | ||
throw new IllegalArgumentException( "No entity graph found for name " + graphName ); | ||
} | ||
return setEntityGraph( namedEntityGraph, semantic ); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this felt like a more user-friendly way to apply that graph, but if you don't like it I'll drop it 😃
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess I'm not very keen on it because it pushes people toward using string-based names whereas we're actively trying to discourage them from using strings.
What we really want them to do is use Entity_._NamedGraph
to get a typesafe reference to the named entity graph from the static metamodel. We prefer that they don't use these string-based lookups.
So, while I'm fine with adding one new operation I'm not sure we want to bloat out the very important SelectionQuery
interface with operations which only streamline the untypesafe usage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤷🏻 😃 it's just when I was adding that test example:
SelectionQuery<Aa> query = s.createSelectionQuery( "from Aa", Aa.class );
query
.setEntityGraph(
// here:
query.getNamedEntityGraph( "a-graph" ), GraphSemantic.FETCH )
.getResultList();
above looks a bit odd to pass something to a query by getting it from the very same query.
IDK how often people generate the static metamodel in their projects, but if "this is the way" 😃 I'm ok with reverting this bit 😃 just let me know
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
above looks a bit odd to pass something to a query by getting it from the very same query.
Yes I understand that.
IDK how often people generate the static metamodel in their projects, but if "this is the way"
It's the new way in JPA 3.2. We want to encourage them to use it.
4929218
to
514df55
Compare
@@ -273,7 +273,7 @@ default Stream<R> stream() { | |||
* @param graph The graph to apply. | |||
* @param semantic The semantic to use when applying the graph | |||
* | |||
* @deprecated Use {@link #setEntityGraph(EntityGraph, GraphSemantic)} | |||
* @deprecated Use {@link SelectionQuery#setEntityGraph(EntityGraph, GraphSemantic)} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you make these changes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😖🙈 hmm, I thought I've reverted all of these (IDE made the change when I updated the method signature through "refactoring" 🤦🏻 ) will revert these
514df55
to
2ed4355
Compare
SelectionQuery#setEntityGraph(..)
to accept entity graphs of subtypesSelectionQuery#setEntityGraph(..)
to accept entity graphs of supertypes
2ed4355
to
5b9bd0f
Compare
@marko-bekhta a second option, which has some downsides, and which I'm not claiming is better, is to add the following method to <R> Query<R> createQuery(String queryString, EntityGraph<R> resultClass); This is a bit more consistent with Here's what it looks like to use this method: var query = session.createQuery( "from Client",
session.getFactory().getNamedEntityGraphs( Client.class )
.get( "MyClientGraph" ) );
for ( Client client : query.getResultList() ) {
String name = client.getName();
...
} and according to an pre-existing proposal for JPA 4, this would simplify to: var query = session.createQuery( "from Client",
session.getNamedEntityGraphs( Client.class ).get( "MyClientGraph" ) );
for ( Client client : query.getResultList() ) {
String name = client.getName();
...
} which doesn't compare that unfavorably to what we have in this PR: var query = session.createQuery( "from Client", Client.class );
query.setEntityGraph( query.getNamedEntityGraph( "MyClientGraph" ) );
for ( Client client : query.getResultList() ) {
String name = client.getName();
...
} |
How about we generate static fields for known entity graphs in the annotation processor and inject proxy implementations that disallow mutations? Then this could be as simple as this: var query = session.createQuery( "from Client", Client_.MyClientGraph ); |
TLDR: let's merge the I went back to see what Search needed, as it's been a while 😃. Entity graphs affect internals, API ( but indirectly) and tests. For internals, we might be building either a criteria or hql queries, and all we really need there is this change ( Query<R> setEntityGraph(EntityGraph<? super R> graph, GraphSemantic semantic); For API, we let the user get an "ORM query" from the search one: SearchQuery<IndexedEntity> searchQuery = ...
Query<IndexedEntity> query = Search.toOrmQuery( searchQuery ); so the users could then do
any way they wish 😃 (not a Search problem anymore). Tests: for Search tests, we aren't building the static metamodel, and I wouldn't want to start doing that. I can live with
With that said, the other changes beyond the |
We do already do that. It's a standard feature of JPA 3.2! @marko-bekhta is worried about a different use case. |
FTR, this is what I have currently proposed for JPA 4: |
On the other hand, thinking this through, there are some reasonably good arguments for adding an overload of
@marko-bekhta When we're designing a public-facing API for ORM, we're not primarily thinking about what Search needs, OK? |
Actually I think whatever we do with this proposal, we should add that overload anyway. |
… graphs of supertypes
5b9bd0f
to
56b507c
Compare
I've updated this PR to only include the |
LGTM |
@marko-bekhta I would say you can just go ahead and merge this now |
ok, thanks! 🥳 |
https://hibernate.atlassian.net/browse/HHH-19096
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license
and can be relicensed under the terms of the LGPL v2.1 license in the future at the maintainers' discretion.
For more information on licensing, please check here.