Id not set on entity of class...

Hi,


All my entities are derived from a "BaseTable". So, there is always an identity field ID.

  [Automapping]
  TBaseTable = class(TInterfacedObject, IBaseTable)
  private
    FID: Integer;

    [Transient]
    [weak]
    FMemento: IBaseTable;

    [Transient]
    FValidations: TList<string>;
    [Transient]
    FRecordState: TRecordState;

There is a 'Preparations' entity

  [Entity]
  [Table(nme_Table)]
  [Automapping]
  TPreparation = class(TBaseTable, IPreparation)
  private
    ...
    [Association([TAssociationProp.Lazy], [TCascadeType.SaveUpdate])]
    [ForeignKey('FK_Preparation_Characteristic')]
    [JoinColumn(fld_CharacteristicID, [])]
    FCharacteristic: Proxy<TCharacteristic>;

And as you can see there is an 'Characteristic' entity.

  [Entity]
  [Table(nme_Table)]
  [Automapping]
  TCharacteristic = class(TBaseTable, ICharacteristic)
  private
    [Column(fld_NameNL, [], 100)]
    FNameNL: string;

When opening the Preparations table there is an error "Id not set on entity of class TCharacteristic"
Inserting a record in Preparations causes no error.

What am I doing wrong?

Regards,

Filip.

Forgot to mention...

  [Entity]
  [Table(nme_Table)]
  [Automapping]
  [Id('FID', TIdGenerator.IdentityOrSequence)]
  TCharacteristic = class(TBaseTable, ICharacteristic)
  private
    [Column('ID', [TColumnProp.Required])]
    FID: Integer;
    [Column(fld_NameNL, [], 100)]
    FNameNL: string;

I tried this, same result.

Hi Filip,

Mapping attributes in non-entity classes (like TBaseTable) are ignored. Mapping can only happen in entity classes (those which will effectively be persisted, marked with [Entity] attribute. If you have properties inherited from base non-entity classes, you must redeclare those properties in derived class and add the attribute there:

[Column('ID')]
property Id;

Regarding your second post, it's not clear what's going on. Please take into account that sometimes Aurelius gives the "Id not set" error but that happens because it does not consider the class to be an entity (either because [Entity] attribute is not added to the class, or because you are using different mapping setup or using multi-model design and that class is not present in the mapping explorer you're using).

Hi Wagner,


Thanks for your reply.
Unfortunately, this doesn't work. There is no multi-model design, and every 'entity-class' has the [Entity] attribute (and is registered with RegisterEntity)

The error occurs when I list all the records of TPreparations (in which TCharacteristic should be lazy loaded).
Therefore I use a (very simple) repository which does the following...
function TRepository<T>.FindAll: TList<T>;
var
  aCriteria: TCriteria;
begin
  aCriteria := FManager.Find<T>();
  Result := aCriteria.List<T>;
end;

Error occurs when executing the List method.

If it's necessary, I can send you the complete code to investigate.

Regards,

Filip

Filip, yes, please send the code to me through e-mail (wagner at tmssoftware com), I will take a look and let you know what I find out. Just confirming, you did remove the attributes from base class and added the [Id] attribute in the derived class, right?

I would like to know solution for this discussion

The issue happening because the entity classes were initializing the proxy association (not many-valued association) to an empty object. That should not be done, associations should just be left untouched (thus having nil value) upon object instantiation.