default predicate, prefetch path and field value

Posts   
 
    
Liero
User
Posts: 40
Joined: 18-Sep-2009
# Posted on: 21-Jul-2010 16:51:43   

I have Advertisement table, with fk column CodeAdvertisementType. I mapped two entities on this table, AdvertisementSingleEntity and AdvertisementMultiEntity.

I want to always have value of CodeAdvertisementType field '1' when it is AdvertisementSingleEntity and '2' when AdvertisementMultiEntity.

So I would made the field readonly. I want to automatically add predicate AdvertisementSingleFields.CodeAdvertisementType==1 resp. AdvertisementMultiFields.CodeAdvertisementType==2 anytime when using resp Advertisement[Single/Multi]Collections

And I want always fetch AdvertisementGroupEntity with AdvertisementMultiEntity (relation 1:1)

Is this possible? especially the default value and default predicate

using v2.6

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 21-Jul-2010 16:58:46   

This sounds ideal for an Inheritnace Hierarchy implementation (TargetPerHierarchy). Where the CodeAdvertisementType can act as the Discriminator column, and so filtering and default values would be automatically handled for you.

Liero
User
Posts: 40
Joined: 18-Sep-2009
# Posted on: 30-Jul-2010 14:44:49   

Isn't there another way how to set default predicate?

Inheritance hierarchy has another issues and didn't worked for me

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 31-Jul-2010 04:20:14   

Lets see how goes your Hierarchy thread so you can use inheritance. If not, we will provide other options.

David Elizondo | LLBLGen Support Team
Liero
User
Posts: 40
Joined: 18-Sep-2009
# Posted on: 05-Aug-2010 16:51:21   

there was also another issue related to DynamicData (see http://llblgen.com/tinyforum/Messages.aspx?ThreadID=18392).

If there is another option I would appreciate it, otherwise i have to do this on application layer, or somehow customize entity code

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 05-Aug-2010 20:47:46   

The usual way to set defaults such as these is to use a factory class that provides instances of the entities with the default items already set.

See also http://www.llblgen.com/documentation/2.6/Using%20the%20generated%20code/gencode_tapinroutinesevents.htm which explains how to tap into the various stages of the entity life cycle - this can also be useful for this sort of thing.

Matt

Liero
User
Posts: 40
Joined: 18-Sep-2009
# Posted on: 12-Aug-2010 18:05:46   

hmmm, is there option to use default alias on for entity?

I have two entities mapped on the same table (AdvertisementSingleEntity, AdvertisementGroupEntity).

I want to use them in the same select in relations and in predicate it gives not expected results.


var ac = new AdvertisementSingleCollection();

var relations = new RelationCollection();
relations.Add(AdvertisementSingleEntity.Relations.AdvertisementFileEntityUsingIdAdvertisement);
relations.Add(AdvertisementFileEntity.Relations.AdvertisementGroupEntityUsingIdAdvertisementGroup, JoinHint.Left);

var filter = new PredicateExpression();
filter.Add(AdvertisementSingleFields.CodeAdvertisementType == 1);
filter.Add(AdvertisementGroupFields.CodeAdvertisementType == 2);

ac.GetMulti(filter, relations);  

results in query: (this is bad)

SELECT DISTINCT [dbo].[Advertisement].[IdAdvertisement], [dbo].[Advertisement].[CodeAdvertisementType], [dbo].[Advertisement].[Created], [dbo].[Advertisement].[Modified], [dbo].[Advertisement].[Name]

FROM ( [dbo].[Advertisement] INNER JOIN [dbo].[AdvertisementFile] ON [dbo].[Advertisement].[IdAdvertisement]=[dbo].[AdvertisementFile].[IdAdvertisement])

WHERE ( ( [dbo].[Advertisement].[CodeAdvertisementType] = 1 AND [dbo].[Advertisement].[CodeAdvertisementType] = 2))

also if i use object alias, in RelationColection and in Predicate, there is still [dbo].[Advertisement] instead of [LPA_a1] in the 'select' part of the query

the query for code:

var relations = new RelationCollection();
relations.Add(AdvertisementSingleEntity.Relations.AdvertisementGroupEntityUsingIdAdvertisement);
ac.GetMulti(filter, relations);

would be: (this would be ok)

SELECT [asingle].IdAdvertisement... FROM [dbo].[Advertisement] as [asingle] INNER JOIN [dbo].[Advertisement] as [agroup] ON [asingle].[IdAdvertisement] =[agroup].[IdAdvertisement]

Ideally, but not necessary:

SELECT [asingle].IdAdvertisement... FROM ( SELECT IdAdvertisement .. FROM [dbo].[Advertisement] WHERE CodeAdvertisementType = 1 ) as [asingle]

INNER JOIN (SELECT IdAdvertisement .. FROM [dbo].[Advertisement] WHERE CodeAdvertisementType = 2 ) as [agroup] on [asingle].[IdAdvertisement] =[agroup].[IdAdvertisement]

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 12-Aug-2010 20:40:51   

I'm not sure I understand why the first generated SQL query is "bad" - it is doing exactly what you have asked it to do in the code

Why is this a problem for you...?

Matt

Liero
User
Posts: 40
Joined: 18-Sep-2009
# Posted on: 13-Aug-2010 09:09:32   

it always returns 0 rows, see where clause. In predicate I did not filter the same entity.

In relationCollection there is AdvertisementGroupEntity, which is not joined to advertisementfile in query. I think there should be: [dbo].[AdvertismentFile] INNER JOIN [dbo].[Advertisement] on [dbo].[AdvertismentFile].IdAvertisementGroup = [dbo].[Advertisement].IdAdvertisement

if there were the advertisement table 2x times in 'FROM' part in query, each with its own alias, it would return some rows

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 13-Aug-2010 09:45:22   

I guess you need to try the following:

var ac = new AdvertisementSingleCollection();

var relations = new RelationCollection();
relations.Add(AdvertisementSingleEntity.Relations.AdvertisementFileEntityUsingIdAdvertisement);
relations.Add(AdvertisementFileEntity.Relations.AdvertisementGroupEntityUsingIdAdvertisementGroup,  someAlias, JoinHint.Left);

var filter = new PredicateExpression();
filter.Add(AdvertisementSingleFields.CodeAdvertisementType == 1);
filter.Add(AdvertisementGroupFields.CodeAdvertisementType.SetObjectAlias(someAlias) == 2);

ac.GetMulti(filter, relations);