Collection of particular Entity TargetPerEntityHierarchy

Posts   
 
    
Posts: 1268
Joined: 10-Mar-2006
# Posted on: 22-Mar-2006 03:30:37   

If you have a TargetPerEntityHierarchy on TableA and you have TableB mapped 1:n to TableA, how can you get a collection from TableA of a certain Sub-Type?

TableBEntity tableB = new TableBEntity(12);

tableB.TableA <--this returns a collection of the Abstract or top level in the hierarchy.

The collection contains various types and I only want a particular type returned. Do I need to define my own property like - 'SubType1TableA' in TableBEntity that returns a SubType1TableACollection? And if so, for that how do I do that exactly? Will the SubType1TableACollection automatically include the discriminating type in the GetMulti() query?

Otis - it would be helpful if there were a section on 'Inheritance' inside the 'Using the generated code' as many of my questions are around using the inheritance. Just a suggestion.

(Also, I think this would potentially be easily solved in 2.0, by creating a view on top of the TableA collection that is returned?)

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 22-Mar-2006 07:31:39   

If the relation is between TableB and the SuperType of TableA, then you should filter on the SubType of TableA when you fetch TableB Entities.

Otherwise it would be better to define the relation between TableB and the SubType of TableA.

Posts: 1268
Joined: 10-Mar-2006
# Posted on: 22-Mar-2006 14:53:40   

Let me try to explain again...

You have the example relationship of CompanyCar, where you can have a subtype of 'SportsCar' and 'EconomyCar'. These are defined on one table with a discriminator value. Lets call this table 'CompanyCar'

You then have table Employee with a 1:n relationship to CompanyCar. (Yes, lets assume the employee can have multiple cars)

Now, you have loaded a particular employee instance with:

EmployeeEntity emp = new EmployeeEntity(12);

Now, I want to get a list of all the 'SportsCars' this employee has. However the only thing I have is: emp.CompanyCar, which contains all kinds of cars. I just wanted a certain type.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 22-Mar-2006 15:16:00   

In case you want to retreive all the Employee's company cars in a the ollection then later find the SportsCar out of them:

The collection will hold Entities of their special types. i.e. the CompanyCar collection will hold entities of Types Company Car, Sports Car...etc according to each entity type.

In case you want to fetch the collection with only sports cars from the start, then you might filter on the discremenating field. i.e. the CompanyCar collection will only hold entities of the type you have filtered upon.

Posts: 1268
Joined: 10-Mar-2006
# Posted on: 23-Mar-2006 17:41:18   

The collection will hold Entities of their special types. i.e. the CompanyCar collection will hold entities of Types Company Car, Sports Car...etc according to each entity type.

Yes, this I am aware of this is what happens normally.

In case you want to fetch the collection with only sports cars from the start, then you might filter on the discremenating field. i.e. the CompanyCar collection will only hold entities of the type you have filtered upon.

However, this is what I have the question about. You are saying I would have to extend the Employee class and add a 'sportscars' collection to it - and in there I would do the filter - right?

If so, how can I filter on the discriminating value easily? LLBLGen already knows what discrimating values go with what objects, so how can I use this information to do a filter without putting SomeField ==3 in a predicate.

In looking through the code, I see something like this:

CreateFilterUsingForeignKeys(orderDetailInstance, imageTypeInstance, fieldsToReturn);

and that is what I want to use where I say 'get me all of this object type' and do not have to put in the actual values. I think the logic is already in place to do that?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 24-Mar-2006 07:52:06   

You are saying I would have to extend the Employee class and add a 'sportscars' collection to it - and in there I would do the filter - right?

No.

Just use the existing CompanyCar collection.

And use a filter that might look lke the following:

filter.Add(PredicateFactory.CompareValue(CompanyCarFieldIndex.YourDiscriminatorFieldName, ComparisonOperator.Equal, YourSportsCarDiscriminatingValue));
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 24-Mar-2006 08:35:41   

Please look into filtering on an entity type -> Using the generated code -> selfservicing -> filtering and sorting -> Advanced filter usage-> Filtering on entity type.

You can use that to add a predicate to filter on a specific type. You then don't have to know which field is the discriminator column, or if there isn't a discriminator column, how to do the filtering in that scenario.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 1268
Joined: 10-Mar-2006
# Posted on: 28-Mar-2006 16:47:09   

That is exactly what I was looking for. Sorry it was in the docs - I hate it when I ask a question and you point out the answer in the docs! :-)