ObjectAlreadyPersistent: Cannot turn persistent object

I have the following entitles:

  [Entity]
  [Table('QUsurvey')]
  [Id('FSID', TIdGenerator.IdentityOrSequence)]
  TQUsurvey = class
  private
    [Column('SID', [TColumnProp.Required, TColumnProp.NoInsert, TColumnProp.NoUpdate])]
    FSID: Integer;
  public
    property SID: Integer read FSID write FSID;
  end;


  [Entity]
  [Table('QUframe')]
  [Id('FFID', TIdGenerator.IdentityOrSequence)]
  TQUframe = class
  private
    [Column('FID', [TColumnProp.Required, TColumnProp.NoInsert, TColumnProp.NoUpdate])]
    FFID: Integer;
    [Association([TAssociationProp.Required], CascadeTypeAll - [TCascadeType.Remove])]
    [JoinColumn('SID', [TColumnProp.Required], 'SID')]
    Fsurvey: TQUSurvey;
  public
    property FID: Integer read FFID write FFID;
    property survey: TQUsurvey read Fsurvey write Fsurvey;
  end;


  [Entity]
  [Table('QUquestion')]
  [Id('FQID', TIdGenerator.IdentityOrSequence)]
  TQUquestion = class
  private
    [Column('QID', [TColumnProp.Required, TColumnProp.NoInsert, TColumnProp.NoUpdate])]
    FQID: Integer;
    [Association([TAssociationProp.Required], CascadeTypeAll - [TCascadeType.Remove])]
    [JoinColumn('SID', [TColumnProp.Required], 'SID')]
    Fsurvey: TQUsurvey;
    [Association([TAssociationProp.Required], CascadeTypeAll - [TCascadeType.Remove])]
    [JoinColumn('FID', [TColumnProp.Required], 'FID')]
    Fframe: TQUframe;
  public
    property QID: Integer read FQID write FQID;
    property survey: TQUsurvey read Fsurvey write Fsurvey;
    property frame: TQUframe read Fframe write Fframe;
  end;

Other not related files are deleted. I am using TXDataClient to do all functions to the db.
I am creating a new TQUframe and I assign in TQUframe.survey the TQUsurvey I have requested before. I can POST the new TQUframe and everything is OK

Now I am creating a TQUquestion and I assign in TQUquestion.frame the existing TQUframe I requested before and in TQUquestion.survey the TQUsurvey I have also requested before.
I try to POST the new TQUquestion and I am getting:

ObjectAlreadyPersistent: Cannot turn persistent object of class TQUsurvey with id 1. The object is already in persistent context.

What am I doing wrong?
Thank you in advance

I can only add that with TQUquestion.FID=0 and assigned TQUquestion.survey and TQUquestion.frame, I can issue a TXDataClient.Put() and the TQUquestion is saved without a returned error.
Can I issue a TXDataClient.Put() with a zero TQUquestion primary index to be inserted?

I got confused with you second message. Can you please be more specific?
Regarding the error described in first message, it should not occur. Are you using an old XData server?

In old ones, it wouldn't merge different objects in the three of same class and same id. But that's not the case for a long time.

To make sure, you should set the PostMode property of your TXDataServer component (or TXDataServerModule) to TPostMode.Replicate.

No I am using last version of Xdata with Delphi 11
In the second message, I rewrite it more clearly:
I am creating a newTQUquestion object instance with TQUquestion.FID=0;
I assign in TQUquestion.frame the existing TQUframe object instanceI requested before and in TQUquestion.survey the TQUsurvey object instance I have also requested before.
I get the above error with TXDataClient.post, but it does not give error and it is created with TXDataClient.PUT

Well, have you at least tried to set the property I mentioned?
If that doesn't work, we will need a sample project reproducing the issue so we have a better idea about what's going on.

Yes it works

1 Like

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