one-to-many relationship automapping fields

I have a entity that can keep a list of itself (tree). Special case on an one-to-many association.


[Entity, Automapping]
  [Table('Right')]
  TRight = class(TObject)
  strict private
    FID: Integer;
    [Association([TAssociationProp.Lazy], [])]
    [JoinColumn('ID_Parent', [], 'ID')]
    FParent: Proxy<TRight>;
    FName: String;
    FSubRights: TList<TRight>;
    procedure SetID(const Value: Integer);
    procedure SetName(const Value: String);
    procedure SetParent(const Value: TRight);
  private
    function getParent: TRight;
  public
    constructor create; overload;
    destructor destroy;
    property Id: Integer read FID write SetID;
    property Parent: TRight read getParent write SetParent;
    property Name: String read FName write SetName;
    [ManyValuedAssociation([], CascadeTypeAll, 'FParent')]
    property SubRights: TList<TRight> read FSubRights write FSubRights;
  end;


This creates this sqlite table


CREATE TABLE Right (
  ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  ID_Parent INTEGER,
  NAME TEXT NOT NULL,
  SUB_RIGHTS_RIGHT_ID INTEGER,
  CONSTRAINT FK_Right_Right_ID_Parent FOREIGN KEY (ID_Parent) REFERENCES Right (ID),
  CONSTRAINT FK_Right_Right_SUB_RIGHTS_RIGH FOREIGN KEY (SUB_RIGHTS_RIGHT_ID) REFERENCES Right (ID))



Why Aurelius creates "SUB_RIGHTS_RIGHT_ID"-field?
The ManyValuedAssociation is mapped to the FParent-field. 
And both fields are filled with the same values.

Is my entity-class correctly defined?

In the xdata result are two lists 

{            "$id": 1,
            "@xdata.type": "XData.Default.Right",
            "ID": 1,
            "Name": "Stammdaten",
            "Parent": null,
            "SubRights@xdata.proxy": "Right(1)/SubRights",
            "SubRights@xdata.proxy": "Right(1)/SubRights"
        }


Automapping attribute maps all fields in the class. Since you have added [Automapping] and also [ManyValuedAssociation] to the property, you are mapping both FSubRights and SubRights properties. Either:


a) Add [Transient] attribute to FSubRights field, or
b) Move the [ManyValuedAssociation] attribute from the SubRights property to FSubRights field

I also strongly recommend using lazy-loading (proxy) for the FSubRights list.

Wagner R. Landgraf2016-12-19 18:41:13