You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
At the moment you have to write X times the same JPQL query if you want to have X different entitygraphs.
It would be great to enable dynamic EntityGraph with a special parameter in the repositories interfaces.
My suggestion would be to have a new special parameter (like Pageable and Sort) using JpaEntityGraph.
When this parameter is present, it would add the hint to the EntityManager with the entity graph to use.
Add a special parameter (handling the parameter index and so on) :
package org.springframework.data.repository.query;
public class Parameter {
static final List<Class<?>> TYPES = Arrays.asList(Pageable.class, Sort.class, JpaEntityGraph.class);
[...]
}
In AbstractJpaQuery, add the hint to the query thanks to JpaParametersParameterAccessor :
protected Query createQuery(JpaParametersParameterAccessor parameters) {
return applyLockMode(applyEntityGraphConfiguration(applyHints(doCreateQuery(parameters), method), method, parameters), method);
}
private Query applyEntityGraphConfiguration(Query query, JpaQueryMethod method, JpaParametersParameterAccessor accessor) {
if (method.getEntityGraph() != null && accessor.getJpaEntityGraph() != null) {
throw new IllegalStateException("Cannot have EntityGraph annotation and JpaEntityGraph parameter at the same time!");
}
JpaEntityGraph entityGraph = method.getEntityGraph();
if (entityGraph == null) {
entityGraph = accessor.getJpaEntityGraph();
}
if (entityGraph != null) {
Map<String, Object> hints = Jpa21Utils.tryGetFetchGraphHints(em, entityGraph , getQueryMethod().getEntityInformation().getJavaType());
for (Map.Entry<String, Object> hint : hints.entrySet()) {
query.setHint(hint.getKey(), hint.getValue());
}
}
else if (entityGraphName != null) {
// Maybe here use Jpa21Utils to make additional checks
query.setHint(hint.getKey(), em.getEntityGraph(entityGraphName.getValue()));
}
return query;
}
Then we can use different entity graphs depending on the caller's context :
// Assuming we have a new JpaEntityGraph constructor with name only (defaulting the type to FETCH like the annotation)
myRepository.findByMyProp("something", new JpaEntityGraph("myentity-with-some-nodes"));
myRepository.findByMyProp("something", new JpaEntityGraph("myentity-with-other-nodes"));
The text was updated successfully, but these errors were encountered:
JordanDurieux
changed the title
Enable dynamic NamedEntityGraph in repositories
Allow dynamic NamedEntityGraph in repositories
Jun 1, 2022
Uh oh!
There was an error while loading. Please reload this page.
At the moment you have to write X times the same JPQL query if you want to have X different entitygraphs.
It would be great to enable dynamic EntityGraph with a special parameter in the repositories interfaces.
My suggestion would be to have a new special parameter (like Pageable and Sort) using JpaEntityGraph.
When this parameter is present, it would add the hint to the EntityManager with the entity graph to use.
Add a special parameter (handling the parameter index and so on) :
In
AbstractJpaQuery
, add the hint to the query thanks toJpaParametersParameterAccessor
:The usage in repositories would be :
Then we can use different entity graphs depending on the caller's context :
The text was updated successfully, but these errors were encountered: