Working with Code and Persistence Mapping

This article only considers Persistence in an 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, much is done by using default values - but, if you want to tune the default values, you can.

Consider this model:

01 - Code and Persistence Mapping.png

We state that the “TheDefaultSuperClass” should be the Superclass for all other classes in our package by setting the Package.

02 - Code and Persistence Mapping.png

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. 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 to do it:

  1. Add a DefaultORMappingBuilder component to your PMP designer.
  2. Set the NewMappingProvider (the mapping that is calculated from your current model) to the DefaultORMappingBuilder1.
  3. Also, set the RunTimeMappingProvider (the mapping that will be used in runtime).
03 - Code and Persistence Mapping.png

In the DefaultORMappingBuilder, we can now influence the Mapping in certain ways:

04 - Code and Persistence Mapping.png

Above, I said that the DefaultSuperclass for the package (here called RootClass) should be ChildMapped – i.e. its attributes are not stored in a table of its own but rather in each child's table.

  • I also choose to 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 each table; the default is “Eco_Id” – but I would rather have it based on the table's name, so I set <TableName>ID.

These two changes from default behavior make the database easier to read for someone just accessing the database – as all Identities that are joinable end with an ID.

For control of an ordinary class’s OR mapping (i.e. the non-DefaultSuperClass), simply set this on the class:

05 - Code and Persistence Mapping.png

If you do not see this property on your classes – it is most likely that you need to set the OrMappingConfigMode setting on your Package:

06 - Code and Persistence Mapping.png

Having the settings above done, we can now: generate code, build all, switch to PMP view, Press Validate Model, and Press Generate default mapping XML:

07 - Code and Persistence Mapping.png

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="&lt;Default&gt;" />
    <AttributeDef Name="CreateTime" Alias="Class1_A" Columns="CreateTime" AttributeMapper="&lt;Default&gt;" />
    <AttributeDef Name="Attribute1" Alias="Class1_A" Columns="Attribute1" AttributeMapper="&lt;Default&gt;" />
  </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="&lt;Default&gt;" />
    <AttributeDef Name="CreateTime" Alias="Class4_A" Columns="CreateTime" AttributeMapper="&lt;Default&gt;" />
    <AttributeDef Name="Attribute2" Alias="Class4_A" Columns="Attribute2" AttributeMapper="&lt;Default&gt;" />
    <AttributeDef Name="Attribute1" Alias="Class4_A" Columns="Attribute1" AttributeMapper="&lt;Default&gt;" />
  </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="&lt;Default&gt;" />
  </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 would not bother 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 uses the old mapping and compares it to a freshly generated mapping in order to deduce all the fine print your model changes inflict on the db-mapping.

08 - Code and Persistence Mapping.png

In order to completely de-mystify the Mapping provision:

  • NewMappingProvider shares information on how the mapping should look after evolve db.
  • OldMappingProvider gets information on how the mapping looks in your current db.
  • RuntimeMappingProvider is the mapping your system uses in runtime when accessing the database.
This page was edited 5 days ago on 06/17/2024. What links here