Requesting a Single Entity with composite id using TXDataClient

I can get a Single Entity using the following commands:

Customer := Client.Get<TCustomer>(10);
Invoice := Client.Get<TInvoice, TGuid>(InvoiceId);

Does it accept variants for Composite IDs with the second command?

For an entity like the following:

  [Entity]
  [Table('mytable')]
  [IdUnsavedValue(-1)]
  [Id('Ftabl2', TIdGenerator.None)]
  [Id('Fid2', TIdGenerator.None)]
  Tmydataactoradrs = class
  private
    [Association([TAssociationProp.Required], CascadeTypeAll - [TCascadeType.Remove])]
    [JoinColumn('id1', [TColumnProp.Required], 'id1')]
    Ftabl2: Tmytable2;
    [Column('id2', [TColumnProp.Required])]
    Fid2: Integer;

Using the following command:
afmact:=client.Get<Tmytable,variant>(vararrayof([id1,id2]));

I am getting:
exception class ECannotBuildKeyPredicateWrongValueLength with message 'Cannot create id predicate for type "XData.Default.mytable". The length of id value does not match number of elements in composite key'.

I finally created a service function for the above:

function Tmyinterface.GetEntity(const classnam:string; const ids:TArray<Integer>): Tobject;
var obj:Tobjectmanager; clazz:tclass; ctx: TRttiContext; typ: TRttiType; list: TArray<TRttiType>;
begin 
obj:=TXDataOperationContext.Current.GetManager; 
clazz:=getclass(classnam);
if not assigned(clazz) then begin
 ctx:=TRttiContext.Create; try
   list:=ctx.GetTypes;
    for typ in list do
     begin
      if typ.IsInstance and (EndsText(classnam, typ.Name)) then
        begin
          clazz:=typ.AsInstance.MetaClassType;
          break;
        end;
    end;
finally ctx.Free; end;
end;
if assigned(clazz) then result:=obj.Find(clazz,ids) else result:=nil; end;

If the entity class can be retrieved from XData module without querying the Rtti, it could be better.
Having registered teh class with Registerclass, it can retrieved with just GetClass(classnam)

It's possible, the Get method expects an array of TValue. Example:

var
  A: TIdAnimal;
  V: TArray<TValue>;
begin
  SetLength(V, 2);
  V[0] := 5;
  V[1] := 'Five';
  A := XClient.Get<TIdAnimal, TArray<TValue>>(V);
end;
1 Like

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