No edit summary |
No edit summary |
||
Line 4: | Line 4: | ||
Consider this model: | Consider this model: | ||
[[File:01 - Code and Persistence Mapping.png|none|thumb]] | |||
We state that the “TheDefaultSuperClass” should be the Superclass for all other classes in our package by setting the Package | We state that the “TheDefaultSuperClass” should be the Superclass for all other classes in our package by setting the Package | ||
[[File:02 - Code and Persistence Mapping.png|none|thumb|418x418px]] | |||
We mark the TheDefaultSuperClass as being Abstract (indicated by Italic style on the class name in diagrams). | We mark the TheDefaultSuperClass as being Abstract (indicated by Italic style on the class name in diagrams). | ||
Line 18: | Line 16: | ||
This is how; Add a DefaultORMappingBuilder component to your PMP designer. Set the NewMappingProvider (the mapping that is calculated from your current model) to the DefaultORMappingBuilder1. Also set the RunTimeMappingProvider (the mapping that will be used in runtime). | This is how; Add a DefaultORMappingBuilder component to your PMP designer. Set the NewMappingProvider (the mapping that is calculated from your current model) to the DefaultORMappingBuilder1. Also set the RunTimeMappingProvider (the mapping that will be used in runtime). | ||
[[File:03 - Code and Persistence Mapping.png|none|thumb|448x448px]] | |||
In the DefaultORMappingBuilder we can now influence the Mapping in certain ways: | In the DefaultORMappingBuilder we can now influence the Mapping in certain ways: | ||
[[File:04 - Code and Persistence Mapping.png|none|thumb|422x422px]] | |||
Above I said that the DefaultSuperclass for the package (here called RootClass) should be ChildMapped – ie its attributes not stored in a table of its own but rather in each child´s table. | Above I said that the DefaultSuperclass for the package (here called RootClass) should be ChildMapped – ie its attributes not stored in a table of its own but rather in each child´s table. | ||
Line 34: | Line 30: | ||
For control of an ordinary class’s OR mapping (ie the non DefaultSuperClass) you simply set this on the class: | For control of an ordinary class’s OR mapping (ie the non DefaultSuperClass) you simply set this on the class: | ||
[[File:05 - Code and Persistence Mapping.png|none|thumb|340x340px]] | |||
If you do not see this property on your classes – it is most likely that you need to set OrMappingConfigMode setting on your package: | If you do not see this property on your classes – it is most likely that you need to set OrMappingConfigMode setting on your package: | ||
[[File:06 - Code and Persistence Mapping.png|none|thumb|460x460px]] | |||
Having the settings above done we can now; generate code, build all, switch to PMP view, Press Validate Model, Press Generate default mapping XML: | Having the settings above done we can now; generate code, build all, switch to PMP view, Press Validate Model, Press Generate default mapping XML: | ||
[[File:07 - Code and Persistence Mapping.png|none|thumb]] | |||
Once you have generated the default mapping XML you get an XML file appended to the project. | Once you have generated the default mapping XML you get an XML file appended to the project. | ||
Line 123: | Line 116: | ||
Normally you will not bother with generating this file. You would just let MDriven generate it when evolving the database – then MDriven stores this in the database so that it can retrieve it on the next evolve. MDriven use the old mapping and compares it to a fresh generated mapping in order to deduce all the fine print your model changes inflict on the db-mapping. | Normally you will not bother with generating this file. You would just let MDriven generate it when evolving the database – then MDriven stores this in the database so that it can retrieve it on the next evolve. MDriven use the old mapping and compares it to a fresh generated mapping in order to deduce all the fine print your model changes inflict on the db-mapping. | ||
[[File:08 - Code and Persistence Mapping.png|none|thumb|392x392px]] | |||
In order to completely de-mystify the Mapping provision: | In order to completely de-mystify the Mapping provision: |
Revision as of 16:12, 2 December 2018
This article only considers Persistence in a SQL database. As such it does not apply to systems that use XmlPersistenceMappers or MDrivenServer (MDrivenServer has the settings described below as default).
In MDriven Framework a lot is done by just using default values. But if you want to tune the default values you can.
Consider this model:
We state that the “TheDefaultSuperClass” should be the Superclass for all other classes in our package by setting the Package
We mark the TheDefaultSuperClass as being Abstract (indicated by Italic style on the class name in diagrams).
The default is that the DefaultSuperclass will get its own table in the database. But that is actually often not what we want to do.
Instead we want to have the fields from DefaultSuperClass in all inheriting classes tables.
This is how; Add a DefaultORMappingBuilder component to your PMP designer. Set the NewMappingProvider (the mapping that is calculated from your current model) to the DefaultORMappingBuilder1. Also set the RunTimeMappingProvider (the mapping that will be used in runtime).
In the DefaultORMappingBuilder we can now influence the Mapping in certain ways:
Above I said that the DefaultSuperclass for the package (here called RootClass) should be ChildMapped – ie its attributes not stored in a table of its own but rather in each child´s table.
I also choose the change how foreign key fields should be generated – the default is <Name>, but here I append an extra “ID”.
I also set the Id Column name for the each table; the default is “Eco_Id” – but I would rather have it to be based on the tables name so I set <TableName>ID.
These two changes from default behavior makes the database easier to read for someone just accessing the database – as all Identities that are joinable end with ID.
For control of an ordinary class’s OR mapping (ie the non DefaultSuperClass) you simply set this on the class:
If you do not see this property on your classes – it is most likely that you need to set OrMappingConfigMode setting on your package:
Having the settings above done we can now; generate code, build all, switch to PMP view, Press Validate Model, Press Generate default mapping XML:
Once you have generated the default mapping XML you get an XML file appended to the project.
It looks like this:
<?xml version="1.0" encoding="utf-8"?> <ORMapping> <ClassDef Name="Class1" SuperClass="TheDefaultSuperClass"> <AliasDef Name="Class1_A" Table="Class1" ExtentRequiresDiscriminator="False"> <KeyImpl Name="EcoKey"> <KeyColumn Name="Class1ID" /> </KeyImpl> <DiscriminatorImpl Name="EcoType" Column="ECO_TYPE" /> </AliasDef> <DiscriminatorValue Name="EcoType" Value="3" /> <AttributeDef Name="ChangeTime" Alias="Class1_A" Columns="ChangeTime" AttributeMapper="<Default>" /> <AttributeDef Name="CreateTime" Alias="Class1_A" Columns="CreateTime" AttributeMapper="<Default>" /> <AttributeDef Name="Attribute1" Alias="Class1_A" Columns="Attribute1" AttributeMapper="<Default>" /> </ClassDef> <ClassDef Name="Class4" SuperClass="Class3"> <AliasDef Name="Class4_A" Table="Class4" ExtentRequiresDiscriminator="False"> <KeyImpl Name="EcoKey"> <KeyColumn Name="Class4ID" /> </KeyImpl> <DiscriminatorImpl Name="EcoType" Column="ECO_TYPE" /> </AliasDef> <DiscriminatorValue Name="EcoType" Value="7" /> <AttributeDef Name="ChangeTime" Alias="Class4_A" Columns="ChangeTime" AttributeMapper="<Default>" /> <AttributeDef Name="CreateTime" Alias="Class4_A" Columns="CreateTime" AttributeMapper="<Default>" /> <AttributeDef Name="Attribute2" Alias="Class4_A" Columns="Attribute2" AttributeMapper="<Default>" /> <AttributeDef Name="Attribute1" Alias="Class4_A" Columns="Attribute1" AttributeMapper="<Default>" /> </ClassDef> <ClassDef Name="Class3" SuperClass="TheDefaultSuperClass" /> <!-- NOTICE - EMPTY DUE to being abstract--> <ClassDef Name="TheDefaultSuperClass"> <KeyDef Name="EcoKey" Signature="System.Int32" IsId="True" KeyMapper="DefaultEcoIdMapper" /> <DiscriminatorDef Name="EcoType" Signature="System.Int16" /> </ClassDef> <ClassDef Name="Class2" SuperClass="Class1"> <AliasDef Name="Class2_A" Table="Class2" ExtentRequiresDiscriminator="False"> <KeyImpl Name="EcoKey"> <KeyColumn Name="Class2ID" /> </KeyImpl> <DiscriminatorImpl Name="EcoType" Column="ECO_TYPE" /> </AliasDef> <DiscriminatorValue Name="EcoType" Value="13" /> <AttributeDef Name="Attribute2" Alias="Class2_A" Columns="Attribute2" AttributeMapper="<Default>" /> </ClassDef> <Database Name=""> <!-- NOTICE – Missing both Class3 and TheDefaultSuperClass since they are both child mapped and abstract--> <Table Name="Class1"> <Column Name="Class1ID" AllowNULL="False" Type="INT" Length="0" Scale="0" Precision="0" DefaultValue="" /> <!-- NOTICE - TableName+ID --> <Column Name="ECO_TYPE" AllowNULL="False" Type="SMALLINT" Length="0" Scale="0" Precision="0" DefaultValue="" /> <Column Name="ChangeTime" AllowNULL="False" Type="DATETIME" Length="255" Scale="-1" Precision="-1" DefaultValue="'19000101'" /> <!-- NOTICE - Inherited attributes child mapped --> <Column Name="CreateTime" AllowNULL="False" Type="DATETIME" Length="255" Scale="-1" Precision="-1" DefaultValue="'19000101'" /> <Column Name="Attribute1" AllowNULL="True" Type="VARCHAR(255)" Length="255" Scale="-1" Precision="-1" DefaultValue="''" /> <Index Name="IX_PK_Class1" Columns="Class1ID" IsUnique="False" IsCaseSensitive="False" IsPrimary="True" IsDescending="False" IsFilteredOnNotNull="False" /> </Table> <Table Name="Class2"> <Column Name="Class2ID" AllowNULL="False" Type="INT" Length="0" Scale="0" Precision="0" DefaultValue="" /> <Column Name="ECO_TYPE" AllowNULL="False" Type="SMALLINT" Length="0" Scale="0" Precision="0" DefaultValue="" /> <Column Name="Attribute2" AllowNULL="True" Type="VARCHAR(255)" Length="255" Scale="-1" Precision="-1" DefaultValue="''" /> <Index Name="IX_PK_Class2" Columns="Class2ID" IsUnique="False" IsCaseSensitive="False" IsPrimary="True" IsDescending="False" IsFilteredOnNotNull="False" /> </Table> <Table Name="Class4"> <!-- NOTICE – attributes from both TheDefaultSuperclass And from Class3--> <Column Name="Class4ID" AllowNULL="False" Type="INT" Length="0" Scale="0" Precision="0" DefaultValue="" /> <Column Name="ECO_TYPE" AllowNULL="False" Type="SMALLINT" Length="0" Scale="0" Precision="0" DefaultValue="" /> <Column Name="Attribute1" AllowNULL="True" Type="VARCHAR(255)" Length="255" Scale="-1" Precision="-1" DefaultValue="''" /> <Column Name="ChangeTime" AllowNULL="False" Type="DATETIME" Length="255" Scale="-1" Precision="-1" DefaultValue="'19000101'" /> <Column Name="CreateTime" AllowNULL="False" Type="DATETIME" Length="255" Scale="-1" Precision="-1" DefaultValue="'19000101'" /> <Column Name="Attribute2" AllowNULL="True" Type="VARCHAR(255)" Length="255" Scale="-1" Precision="-1" DefaultValue="''" /> <Index Name="IX_PK_Class4" Columns="Class4ID" IsUnique="False" IsCaseSensitive="False" IsPrimary="True" IsDescending="False" IsFilteredOnNotNull="False" /> </Table> </Database> </ORMapping>
Normally you will not bother with generating this file. You would just let MDriven generate it when evolving the database – then MDriven stores this in the database so that it can retrieve it on the next evolve. MDriven use the old mapping and compares it to a fresh generated mapping in order to deduce all the fine print your model changes inflict on the db-mapping.
In order to completely de-mystify the Mapping provision:
NewMappingProvider to get information on how the mapping should look after evolve db.
OldMappingProvider to get information on how the mapping looks in your current db.
RuntimeMappingProvider is the mapping your system use in runtime when accessing the database.