use of org.hibernate.graph.GraphSemantic in project hibernate-orm by hibernate.
the class EntityGraphLoadPlanBuilderTest method testLoadPlanBuildingWithSubgraph.
@ParameterizedTest
@EnumSource(GraphSemantic.class)
void testLoadPlanBuildingWithSubgraph(GraphSemantic graphSemantic) {
scope.inTransaction(em -> {
final RootGraphImplementor<Cat> eg = em.createEntityGraph(Cat.class);
eg.addSubgraph("owner", Person.class);
final SelectStatement sqlAst = buildSqlSelectAst(Cat.class, eg, graphSemantic, scope);
// Check the from-clause
assertEntityValuedJoinedGroup(sqlAst, "owner", Person.class, this::assertPersonHomeAddressJoinedGroup);
// Check the domain-result graph
assertDomainResult(sqlAst, Cat.class, "owner", Person.class, entityFetch -> {
if (graphSemantic == GraphSemantic.LOAD) {
assertThat(entityFetch, instanceOf(EntityFetchJoinedImpl.class));
final EntityResult entityResult = ((EntityFetchJoinedImpl) entityFetch).getEntityResult();
final Map<String, Class<? extends Fetch>> fetchClassByAttributeName = entityResult.getFetches().stream().collect(Collectors.toMap(fetch -> fetch.getFetchedMapping().getPartName(), Fetch::getClass));
final Map<String, Class<? extends Fetch>> expectedFetchClassByAttributeName = new HashMap<>();
expectedFetchClassByAttributeName.put("pets", DelayedCollectionFetch.class);
expectedFetchClassByAttributeName.put("homeAddress", EmbeddableFetchImpl.class);
expectedFetchClassByAttributeName.put("company", EntityDelayedFetchImpl.class);
assertThat(fetchClassByAttributeName, is(expectedFetchClassByAttributeName));
}
});
});
}
use of org.hibernate.graph.GraphSemantic in project hibernate-orm by hibernate.
the class HqlEntityGraphTest method testSemanticsWithSubgraph.
@ParameterizedTest
@EnumSource(GraphSemantic.class)
void testSemanticsWithSubgraph(GraphSemantic graphSemantic) {
scope.inTransaction(session -> {
final RootGraphImplementor<Cat> eg = session.createEntityGraph(Cat.class);
eg.addSubgraph("owner", Person.class);
final SelectStatement sqlAst = buildSqlSelectAst(Cat.class, "select c from Cat as c", eg, graphSemantic, session);
// Check the from-clause
assertEntityValuedJoinedGroup(sqlAst, "owner", Person.class, this::assertPersonHomeAddressJoinedGroup);
// Check the domain-result graph
assertDomainResult(sqlAst, Cat.class, "owner", Person.class, entityFetch -> {
if (graphSemantic == GraphSemantic.LOAD) {
assertThat(entityFetch, instanceOf(EntityFetchJoinedImpl.class));
final EntityResult entityResult = ((EntityFetchJoinedImpl) entityFetch).getEntityResult();
final Map<String, Class<? extends Fetch>> fetchClassByAttributeName = entityResult.getFetches().stream().collect(Collectors.toMap(fetch -> fetch.getFetchedMapping().getPartName(), Fetch::getClass));
final Map<String, Class<? extends Fetch>> expectedFetchClassByAttributeName = new HashMap<>();
expectedFetchClassByAttributeName.put("pets", DelayedCollectionFetch.class);
expectedFetchClassByAttributeName.put("homeAddress", EmbeddableFetchImpl.class);
expectedFetchClassByAttributeName.put("company", EntityDelayedFetchImpl.class);
assertThat(fetchClassByAttributeName, is(expectedFetchClassByAttributeName));
}
});
});
}
use of org.hibernate.graph.GraphSemantic in project hibernate-orm by hibernate.
the class SessionImpl method internalLoad.
@Override
public Object internalLoad(String entityName, Object id, boolean eager, boolean nullable) {
final EffectiveEntityGraph effectiveEntityGraph = getLoadQueryInfluencers().getEffectiveEntityGraph();
final GraphSemantic semantic = effectiveEntityGraph.getSemantic();
final RootGraphImplementor<?> graph = effectiveEntityGraph.getGraph();
boolean clearedEffectiveGraph = false;
if (semantic != null) {
if (!graph.appliesTo(entityName)) {
log.debug("Clearing effective entity graph for subsequent-select");
clearedEffectiveGraph = true;
effectiveEntityGraph.clear();
}
}
try {
final LoadType type;
if (nullable) {
type = LoadEventListener.INTERNAL_LOAD_NULLABLE;
} else {
type = eager ? LoadEventListener.INTERNAL_LOAD_EAGER : LoadEventListener.INTERNAL_LOAD_LAZY;
}
LoadEvent event = loadEvent;
loadEvent = null;
event = recycleEventInstance(event, id, entityName);
fireLoadNoChecks(event, type);
Object result = event.getResult();
if (!nullable) {
UnresolvableObjectException.throwIfNull(result, id, entityName);
}
if (loadEvent == null) {
event.setEntityClassName(null);
event.setEntityId(null);
event.setInstanceToLoad(null);
event.setResult(null);
loadEvent = event;
}
return result;
} finally {
if (clearedEffectiveGraph) {
effectiveEntityGraph.applyGraph(graph, semantic);
}
}
}
use of org.hibernate.graph.GraphSemantic in project hibernate-orm by hibernate.
the class LoaderSelectBuilder method determineGraphTraversalState.
private static EntityGraphTraversalState determineGraphTraversalState(LoadQueryInfluencers loadQueryInfluencers) {
if (loadQueryInfluencers != null) {
final EffectiveEntityGraph effectiveEntityGraph = loadQueryInfluencers.getEffectiveEntityGraph();
if (effectiveEntityGraph != null) {
final GraphSemantic graphSemantic = effectiveEntityGraph.getSemantic();
final RootGraphImplementor rootGraphImplementor = effectiveEntityGraph.getGraph();
if (graphSemantic != null && rootGraphImplementor != null) {
return new StandardEntityGraphTraversalStateImpl(graphSemantic, rootGraphImplementor);
}
}
}
return null;
}
use of org.hibernate.graph.GraphSemantic in project hibernate-orm by hibernate.
the class NaturalIdMultiLoadAccessStandard method multiLoad.
@Override
@SuppressWarnings("unchecked")
public List<T> multiLoad(Object... ids) {
final CacheMode sessionCacheMode = session.getCacheMode();
boolean cacheModeChanged = false;
if (cacheMode != null) {
// todo : account for "conceptually equal"
if (cacheMode != sessionCacheMode) {
session.setCacheMode(cacheMode);
cacheModeChanged = true;
}
}
session.autoFlushIfRequired((Set) CollectionHelper.setOf(entityDescriptor.getQuerySpaces()));
final LoadQueryInfluencers loadQueryInfluencers = session.getLoadQueryInfluencers();
try {
final EffectiveEntityGraph effectiveEntityGraph = loadQueryInfluencers.getEffectiveEntityGraph();
final GraphSemantic initialGraphSemantic = effectiveEntityGraph.getSemantic();
final RootGraphImplementor<?> initialGraph = effectiveEntityGraph.getGraph();
final boolean hadInitialGraph = initialGraphSemantic != null;
if (graphSemantic != null) {
if (rootGraph == null) {
throw new IllegalArgumentException("Graph semantic specified, but no RootGraph was supplied");
}
effectiveEntityGraph.applyGraph(rootGraph, graphSemantic);
}
try {
return (List<T>) entityDescriptor.getMultiNaturalIdLoader().multiLoad(ids, this, session);
} finally {
if (graphSemantic != null) {
if (hadInitialGraph) {
effectiveEntityGraph.applyGraph(initialGraph, initialGraphSemantic);
} else {
effectiveEntityGraph.clear();
}
}
}
} finally {
if (cacheModeChanged) {
// change it back
session.setCacheMode(sessionCacheMode);
}
}
}
Aggregations