Search in sources :

Example 11 with EntityResult

use of org.hibernate.sql.results.graph.entity.EntityResult in project hibernate-orm by hibernate.

the class CriteriaEntityGraphTest 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, 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));
            }
        });
    });
}
Also used : EntityFetchJoinedImpl(org.hibernate.sql.results.graph.entity.internal.EntityFetchJoinedImpl) DelayedCollectionFetch(org.hibernate.sql.results.graph.collection.internal.DelayedCollectionFetch) Fetch(org.hibernate.sql.results.graph.Fetch) EntityFetch(org.hibernate.sql.results.graph.entity.EntityFetch) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Entity(jakarta.persistence.Entity) AssignableMatcher.assignableTo(org.hibernate.testing.hamcrest.AssignableMatcher.assignableTo) EntityPersister(org.hibernate.persister.entity.EntityPersister) GraphSemantic(org.hibernate.graph.GraphSemantic) DelayedCollectionFetch(org.hibernate.sql.results.graph.collection.internal.DelayedCollectionFetch) TableGroupJoin(org.hibernate.sql.ast.tree.from.TableGroupJoin) EntityValuedModelPart(org.hibernate.metamodel.mapping.EntityValuedModelPart) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) TestForIssue(org.hibernate.testing.TestForIssue) SqmQueryImplementor(org.hibernate.query.hql.spi.SqmQueryImplementor) SessionFactoryScopeAware(org.hibernate.testing.orm.junit.SessionFactoryScopeAware) Map(java.util.Map) EntityResult(org.hibernate.sql.results.graph.entity.EntityResult) FetchType(jakarta.persistence.FetchType) Fetchable(org.hibernate.sql.results.graph.Fetchable) EntityDelayedFetchImpl(org.hibernate.sql.results.graph.entity.internal.EntityDelayedFetchImpl) SqmSelectStatement(org.hibernate.query.sqm.tree.select.SqmSelectStatement) CollectionUtils(org.junit.platform.commons.util.CollectionUtils) LazyTableGroup(org.hibernate.sql.ast.tree.from.LazyTableGroup) Collection(java.util.Collection) DomainResult(org.hibernate.sql.results.graph.DomainResult) Set(java.util.Set) Id(jakarta.persistence.Id) DomainModel(org.hibernate.testing.orm.junit.DomainModel) QuerySqmImpl(org.hibernate.query.sqm.internal.QuerySqmImpl) Collectors(java.util.stream.Collectors) EmbeddableFetchImpl(org.hibernate.sql.results.graph.embeddable.internal.EmbeddableFetchImpl) SelectStatement(org.hibernate.sql.ast.tree.select.SelectStatement) Test(org.junit.jupiter.api.Test) LoadQueryInfluencers(org.hibernate.engine.spi.LoadQueryInfluencers) List(java.util.List) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) FromClause(org.hibernate.sql.ast.tree.from.FromClause) EmbeddedAttributeMapping(org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping) OneToMany(jakarta.persistence.OneToMany) ManyToOne(jakarta.persistence.ManyToOne) HashMap(java.util.HashMap) EnumSource(org.junit.jupiter.params.provider.EnumSource) Embedded(jakarta.persistence.Embedded) RootGraphImplementor(org.hibernate.graph.spi.RootGraphImplementor) QueryImplementor(org.hibernate.query.spi.QueryImplementor) CollectionMatchers.hasSize(org.hibernate.testing.hamcrest.CollectionMatchers.hasSize) StandardSqmTranslator(org.hibernate.query.sqm.sql.internal.StandardSqmTranslator) Embeddable(jakarta.persistence.Embeddable) CollectionMatchers.isEmpty(org.hibernate.testing.hamcrest.CollectionMatchers.isEmpty) ElementCollection(jakarta.persistence.ElementCollection) SqmTranslation(org.hibernate.query.sqm.sql.SqmTranslation) Fetch(org.hibernate.sql.results.graph.Fetch) Consumer(java.util.function.Consumer) EntityFetch(org.hibernate.sql.results.graph.entity.EntityFetch) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery) StandardVirtualTableGroup(org.hibernate.sql.ast.tree.from.StandardVirtualTableGroup) SessionFactoryScope(org.hibernate.testing.orm.junit.SessionFactoryScope) CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) SessionFactory(org.hibernate.testing.orm.junit.SessionFactory) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) EntityFetchJoinedImpl(org.hibernate.sql.results.graph.entity.internal.EntityFetchJoinedImpl) SqmSelectStatement(org.hibernate.query.sqm.tree.select.SqmSelectStatement) SelectStatement(org.hibernate.sql.ast.tree.select.SelectStatement) HashMap(java.util.HashMap) EntityResult(org.hibernate.sql.results.graph.entity.EntityResult) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 12 with EntityResult

use of org.hibernate.sql.results.graph.entity.EntityResult in project hibernate-orm by hibernate.

the class CriteriaEntityGraphTest method assertDomainResult.

// util methods for verifying 'domain-result' graph ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private void assertDomainResult(SelectStatement sqlAst, Class<?> expectedEntityJpaClass, String expectedAttributeName, Class<?> expectedAttributeEntityJpaClass, Consumer<EntityFetch> entityFetchConsumer) {
    assertThat(sqlAst.getDomainResultDescriptors(), hasSize(1));
    final DomainResult domainResult = sqlAst.getDomainResultDescriptors().get(0);
    assertThat(domainResult, instanceOf(EntityResult.class));
    final EntityResult entityResult = (EntityResult) domainResult;
    assertThat(entityResult.getReferencedModePart().getJavaType().getJavaTypeClass(), assignableTo(expectedEntityJpaClass));
    assertThat(entityResult.getFetches(), hasSize(1));
    final Fetch fetch = entityResult.getFetches().get(0);
    assertThat(fetch, instanceOf(EntityFetch.class));
    final EntityFetch entityFetch = (EntityFetch) fetch;
    assertThat(entityFetch.getFetchedMapping().getFetchableName(), is(expectedAttributeName));
    assertThat(entityFetch.getReferencedModePart().getJavaType().getJavaTypeClass(), assignableTo(expectedAttributeEntityJpaClass));
    entityFetchConsumer.accept(entityFetch);
}
Also used : DelayedCollectionFetch(org.hibernate.sql.results.graph.collection.internal.DelayedCollectionFetch) Fetch(org.hibernate.sql.results.graph.Fetch) EntityFetch(org.hibernate.sql.results.graph.entity.EntityFetch) EntityFetch(org.hibernate.sql.results.graph.entity.EntityFetch) DomainResult(org.hibernate.sql.results.graph.DomainResult) EntityResult(org.hibernate.sql.results.graph.entity.EntityResult)

Example 13 with EntityResult

use of org.hibernate.sql.results.graph.entity.EntityResult in project hibernate-orm by hibernate.

the class HqlEntityGraphTest method assertDomainResult.

// util methods for verifying 'domain-result' graph ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private void assertDomainResult(SelectStatement sqlAst, Class<?> expectedEntityJpaClass, String expectedAttributeName, Class<?> expectedAttributeEntityJpaClass, Consumer<EntityFetch> entityFetchConsumer) {
    assertThat(sqlAst.getDomainResultDescriptors(), hasSize(1));
    final DomainResult domainResult = sqlAst.getDomainResultDescriptors().get(0);
    assertThat(domainResult, instanceOf(EntityResult.class));
    final EntityResult entityResult = (EntityResult) domainResult;
    assertThat(entityResult.getReferencedModePart().getJavaType().getJavaTypeClass(), assignableTo(expectedEntityJpaClass));
    assertThat(entityResult.getFetches(), hasSize(1));
    final Fetch fetch = entityResult.getFetches().get(0);
    assertThat(fetch, instanceOf(EntityFetch.class));
    final EntityFetch entityFetch = (EntityFetch) fetch;
    assertThat(entityFetch.getFetchedMapping().getFetchableName(), is(expectedAttributeName));
    assertThat(entityFetch.getReferencedModePart().getJavaType().getJavaTypeClass(), assignableTo(expectedAttributeEntityJpaClass));
    entityFetchConsumer.accept(entityFetch);
}
Also used : DelayedCollectionFetch(org.hibernate.sql.results.graph.collection.internal.DelayedCollectionFetch) Fetch(org.hibernate.sql.results.graph.Fetch) EntityFetch(org.hibernate.sql.results.graph.entity.EntityFetch) EntityFetch(org.hibernate.sql.results.graph.entity.EntityFetch) DomainResult(org.hibernate.sql.results.graph.DomainResult) EntityResult(org.hibernate.sql.results.graph.entity.EntityResult)

Example 14 with EntityResult

use of org.hibernate.sql.results.graph.entity.EntityResult in project hibernate-orm by hibernate.

the class EntityGraphLoadPlanBuilderTest method assertDomainResult.

// util methods for verifying 'domain-result' graph ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private void assertDomainResult(SelectStatement sqlAst, Class<?> expectedEntityJpaClass, String expectedAttributeName, Class<?> expectedAttributeEntityJpaClass, Consumer<EntityFetch> entityFetchConsumer) {
    assertThat(sqlAst.getDomainResultDescriptors(), hasSize(1));
    final DomainResult domainResult = sqlAst.getDomainResultDescriptors().get(0);
    assertThat(domainResult, instanceOf(EntityResult.class));
    final EntityResult entityResult = (EntityResult) domainResult;
    assertThat(entityResult.getReferencedModePart().getJavaType().getJavaTypeClass(), assignableTo(expectedEntityJpaClass));
    assertThat(entityResult.getFetches(), hasSize(1));
    final Fetch fetch = entityResult.getFetches().get(0);
    assertThat(fetch, instanceOf(EntityFetch.class));
    final EntityFetch entityFetch = (EntityFetch) fetch;
    assertThat(entityFetch.getFetchedMapping().getFetchableName(), is(expectedAttributeName));
    assertThat(entityFetch.getReferencedModePart().getJavaType().getJavaTypeClass(), assignableTo(expectedAttributeEntityJpaClass));
    entityFetchConsumer.accept(entityFetch);
}
Also used : DelayedCollectionFetch(org.hibernate.sql.results.graph.collection.internal.DelayedCollectionFetch) Fetch(org.hibernate.sql.results.graph.Fetch) EntityFetch(org.hibernate.sql.results.graph.entity.EntityFetch) EntityFetch(org.hibernate.sql.results.graph.entity.EntityFetch) DomainResult(org.hibernate.sql.results.graph.DomainResult) EntityResult(org.hibernate.sql.results.graph.entity.EntityResult)

Example 15 with EntityResult

use of org.hibernate.sql.results.graph.entity.EntityResult in project hibernate-orm by hibernate.

the class LoadPlanBuilderTest method testSimpleBuild.

@Test
public void testSimpleBuild(SessionFactoryScope scope) {
    final SessionFactoryImplementor sessionFactory = scope.getSessionFactory();
    final EntityPersister entityDescriptor = sessionFactory.getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor(Message.class);
    final SingleIdEntityLoaderStandardImpl<?> loader = new SingleIdEntityLoaderStandardImpl<>(entityDescriptor, sessionFactory);
    final SingleIdLoadPlan<?> loadPlan = loader.resolveLoadPlan(LockOptions.READ, LoadQueryInfluencers.NONE, sessionFactory);
    final List<DomainResult<?>> domainResults = loadPlan.getJdbcSelect().getJdbcValuesMappingProducer().resolve(null, sessionFactory).getDomainResults();
    assertThat(domainResults).hasSize(1);
    final DomainResult<?> domainResult = domainResults.get(0);
    assertThat(domainResult).isInstanceOf(EntityResult.class);
    final EntityResult entityResult = (EntityResult) domainResult;
    assertThat(entityResult.getFetches()).hasSize(2);
    final Fetch txtFetch = entityResult.getFetches().get(0);
    assertThat(txtFetch).isNotNull();
    assertThat(txtFetch.getFetchedMapping().getFetchableName()).isEqualTo("msgTxt");
    assertThat(txtFetch.getTiming()).isEqualTo(FetchTiming.IMMEDIATE);
    final Fetch posterFetch = entityResult.getFetches().get(1);
    assertThat(posterFetch).isNotNull();
    assertThat(posterFetch.getFetchedMapping().getFetchableName()).isEqualTo("poster");
    assertThat(posterFetch.getTiming()).isEqualTo(FetchTiming.DELAYED);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) Fetch(org.hibernate.sql.results.graph.Fetch) SingleIdEntityLoaderStandardImpl(org.hibernate.loader.ast.internal.SingleIdEntityLoaderStandardImpl) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) CollectionDomainResult(org.hibernate.sql.results.graph.collection.internal.CollectionDomainResult) DomainResult(org.hibernate.sql.results.graph.DomainResult) EntityResult(org.hibernate.sql.results.graph.entity.EntityResult) Test(org.junit.jupiter.api.Test)

Aggregations

EntityResult (org.hibernate.sql.results.graph.entity.EntityResult)16 DomainResult (org.hibernate.sql.results.graph.DomainResult)14 Fetch (org.hibernate.sql.results.graph.Fetch)13 DelayedCollectionFetch (org.hibernate.sql.results.graph.collection.internal.DelayedCollectionFetch)10 EntityPersister (org.hibernate.persister.entity.EntityPersister)9 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)9 EntityFetch (org.hibernate.sql.results.graph.entity.EntityFetch)9 Test (org.junit.jupiter.api.Test)9 HashMap (java.util.HashMap)7 LoadQueryInfluencers (org.hibernate.engine.spi.LoadQueryInfluencers)7 TableGroupJoin (org.hibernate.sql.ast.tree.from.TableGroupJoin)7 SelectStatement (org.hibernate.sql.ast.tree.select.SelectStatement)7 ElementCollection (jakarta.persistence.ElementCollection)6 Embeddable (jakarta.persistence.Embeddable)6 Embedded (jakarta.persistence.Embedded)6 Entity (jakarta.persistence.Entity)6 FetchType (jakarta.persistence.FetchType)6 Id (jakarta.persistence.Id)6 ManyToOne (jakarta.persistence.ManyToOne)6 OneToMany (jakarta.persistence.OneToMany)6