Proxy property

Hello, don't understand this part:

I'm have one database user, and this user is owner of two posts, but why Posts.Count = 0 ?



uses
  System.SysUtils,
  System.Generics.Collections,
  Aurelius.Engine.ObjectManager,
  Aurelius.Drivers.Interfaces,
  Aurelius.Drivers.SQLite,
  Aurelius.Engine.DatabaseManager,
  Entities in 'Entities.pas';


var
  Connection: IDBConnection;
  DBManager: TDatabaseManager;
  ObjManager: TObjectManager;
  User1, User: TUser;
  Post1, Post2, Post: TPost;
  Posts: TList<TPost>;
begin
  Writeln('Starting...');
  Connection := TSQLiteNativeConnectionAdapter.Create(':memory:');
  DBManager := TDatabaseManager.Create(Connection);
  DBManager.BuildDatabase;
  DBManager.Free;
  ObjManager := TObjectManager.Create(Connection);


  User1 := TUser.Create;
  User1.Name := 'User 1';
  ObjManager.Save(User1);
  Writeln('User1 "' + User1.Name + '" added');


  Post1 := TPost.Create;
  Post1.User := User1;
  ObjManager.Save(Post1);
  Writeln('Post1 "' + Post1.ID.ToString + '" added');


  Post2 := TPost.Create;
  Post2.User := User1;
  ObjManager.Save(Post2);
  Writeln('Post2 "' + Post2.ID.ToString + '" added');
  ObjManager.Flush;


  Post := ObjManager.Find<TPost>(Post1.ID);
  Writeln('User for Post 1: "' + Post.User.Name + '"');


  User := ObjManager.Find<TUser>(User1.ID);
  Posts := User.Posts;
  Writeln('Find User: "' + User.Name + '", Posts Count:' + Posts.Count.ToString);


  ObjManager.Free;


  Writeln('Finished... Press Enter');
  Readln;
end.


This is Program output:


Starting...
User1 "User 1" added
Post1 "{43AC66F4-B5B5-4740-9040-2EE1EB5377B4}" added
Post2 "{E85FD114-B412-4C36-957B-230B63EB99FD}" added
User for Post 1: "User 1"
Find User: "User 1", Posts Count:0
Finished... Press Enter


And this is Entities.pas content;

unit Entities;


interface


uses
  Generics.Collections,
  Aurelius.Mapping.Attributes,
  Aurelius.Types.Proxy;


type
  TPost = class;
  TUser = class;


  [Entity]
  [Table('Post')]
  [Id('FID', TIdGenerator.Guid)]
  TPost = class
  private
    [Column('ID', [TColumnProp.Required])]
    FID: TGuid;


    [Association([TAssociationProp.Lazy], CascadeTypeAll - [TCascadeType.Remove])]
    [JoinColumn('User', [], 'ID')]
    FUser: Proxy<TUser>;
    function GetUser: TUser;
    procedure SetUser(const Value: TUser);
  public
    property ID: TGuid read FID write FID;
    property User: TUser read GetUser write SetUser;
  end;


  [Entity]
  [Table('User')]
  [Id('FID', TIdGenerator.Guid)]
  TUser = class
  private
    [Column('ID', [TColumnProp.Required])]
    FID: TGuid;


    [Column('Name', [TColumnProp.Required], 50)]
    FName: string;


    [ManyValuedAssociation([TAssociationProp.Lazy], [TCascadeType.SaveUpdate, TCascadeType.Merge], 'FUser')]
    FPosts: Proxy<TList<TPost>>;
    function GetPosts: TList<TPost>;
  public
    constructor Create;
    destructor Destroy; override;
    property ID: TGuid read FID write FID;
    property Name: string read FName write FName;
    property Posts: TList<TPost> read GetPosts;
  end;


implementation


{ TPost }


function TPost.GetUser: TUser;
begin
  result := FUser.Value;
end;


procedure TPost.SetUser(const Value: TUser);
begin
  FUser.Value := Value;
end;


{ TUser }


constructor TUser.Create;
begin
  inherited;
  FPosts.SetInitialValue(TList<TPost>.Create);
end;


destructor TUser.Destroy;
begin
  FPosts.DestroyValue;
  inherited;
end;


function TUser.GetPosts: TList<TPost>;
begin
  result := FPosts.Value;
end;


initialization
  RegisterEntity(TPost);
  RegisterEntity(TUser);


finalization


end.

I'm found, if I add this line: ObjManager.Clear; before finding the User, than this is works!

This command clear the object manager cache?


  ObjManager.Clear;

  User := ObjManager.Find<TUser>(User1.ID);
  Posts := User.Posts;
  Writeln('Find User: "' + User.Name + '", Posts Count:' + Posts.Count.ToString);

Yes, it does clear the cache. When you create the post, it's not automatically added to User.Posts list. And since the User object is already in the cache, when you do Find<TUser> it will just bring the existing instance in the manager, without hitting the database.


You can alternatively use 
ObjManager.Refresh(User)