Search in sources :

Example 1 with BidirectionalEntityReference

use of org.hibernate.loader.plan.spi.BidirectionalEntityReference in project hibernate-orm by hibernate.

the class AbstractExpandingFetchSource method buildBidirectionalEntityReference.

@Override
public BidirectionalEntityReference buildBidirectionalEntityReference(AssociationAttributeDefinition attributeDefinition, FetchStrategy fetchStrategy, EntityReference targetEntityReference) {
    final EntityType fetchedType = (EntityType) attributeDefinition.getType();
    final EntityPersister fetchedPersister = attributeDefinition.toEntityDefinition().getEntityPersister();
    if (fetchedPersister == null) {
        throw new WalkingException(String.format("Unable to locate EntityPersister [%s] for bidirectional entity reference [%s]", fetchedType.getAssociatedEntityName(), attributeDefinition.getName()));
    }
    final BidirectionalEntityReference bidirectionalEntityReference = new BidirectionalEntityReferenceImpl(this, attributeDefinition, targetEntityReference);
    addBidirectionalEntityReference(bidirectionalEntityReference);
    return bidirectionalEntityReference;
}
Also used : EntityType(org.hibernate.type.EntityType) EntityPersister(org.hibernate.persister.entity.EntityPersister) WalkingException(org.hibernate.persister.walking.spi.WalkingException) BidirectionalEntityReference(org.hibernate.loader.plan.spi.BidirectionalEntityReference)

Example 2 with BidirectionalEntityReference

use of org.hibernate.loader.plan.spi.BidirectionalEntityReference in project hibernate-orm by hibernate.

the class AbstractRowReader method resolveEntityKey.

private void resolveEntityKey(ResultSet resultSet, ResultSetProcessingContextImpl context, FetchSource fetchSource) throws SQLException {
    // Resolve any bidirectional entity references first.
    for (BidirectionalEntityReference bidirectionalEntityReference : fetchSource.getBidirectionalEntityReferences()) {
        final EntityReferenceInitializer targetEntityReferenceInitializer = entityInitializerByEntityReference.get(bidirectionalEntityReference.getTargetEntityReference());
        resolveEntityKey(resultSet, context, targetEntityReferenceInitializer);
        targetEntityReferenceInitializer.hydrateEntityState(resultSet, context);
    }
    for (Fetch fetch : fetchSource.getFetches()) {
        if (EntityFetch.class.isInstance(fetch)) {
            final EntityFetch entityFetch = (EntityFetch) fetch;
            final EntityReferenceInitializer entityReferenceInitializer = entityInitializerByEntityReference.get(entityFetch);
            if (entityReferenceInitializer != null) {
                resolveEntityKey(resultSet, context, entityReferenceInitializer);
                entityReferenceInitializer.hydrateEntityState(resultSet, context);
            }
        } else if (CompositeFetch.class.isInstance(fetch)) {
            resolveEntityKey(resultSet, context, (CompositeFetch) fetch);
        }
    }
}
Also used : CompositeFetch(org.hibernate.loader.plan.spi.CompositeFetch) Fetch(org.hibernate.loader.plan.spi.Fetch) EntityFetch(org.hibernate.loader.plan.spi.EntityFetch) EntityFetch(org.hibernate.loader.plan.spi.EntityFetch) CompositeFetch(org.hibernate.loader.plan.spi.CompositeFetch) BidirectionalEntityReference(org.hibernate.loader.plan.spi.BidirectionalEntityReference) EntityReferenceInitializer(org.hibernate.loader.plan.exec.process.spi.EntityReferenceInitializer)

Example 3 with BidirectionalEntityReference

use of org.hibernate.loader.plan.spi.BidirectionalEntityReference in project hibernate-orm by hibernate.

the class LoadPlanStructureAssertionTest method testEncapsulatedCompositeIdWithFetches2.

@Test
public void testEncapsulatedCompositeIdWithFetches2() {
    Configuration cfg = new Configuration();
    cfg.addAnnotatedClass(Card.class);
    cfg.addAnnotatedClass(CardField.class);
    cfg.addAnnotatedClass(Key.class);
    cfg.addAnnotatedClass(PrimaryKey.class);
    final SessionFactoryImplementor sf = (SessionFactoryImplementor) cfg.buildSessionFactory();
    try {
        final OuterJoinLoadable cardPersister = (OuterJoinLoadable) sf.getClassMetadata(Card.class);
        doCompare(sf, cardPersister);
        final LoadPlan cardLoadPlan = LoadPlanStructureAssertionHelper.INSTANCE.buildLoadPlan(sf, cardPersister);
        assertEquals(LoadPlan.Disposition.ENTITY_LOADER, cardLoadPlan.getDisposition());
        assertEquals(1, cardLoadPlan.getReturns().size());
        // Check the root EntityReturn(Card)
        final EntityReturn cardReturn = assertTyping(EntityReturn.class, cardLoadPlan.getReturns().get(0));
        assertFalse(cardReturn.getIdentifierDescription().hasFetches());
        // Card should have one fetch, the fields collection
        assertEquals(1, cardReturn.getFetches().length);
        final CollectionAttributeFetch fieldsFetch = assertTyping(CollectionAttributeFetch.class, cardReturn.getFetches()[0]);
        assertNotNull(fieldsFetch.getElementGraph());
        // the Card.fields collection has entity elements of type CardField...
        final CollectionFetchableElementEntityGraph cardFieldElementGraph = assertTyping(CollectionFetchableElementEntityGraph.class, fieldsFetch.getElementGraph());
        // CardField should have no fetches
        assertEquals(0, cardFieldElementGraph.getFetches().length);
        // But it should have 1 key-many-to-one fetch for Key (Card is already handled)
        assertTrue(cardFieldElementGraph.getIdentifierDescription().hasFetches());
        final FetchSource cardFieldElementGraphIdAsFetchSource = assertTyping(FetchSource.class, cardFieldElementGraph.getIdentifierDescription());
        assertEquals(1, cardFieldElementGraphIdAsFetchSource.getFetches().length);
        assertEquals(1, cardFieldElementGraphIdAsFetchSource.getBidirectionalEntityReferences().length);
        BidirectionalEntityReference circularCardFetch = assertTyping(BidirectionalEntityReference.class, cardFieldElementGraphIdAsFetchSource.getBidirectionalEntityReferences()[0]);
        assertSame(circularCardFetch.getTargetEntityReference(), cardReturn);
        // the fetch above is to the other key-many-to-one for CardField.primaryKey composite: key
        EntityFetch keyFetch = assertTyping(EntityFetch.class, cardFieldElementGraphIdAsFetchSource.getFetches()[0]);
        assertEquals(Key.class.getName(), keyFetch.getEntityPersister().getEntityName());
    } finally {
        sf.close();
    }
}
Also used : EntityFetch(org.hibernate.loader.plan.spi.EntityFetch) OuterJoinLoadable(org.hibernate.persister.entity.OuterJoinLoadable) CollectionAttributeFetch(org.hibernate.loader.plan.spi.CollectionAttributeFetch) CollectionFetchableElementEntityGraph(org.hibernate.loader.plan.build.internal.returns.CollectionFetchableElementEntityGraph) FetchSource(org.hibernate.loader.plan.spi.FetchSource) Configuration(org.hibernate.cfg.Configuration) LoadPlan(org.hibernate.loader.plan.spi.LoadPlan) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) BidirectionalEntityReference(org.hibernate.loader.plan.spi.BidirectionalEntityReference) Key(org.hibernate.test.annotations.cid.keymanytoone.Key) PrimaryKey(org.hibernate.test.annotations.cid.keymanytoone.PrimaryKey) Card(org.hibernate.test.annotations.cid.keymanytoone.Card) EntityReturn(org.hibernate.loader.plan.spi.EntityReturn) Test(org.junit.Test) EncapsulatedCompositeIdResultSetProcessorTest(org.hibernate.test.loadplans.process.EncapsulatedCompositeIdResultSetProcessorTest)

Aggregations

BidirectionalEntityReference (org.hibernate.loader.plan.spi.BidirectionalEntityReference)3 EntityFetch (org.hibernate.loader.plan.spi.EntityFetch)2 Configuration (org.hibernate.cfg.Configuration)1 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1 CollectionFetchableElementEntityGraph (org.hibernate.loader.plan.build.internal.returns.CollectionFetchableElementEntityGraph)1 EntityReferenceInitializer (org.hibernate.loader.plan.exec.process.spi.EntityReferenceInitializer)1 CollectionAttributeFetch (org.hibernate.loader.plan.spi.CollectionAttributeFetch)1 CompositeFetch (org.hibernate.loader.plan.spi.CompositeFetch)1 EntityReturn (org.hibernate.loader.plan.spi.EntityReturn)1 Fetch (org.hibernate.loader.plan.spi.Fetch)1 FetchSource (org.hibernate.loader.plan.spi.FetchSource)1 LoadPlan (org.hibernate.loader.plan.spi.LoadPlan)1 EntityPersister (org.hibernate.persister.entity.EntityPersister)1 OuterJoinLoadable (org.hibernate.persister.entity.OuterJoinLoadable)1 WalkingException (org.hibernate.persister.walking.spi.WalkingException)1 Card (org.hibernate.test.annotations.cid.keymanytoone.Card)1 Key (org.hibernate.test.annotations.cid.keymanytoone.Key)1 PrimaryKey (org.hibernate.test.annotations.cid.keymanytoone.PrimaryKey)1 EncapsulatedCompositeIdResultSetProcessorTest (org.hibernate.test.loadplans.process.EncapsulatedCompositeIdResultSetProcessorTest)1 EntityType (org.hibernate.type.EntityType)1