Fetch data for entity's mapped on relation field

Posts   
 
    
sg_mt
User
Posts: 4
Joined: 22-Jun-2009
# Posted on: 23-Jun-2009 00:40:35   

I have a fetched entity A. This entity has field CollectionOfB "mapped on relation" (1:n). EntityCollection<B> in this field wasn't fetched (for any reason).

What is the simplies, easiest and the most logic way to fetch data for this field/collection using the adapter?

I just can't accept that solution could look like that:


//AEntity InstanceOfA - previously fetched entity
//I want to fetch InstanceOfA.CollectionOfB field
IRelationPredicateBucket filter = new RelationPredicateBucket();
            filter.PredicateExpression.Add(BFields.RelationId == InstanceOfA.Id);
            DataAccessAdapter adapter = new DataAccessAdapter();
            adapter.FetchEntityCollection(InstanceOfA. CollectionOfB, filter);

Regards, J (hope my very first approach to LLBLGEN will not end in disaster)

rdhatch
User
Posts: 198
Joined: 03-Nov-2007
# Posted on: 23-Jun-2009 03:12:01   

Hi J,

What you're probably looking for is called Prefetch. Prefetching allows you to fetch an Entity and its related Entities at the same time. This will populate your EntityCollection<B> for you.

Hope this helps!

Ryan

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 23-Jun-2009 04:43:53   

Hi J. As Ryan suggested, you need to use PrefetchPaths. Your code above should look like:

// build the prefetchpath
IPrefetchPath2 thePath = new PrefetchPath2((int) EntityType.AEntity);
thePath.Add(AEntity.PrefetchPathBUsingRelationId);

// fetch
AEntity InstanceOfA = new InstanceOfA(pk);
using (DataAccessAdapter adapter = new DataAccessAdapter())
{
     adapter.FetchEntity(InstanceOfA, thePath);
}
David Elizondo | LLBLGen Support Team
sg_mt
User
Posts: 4
Joined: 22-Jun-2009
# Posted on: 23-Jun-2009 09:50:12   

Maybe I didn't described situation good enough.

There are n tables/entities (I cannot know how many in moment of fetch, sometimes it's simply a recursive relation):

A 1-oo B 1-oo C .... 1-oo N

Using fetch (with prefetch) I can get data only from the first "relation":

A and A.CollectionOfB

So in CollectionOfB I have collection of related entities of type B. But all fetched entities B have not fetched related CollectionOfC.

So I have situation, where I have data of some entity and I want to fetch related data to "mapped relation field".

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 23-Jun-2009 10:34:43   

The following is copied from the docs:

The easiest way to retrieve a set of entities in an entity collection class, is by using a related entity, which in turn is used as a filter. For example, let's use our user "CHOPS" again and let's retrieve all the order entities for that customer. Note that in the following example we do not actually fetch the customer entity from the database, we only use the object and its PK value to construct the filter.

// C#, .NET 2.0 CustomerEntity customer = new CustomerEntity("CHOPS"); DataAccessAdapter adapter = new DataAccessAdapter(); EntityCollection<OrderEntity> orders = customer.Orders; adapter.FetchEntityCollection(orders, customer.GetRelationInfoOrders());

Having the customer entity, the above is how to fetch its Orders collection.

sg_mt
User
Posts: 4
Joined: 22-Jun-2009
# Posted on: 23-Jun-2009 10:57:39   

Walaa wrote:

The following is copied from the docs:


// C#, .NET 2.0
CustomerEntity customer = new CustomerEntity("CHOPS");
DataAccessAdapter adapter = new DataAccessAdapter();
EntityCollection<OrderEntity> orders = customer.Orders;
adapter.FetchEntityCollection(orders, customer.GetRelationInfoOrders());

Having the customer entity, the above is how to fetch its Orders collection.

So, without additional variables

 adapter.FetchEntityCollection(InstanceOfA.CollectionOfB, InstanceOfA.GetRelationInfoCollectionOfB());

Thanks!