Search in sources :

Example 1 with AliasResolutionContextImpl

use of org.hibernate.loader.plan.exec.internal.AliasResolutionContextImpl in project hibernate-orm by hibernate.

the class EntityGraphLoadPlanBuilderTest method testBasicLoadLoadPlanBuilding.

/**
	 * EntityGraph(1):
	 *
	 * Cat
	 *
	 * LoadPlan:
	 *
	 * Cat
	 *
	 * ---------------------
	 *
	 * EntityGraph(2):
	 *
	 * Cat
	 * owner -- Person
	 *
	 * LoadPlan:
	 *
	 * Cat
	 * owner -- Person
	 * address --- Address
	 * country -- Country
	 */
@Test
public void testBasicLoadLoadPlanBuilding() {
    EntityManager em = getOrCreateEntityManager();
    EntityGraph eg = em.createEntityGraph(Cat.class);
    LoadPlan plan = buildLoadPlan(eg, Mode.LOAD, Cat.class);
    LoadPlanTreePrinter.INSTANCE.logTree(plan, new AliasResolutionContextImpl(sfi()));
    QuerySpace rootQuerySpace = plan.getQuerySpaces().getRootQuerySpaces().get(0);
    assertFalse("With fetchgraph property and an empty EntityGraph, there should be no join at all", rootQuerySpace.getJoins().iterator().hasNext());
    // -------------------------------------------------- another a little more complicated case
    eg = em.createEntityGraph(Cat.class);
    eg.addSubgraph("owner", Person.class);
    plan = buildLoadPlan(eg, Mode.LOAD, Cat.class);
    LoadPlanTreePrinter.INSTANCE.logTree(plan, new AliasResolutionContextImpl(sfi()));
    rootQuerySpace = plan.getQuerySpaces().getRootQuerySpaces().get(0);
    Iterator<Join> iterator = rootQuerySpace.getJoins().iterator();
    assertTrue("With fetchgraph property and an empty EntityGraph, there should be no join at all", iterator.hasNext());
    Join personJoin = iterator.next();
    assertNotNull(personJoin);
    QuerySpace.Disposition disposition = personJoin.getRightHandSide().getDisposition();
    assertEquals("This should be an entity join which fetches Person", QuerySpace.Disposition.ENTITY, disposition);
    iterator = personJoin.getRightHandSide().getJoins().iterator();
    assertTrue("The composite address should be fetched", iterator.hasNext());
    Join addressJoin = iterator.next();
    assertNotNull(addressJoin);
    disposition = addressJoin.getRightHandSide().getDisposition();
    assertEquals(QuerySpace.Disposition.COMPOSITE, disposition);
    iterator = addressJoin.getRightHandSide().getJoins().iterator();
    assertTrue(iterator.hasNext());
    Join countryJoin = iterator.next();
    assertNotNull(countryJoin);
    disposition = countryJoin.getRightHandSide().getDisposition();
    assertEquals(QuerySpace.Disposition.ENTITY, disposition);
    assertFalse("The ManyToOne attribute in composite should not be fetched", countryJoin.getRightHandSide().getJoins().iterator().hasNext());
    em.close();
}
Also used : EntityGraph(javax.persistence.EntityGraph) AliasResolutionContextImpl(org.hibernate.loader.plan.exec.internal.AliasResolutionContextImpl) EntityManager(javax.persistence.EntityManager) LoadPlan(org.hibernate.loader.plan.spi.LoadPlan) Join(org.hibernate.loader.plan.spi.Join) QuerySpace(org.hibernate.loader.plan.spi.QuerySpace) Test(org.junit.Test)

Example 2 with AliasResolutionContextImpl

use of org.hibernate.loader.plan.exec.internal.AliasResolutionContextImpl in project hibernate-orm by hibernate.

the class EntityGraphLoadPlanBuilderTest method testEmbeddedCollection.

@Test
public void testEmbeddedCollection() {
    EntityManager em = getOrCreateEntityManager();
    EntityGraph eg = em.createEntityGraph(ExpressCompany.class);
    eg.addAttributeNodes("shipAddresses");
    //WTF name!!!
    LoadPlan loadLoadPlan = buildLoadPlan(eg, Mode.LOAD, ExpressCompany.class);
    LoadPlanTreePrinter.INSTANCE.logTree(loadLoadPlan, new AliasResolutionContextImpl(sfi()));
    QuerySpace querySpace = loadLoadPlan.getQuerySpaces().getRootQuerySpaces().iterator().next();
    Iterator<Join> iterator = querySpace.getJoins().iterator();
    assertTrue(iterator.hasNext());
    Join collectionJoin = iterator.next();
    assertEquals(QuerySpace.Disposition.COLLECTION, collectionJoin.getRightHandSide().getDisposition());
    assertFalse(iterator.hasNext());
    iterator = collectionJoin.getRightHandSide().getJoins().iterator();
    assertTrue(iterator.hasNext());
    Join collectionElementJoin = iterator.next();
    assertFalse(iterator.hasNext());
    assertEquals(QuerySpace.Disposition.COMPOSITE, collectionElementJoin.getRightHandSide().getDisposition());
    iterator = collectionElementJoin.getRightHandSide().getJoins().iterator();
    assertTrue(iterator.hasNext());
    Join countryJoin = iterator.next();
    assertFalse(iterator.hasNext());
    assertEquals(QuerySpace.Disposition.ENTITY, countryJoin.getRightHandSide().getDisposition());
    //----------------------------------------------------------------
    LoadPlan fetchLoadPlan = buildLoadPlan(eg, Mode.FETCH, ExpressCompany.class);
    LoadPlanTreePrinter.INSTANCE.logTree(fetchLoadPlan, new AliasResolutionContextImpl(sfi()));
    querySpace = fetchLoadPlan.getQuerySpaces().getRootQuerySpaces().iterator().next();
    iterator = querySpace.getJoins().iterator();
    assertTrue(iterator.hasNext());
    collectionJoin = iterator.next();
    assertEquals(QuerySpace.Disposition.COLLECTION, collectionJoin.getRightHandSide().getDisposition());
    assertFalse(iterator.hasNext());
    iterator = collectionJoin.getRightHandSide().getJoins().iterator();
    assertTrue(iterator.hasNext());
    collectionElementJoin = iterator.next();
    assertFalse(iterator.hasNext());
    assertEquals(QuerySpace.Disposition.COMPOSITE, collectionElementJoin.getRightHandSide().getDisposition());
    iterator = collectionElementJoin.getRightHandSide().getJoins().iterator();
    assertFalse(iterator.hasNext());
    //----------------------------------------------------------------
    em.close();
}
Also used : EntityGraph(javax.persistence.EntityGraph) AliasResolutionContextImpl(org.hibernate.loader.plan.exec.internal.AliasResolutionContextImpl) EntityManager(javax.persistence.EntityManager) LoadPlan(org.hibernate.loader.plan.spi.LoadPlan) Join(org.hibernate.loader.plan.spi.Join) QuerySpace(org.hibernate.loader.plan.spi.QuerySpace) Test(org.junit.Test)

Example 3 with AliasResolutionContextImpl

use of org.hibernate.loader.plan.exec.internal.AliasResolutionContextImpl in project hibernate-orm by hibernate.

the class EntityGraphLoadPlanBuilderTest method testBasicFetchLoadPlanBuilding.

/**
	 * EntityGraph(1):
	 *
	 * Cat
	 *
	 * LoadPlan:
	 *
	 * Cat
	 *
	 * ---------------------
	 *
	 * EntityGraph(2):
	 *
	 * Cat
	 * owner -- Person
	 *
	 * LoadPlan:
	 *
	 * Cat
	 * owner -- Person
	 * address --- Address
	 */
@Test
public void testBasicFetchLoadPlanBuilding() {
    EntityManager em = getOrCreateEntityManager();
    EntityGraph eg = em.createEntityGraph(Cat.class);
    LoadPlan plan = buildLoadPlan(eg, Mode.FETCH, Cat.class);
    LoadPlanTreePrinter.INSTANCE.logTree(plan, new AliasResolutionContextImpl(sfi()));
    QuerySpace rootQuerySpace = plan.getQuerySpaces().getRootQuerySpaces().get(0);
    assertFalse("With fetchgraph property and an empty EntityGraph, there should be no join at all", rootQuerySpace.getJoins().iterator().hasNext());
    // -------------------------------------------------- another a little more complicated case
    eg = em.createEntityGraph(Cat.class);
    eg.addSubgraph("owner", Person.class);
    plan = buildLoadPlan(eg, Mode.FETCH, Cat.class);
    LoadPlanTreePrinter.INSTANCE.logTree(plan, new AliasResolutionContextImpl(sfi()));
    rootQuerySpace = plan.getQuerySpaces().getRootQuerySpaces().get(0);
    Iterator<Join> iterator = rootQuerySpace.getJoins().iterator();
    assertTrue("With fetchgraph property and an empty EntityGraph, there should be no join at all", iterator.hasNext());
    Join personJoin = iterator.next();
    assertNotNull(personJoin);
    QuerySpace.Disposition disposition = personJoin.getRightHandSide().getDisposition();
    assertEquals("This should be an entity join which fetches Person", QuerySpace.Disposition.ENTITY, disposition);
    iterator = personJoin.getRightHandSide().getJoins().iterator();
    assertTrue("The composite address should be fetched", iterator.hasNext());
    Join addressJoin = iterator.next();
    assertNotNull(addressJoin);
    disposition = addressJoin.getRightHandSide().getDisposition();
    assertEquals(QuerySpace.Disposition.COMPOSITE, disposition);
    assertFalse(iterator.hasNext());
    assertFalse("The ManyToOne attribute in composite should not be fetched", addressJoin.getRightHandSide().getJoins().iterator().hasNext());
    em.close();
}
Also used : EntityGraph(javax.persistence.EntityGraph) AliasResolutionContextImpl(org.hibernate.loader.plan.exec.internal.AliasResolutionContextImpl) EntityManager(javax.persistence.EntityManager) LoadPlan(org.hibernate.loader.plan.spi.LoadPlan) Join(org.hibernate.loader.plan.spi.Join) QuerySpace(org.hibernate.loader.plan.spi.QuerySpace) Test(org.junit.Test)

Example 4 with AliasResolutionContextImpl

use of org.hibernate.loader.plan.exec.internal.AliasResolutionContextImpl in project hibernate-orm by hibernate.

the class EntityGraphLoadPlanBuilderTest method testBasicElementCollections.

@Test
public void testBasicElementCollections() {
    EntityManager em = getOrCreateEntityManager();
    EntityGraph eg = em.createEntityGraph(Dog.class);
    eg.addAttributeNodes("favorites");
    //WTF name!!!
    LoadPlan loadLoadPlan = buildLoadPlan(eg, Mode.LOAD, Dog.class);
    LoadPlanTreePrinter.INSTANCE.logTree(loadLoadPlan, new AliasResolutionContextImpl(sfi()));
    QuerySpace querySpace = loadLoadPlan.getQuerySpaces().getRootQuerySpaces().iterator().next();
    Iterator<Join> iterator = querySpace.getJoins().iterator();
    assertTrue(iterator.hasNext());
    Join collectionJoin = iterator.next();
    assertEquals(QuerySpace.Disposition.COLLECTION, collectionJoin.getRightHandSide().getDisposition());
    assertFalse(iterator.hasNext());
    //----------------------------------------------------------------
    LoadPlan fetchLoadPlan = buildLoadPlan(eg, Mode.FETCH, Dog.class);
    LoadPlanTreePrinter.INSTANCE.logTree(fetchLoadPlan, new AliasResolutionContextImpl(sfi()));
    querySpace = fetchLoadPlan.getQuerySpaces().getRootQuerySpaces().iterator().next();
    iterator = querySpace.getJoins().iterator();
    assertTrue(iterator.hasNext());
    collectionJoin = iterator.next();
    assertEquals(QuerySpace.Disposition.COLLECTION, collectionJoin.getRightHandSide().getDisposition());
    assertFalse(iterator.hasNext());
    em.close();
}
Also used : EntityGraph(javax.persistence.EntityGraph) AliasResolutionContextImpl(org.hibernate.loader.plan.exec.internal.AliasResolutionContextImpl) EntityManager(javax.persistence.EntityManager) LoadPlan(org.hibernate.loader.plan.spi.LoadPlan) Join(org.hibernate.loader.plan.spi.Join) QuerySpace(org.hibernate.loader.plan.spi.QuerySpace) Test(org.junit.Test)

Example 5 with AliasResolutionContextImpl

use of org.hibernate.loader.plan.exec.internal.AliasResolutionContextImpl in project hibernate-orm by hibernate.

the class LoadPlanBuilderTest method testSimpleBuild.

@Test
public void testSimpleBuild() {
    EntityPersister ep = (EntityPersister) sessionFactory().getClassMetadata(Message.class);
    FetchStyleLoadPlanBuildingAssociationVisitationStrategy strategy = new FetchStyleLoadPlanBuildingAssociationVisitationStrategy(sessionFactory(), LoadQueryInfluencers.NONE, LockMode.NONE);
    LoadPlan plan = MetamodelDrivenLoadPlanBuilder.buildRootEntityLoadPlan(strategy, ep);
    assertFalse(plan.hasAnyScalarReturns());
    assertEquals(1, plan.getReturns().size());
    Return rtn = plan.getReturns().get(0);
    EntityReturn entityReturn = ExtraAssertions.assertTyping(EntityReturn.class, rtn);
    assertNotNull(entityReturn.getFetches());
    assertEquals(1, entityReturn.getFetches().length);
    Fetch fetch = entityReturn.getFetches()[0];
    EntityFetch entityFetch = ExtraAssertions.assertTyping(EntityFetch.class, fetch);
    assertNotNull(entityFetch.getFetches());
    assertEquals(0, entityFetch.getFetches().length);
    LoadPlanTreePrinter.INSTANCE.logTree(plan, new AliasResolutionContextImpl(sessionFactory()));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) Fetch(org.hibernate.loader.plan.spi.Fetch) EntityFetch(org.hibernate.loader.plan.spi.EntityFetch) EntityFetch(org.hibernate.loader.plan.spi.EntityFetch) AliasResolutionContextImpl(org.hibernate.loader.plan.exec.internal.AliasResolutionContextImpl) CollectionReturn(org.hibernate.loader.plan.spi.CollectionReturn) Return(org.hibernate.loader.plan.spi.Return) EntityReturn(org.hibernate.loader.plan.spi.EntityReturn) LoadPlan(org.hibernate.loader.plan.spi.LoadPlan) FetchStyleLoadPlanBuildingAssociationVisitationStrategy(org.hibernate.loader.plan.build.internal.FetchStyleLoadPlanBuildingAssociationVisitationStrategy) EntityReturn(org.hibernate.loader.plan.spi.EntityReturn) Test(org.junit.Test)

Aggregations

AliasResolutionContextImpl (org.hibernate.loader.plan.exec.internal.AliasResolutionContextImpl)7 LoadPlan (org.hibernate.loader.plan.spi.LoadPlan)7 Test (org.junit.Test)7 EntityGraph (javax.persistence.EntityGraph)4 EntityManager (javax.persistence.EntityManager)4 Join (org.hibernate.loader.plan.spi.Join)4 QuerySpace (org.hibernate.loader.plan.spi.QuerySpace)4 CollectionReturn (org.hibernate.loader.plan.spi.CollectionReturn)3 EntityReturn (org.hibernate.loader.plan.spi.EntityReturn)3 Return (org.hibernate.loader.plan.spi.Return)3 FetchStyleLoadPlanBuildingAssociationVisitationStrategy (org.hibernate.loader.plan.build.internal.FetchStyleLoadPlanBuildingAssociationVisitationStrategy)2 EntityFetch (org.hibernate.loader.plan.spi.EntityFetch)2 Fetch (org.hibernate.loader.plan.spi.Fetch)2 EntityPersister (org.hibernate.persister.entity.EntityPersister)2 CascadeStyleLoadPlanBuildingAssociationVisitationStrategy (org.hibernate.loader.plan.build.internal.CascadeStyleLoadPlanBuildingAssociationVisitationStrategy)1 EntityReference (org.hibernate.loader.plan.spi.EntityReference)1 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)1