Mapping/Inheritance not creating fields associations on Aurelius 4.17.2

Hi, I have two types that represent the same table. The first is the 'min', a model retrieved. The second is a type that extends the first and creates new fields, which should generate a foreign key in the referenced fields, however it is not creating. In delphi 10.3.3 it does not occur, after migrating to 10.4 with aurelius 4.17.2 it is occurring.

The following code represents my doubt:

type
   [Entity]
   [Inheritance(TInheritanceStrategy.SingleTable)]
   [Sequence('GEN_CLIFOR_ID')]
   [Id('CHAVE', TIdGenerator.None)]
   [Table('CLIFOR')]
   TPessoaMin = class
   private
   [...]
   public
   [...]
end;


type
   [Entity]
   TPessoa = class(TPessoaMin)
   private
      FCliforRepr: TList<TCliforRepr>;
      FContatos: TList<TContatoPessoa>;
      FEnderecos: TList<TEnderecoPessoa>;
   public
      [ManyValuedAssociation([TAssociationProp.Required], CascadeTypeAll)]
      [ForeignJoinColumn('CHAVECLIFOR')]
      property CliforRepr: TList<TCliforRepr> read FCliforRepr write FCliforRepr;

      [ManyValuedAssociation([], CascadeTypeAll)]
      [ForeignJoinColumn('CHAVECLIFOR')]
      [Where('{Ativo} = 1')]
      property Contatos: TList<TContatoPessoa> read FContatos write FContatos;

      [ManyValuedAssociation([], CascadeTypeAll)]
      [ForeignJoinColumn('CHAVECLIFOR')]
      [Where('{Ativo} = 1')]
      property Enderecos: TList<TEnderecoPessoa> read FEnderecos write FEnderecos;
   end;

The TPessoa ID is not created on the TCliforRepr, TContatoPessoa,TEnderecoPessoa tables.

I'm forgetting something, could you help me?

That is not a valid mapping. Inheritance in Aurelius must be related to persistence, meaning if you have a single table hierarchy, you have to define DiscriminatorColumn attribute in base class and DiscriminatorValue in all classes of the hierarchy.

If you have a minimal sample project that reproduces the issue with SQLite, we can take a deeper look at it.

First of all, thanks for the reply. Can you give us a more complete example of how to use inheritance and Discriminators? About the project, the idea is to create the field 'CHAVECLIFOR' (which is the primary key of the TPessoaMin table), as a foreign key in the tables
TCliforRepr,
TContatoPessoa, TEnderecoPessa. We were unable to reproduce a minimum project to make available to you at this time.

We need TPessoaMin and TPessoa to represent the same table (CLIFOR), where TPessoa inherits from TPessoaMin and adds new fields

Single-table inheritance example is described here: https://download.tmssoftware.com/business/aurelius/doc/web/single-table_inheritance.html.

What you can do is simply have two different classes (TPessoa and TPessoaMin), map them individually, and set them in different models. Then use one model or the other depending on the class you want to use.

Why?

What we need is that TPessoa inherits from TPessoaMin and that TPessoa creates new fields in SQLite. Currently, it is not creating the associations present in TPessoa, only what is mapped in TPessoaMin

We were able to structure a test project. The creation of the 'CHAVECLIFOR' field does not occur in the 'CLIFORENDERECOS' table, using inheritance. However, in versions prior to Aurelius 4.17.2, this association was normally created ... Follow link to the project -> https://files.fm/u/64euygke

This is the approach you should use:

   TPessoaBase = class
   private
      FFantasia: string;
      FAtivo: integer;
      FCodigo: string;
      FChaveEmpresa: integer;
      FChave: integer;
   public
      property Chave: integer read FChave write FChave;
      property Ativo: integer read FAtivo write FAtivo;
      property ChaveEmpresa: integer read FChaveEmpresa write FChaveEmpresa;
      property Codigo: string read FCodigo write FCodigo;
      property Fantasia: string read FFantasia write FFantasia;
   end;

   [Entity]
   [Model('Min')]
   [Sequence('GEN_CLIFOR_ID')]
   [Id('CHAVE', TIdGenerator.None)]
   [Table('CLIFOR')]
   TPessoaMin = class(TPessoaBase)
   public
      [Column('CHAVE')]
      property Chave;
      [Column('ATIVO')]
      property Ativo;
      [Column('CHAVEEMPRESA')]
      property ChaveEmpresa;
      [Column('CODIGO')]
      property Codigo;
      [Column('FANTASIA')]
      property Fantasia;
   end;

   [Entity]
   [Sequence('GEN_CLIFOR_ID')]
   [Id('CHAVE', TIdGenerator.None)]
   [Table('CLIFOR')]
   TPessoa = class(TPessoaBase)
   private
      [ManyValuedAssociation([TAssociationProp.Lazy], CascadeTypeAll)]
      [ForeignJoinColumn('CHAVECLIFOR')]
      [Where('{Ativo} = 1')]
      FEnderecos: Proxy<TList<TEnderecoPessoa>>;
    function GetEnderecos: TList<TEnderecoPessoa>;
   public
      constructor Create;
      destructor Destroy; override;
      property Enderecos: TList<TEnderecoPessoa> read GetEnderecos;
   public
      [Column('CHAVE')]
      property Chave;
      [Column('ATIVO')]
      property Ativo;
      [Column('CHAVEEMPRESA')]
      property ChaveEmpresa;
      [Column('CODIGO')]
      property Codigo;
      [Column('FANTASIA')]
      property Fantasia;
   end;

...

constructor TPessoa.Create;
begin
  FEnderecos.SetInitialValue(TList<TEnderecoPessoa>.Create);
end;

destructor TPessoa.Destroy;
begin
  FEnderecos.DestroyValue;
  inherited;
end;

function TPessoa.GetEnderecos: TList<TEnderecoPessoa>;
begin
  Result := FEnderecos.Value;
end;

One different mapping in each model. To use TPessoaMin, you should use a manager created with model Min:

Manager := TObjectManager.Create(Connection, TMappingExplorer.Get('Min'));
PessoaMin := Manager.Find<TPessoaMin>(Id);

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.