unit edEntities;

interface

uses
  SysUtils, 
  Generics.Collections, 
  Aurelius.Mapping.Attributes, 
  Aurelius.Types.Blob, 
  Aurelius.Types.DynamicProperties, 
  Aurelius.Types.Nullable, 
  Aurelius.Types.Proxy;

type
  TProvince = class;
  TSchool = class;
  
  [Entity]
  [Table('Province')]
  [Id('FprID', TIdGenerator.IdentityOrSequence)]
  TProvince = class
  private
    [Column('prID', [TColumnProp.Required, TColumnProp.NoInsert, TColumnProp.NoUpdate])]
    FprID: Integer;
    
    [Column('prName', [TColumnProp.Required], 50)]
    FprName: string;

    [ManyValuedAssociation([TAssociationProp.Lazy], CascadeTypeAll)]
    [ForeignJoinColumn('scProvinceRef', [TColumnProp.Required])]
    FSchools: Proxy<TList<Tschool>>;

    function GetSChools: TList<Tschool>;

  public
    constructor Create; overload;
    constructor Create(const AName: string); overload;
    property prID: Integer read FprID write FprID;
    property prName: string read FprName write FprName;
    property Schools: TList<Tschool> read GetSchools;
  end;



  [Entity]
  [Table('School')]
  [Id('FscID', TIdGenerator.IdentityOrSequence)]
  TSchool = class
  private
    [Column('scID', [TColumnProp.Required, TColumnProp.NoInsert, TColumnProp.NoUpdate])]
    FscID: Integer;
    
    [Column('scName', [TColumnProp.Required], 100)]
    FscName: string;
    
    [Association([TAssociationProp.Lazy, TAssociationProp.Required], CascadeTypeAll - [TCascadeType.Remove])]
    [JoinColumn('scProvinceRef', [TColumnProp.Required], 'prID')]
    FscProvinceRef: Proxy<Tprovince>;

    function GetscProvinceRef: Tprovince;
    procedure SetscProvinceRef(const Value: Tprovince);

  public
    constructor Create(const AName: string);
    property scID: Integer read FscID write FscID;
    property scName: string read FscName write FscName;
    property scProvinceRef: Tprovince read GetscProvinceRef write SetscProvinceRef;
  end;


implementation

{ TProvince }

constructor TProvince.Create;
begin
  FSchools.SetInitialValue(TList<TSchool>.Create);
end;

constructor TProvince.Create(const AName: string);
begin
  Create;
  prName := AName;
end;

function TProvince.GetSChools: TList<Tschool>;
begin
  Result := FSchools.Value;
end;


{ TSchool }

constructor TSchool.Create(const AName: string);
begin
  FscName := AName;
end;

function TSchool.GetscProvinceRef: Tprovince;
begin
  Result := FscProvinceRef.Value;
end;

procedure TSchool.SetscProvinceRef(const Value: Tprovince);
begin
  FscProvinceRef.Value := Value;
end;


initialization
  RegisterEntity(TProvince);
  RegisterEntity(TSchool);

finalization

end.
