Prefetch problems

Posts   
 
    
hplloyd
User
Posts: 191
Joined: 29-Oct-2004
# Posted on: 25-Apr-2005 13:57:57   

I am having problems getting my prefetch to return anything... I am probably doing something wrong as I am fairly new to prefetches!! The example below is something I will make alot of use of so any help is much appreciated

I have 4 related tables

Product

ProductID ProductName

ProductGroupMemberProductMap

ProductGroupMemberProductMapID ProductID Product GroupMemberID

ProductGroupMember

ProductGroupMemberID ProductGroupMemberName ProductGroupID

ProductGroup

ProductGroupID Product GroupName

Therefore a ProductGroup (e.g. "Category") has a number of ProductGroupMembers (e.g. "Software" or "Hardware") and each product is associated with a member for each product group via the m:n table (I.e. Product A's category is software etc)

I am trying to return a collection of all products together with their respective ProductGroupMemberName for a specified ProductGroupID and am using the following code (that makes use of the 3rd party manager classes )

PrefetchPath2 myPrefetch = new PrefetchPath2( (int) EntityType.ProductEntity );
                myPrefetch.Add(ProductEntity.PrefetchPathProductGroupMember);

                IRelationPredicateBucket myBucket = new RelationPredicateBucket();
                myBucket.Relations.Add(ProductEntity.Relations.ProductGroupMemberProductMapEntityUsingProductID);
                myBucket.Relations.Add(ProductGroupMemberProductMapEntity.Relations.ProductGroupMemberEntityUsingProductGroupMemberID);

                IPredicateExpression myExpression = new PredicateExpression();              
                myExpression.Add(PredicateFactory.CompareValue(ProductGroupMemberFieldIndex.ProductGroupID, ComparisonOperator.Equal, GroupID));
                myBucket.PredicateExpression.Add(myExpression);

                return FetchCollectionWithPrefetch((RelationPredicateBucket) myBucket,null,myPrefetch,myAdapter);

Where GroupID is the ProductGroupID that I want to analyse...

Firstly why did I have to add the relations to myBucket to get the fetch to work (they are the same relations that are in the prefetch)... is it because I need the relations for my predicate expression?

Secondly, I want to use the value of the ProductGroupMemberName in a Janus grid so I have added the following code to the ProductEntity usercoderegion

public string ProductGroupMember_Name
        {
            get
            {
                if (_productGroupMember.Items.Count != 1)
                {
                    return String.Empty;
                }
                else
                {
                    ProductGroupMemberEntity myEntity = (ProductGroupMemberEntity) _productGroupMember.Items[0];
                    return myEntity.Name;
                }
            }

and have set the DisplyMember for the grid column to ProductGroupMember_Name.

However nothing is displayed

Any help would be much appreciated

hplloyd
User
Posts: 191
Joined: 29-Oct-2004
# Posted on: 25-Apr-2005 15:12:57   

Further information.....

I know why I am getting nothing and the reason is that 4 items are being returned for each Product....strange...

It just so happens that there are 4 rows in the ProductgroupMemberProductGroupMap table for each product...

I.E. It looks like my filter to restrict the returned prefetch items to the ProductGroup i Selected is not working.....

Wait a minute I havent included a filter on the prefetch items I have (incorrectly I assume) included the filter on the root fetch not the prefetch .....

How do I include a filter on the prefetch.....checking this out in help files!!

hplloyd
User
Posts: 191
Joined: 29-Oct-2004
# Posted on: 25-Apr-2005 15:18:39   

OK,

Sorted

I needed to put the Predicate expression in the prefetch and it all works great!!

Infact I just realised that if i use the ProductGroupMemberProductMap table as my root table then I can much more easily pick up the productName and productgroupmembername because LLBL can add these proerties itself!!

I shall change my code but anyways it has been a good learning exercise for me to get my head around prefetches!!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 25-Apr-2005 17:51:21   

I was just about to answer this one smile , glad you sorted it out! simple_smile

Frans Bouma | Lead developer LLBLGen Pro