Mapping Person-User

Hi, I'm having a problem with a mapping like this:


[Entity]
[Automapping]
[ID('FID', TIdGenerator.Uuid38)]
TPerson = class
private
  [Column('PERSON_ID')]
  FID: string;
  FName: string;
  [JoinColumn('PERSON_ID')]
  FUser: TUser;
public
  property ID: string read FID write FID;
  property Name: string read FNome write FNome;
  property User: TUsuario read FUser write FUser;
end;


[Entity]
[Automapping]
[ID('FID', TIdGenerator.Uuid38)]
TUser = class
private
  [Column('USER_ID')] 
  FID: string; 
  FLogin: string;
  FPass: string;
public
  property ID: string read FID write FID;
  property Login: string read FLogin write FLogin; 
  property Pass: string read FPass write FPass;
end;




USER_ID (TUser) is a PK/FK referencing the table Person field PERSON_ID. I want to list the user of the person (Person.User.ID). It's possible to do this way instead of using only the TUser?

There is ForeignJoinColumn attribute, but I can only use it with ManyValuedAssociation. In this specific case, is not ManyValuedAssociation.

Sorry, but I didn't understand what you are trying to do. Can you explain again, maybe using some examples?

Sure.

This mapping works fine on Find. I can get the results and see the results Ok. But the problem it's on the moment that I try to Save, Update or Flush the object.

Tables:

PERSON
PERSON_ID -> PK
NAME


USER
USER_ID -> PK/ FK (reference Person(PERSON_ID))
LOGIN
PASS


Person
PERSON_ID| NAME
123      | Lucas 


User
USER_ID | Login | Pass
123     | lucas | lucas


When I do:

Person := OM.Find<TPerson>('123');


I get the result. I can see all the data on Person where ID = 123.

But when I do:

 Person.Name := 'New Name';

And try to save/update/flush, I get an error because PERSON_ID is getting added twice at the SQL generated by Aurelius.

I can not get the exactly error message right now and the generated SQL code, because I'm out of the office.

Your mapping is wrong. If you want User to reference Person, your mapping should be:


public
  property ID: string read FID write FID;
  property Login: string read FLogin write FLogin; 
property Pass: string read FPass write FPass;
end;

Hi Wagner, thanks.


I want Person to reference User. This way:  Person.User.Login := 'test';
Is it possible?

Yes

Using JoinColumn?

Not sure what do you mean by "JoinColumn"? Just use the objects. You don't need to use join column if you use Automapping. Just use it without any attribute, and everything will be automatic for you:


[Entity]
[Automapping]
[ID('FID', TIdGenerator.Uuid38)]
TPerson = class
private
  FID: string;
  FName: string;
  FUser: TUser;
public
  property ID: string read FID write FID;
  property Name: string read FNome write FNome;
  property User: TUser read FUser write FUser;
end;

[Entity]
[Automapping]
[ID('FID', TIdGenerator.Uuid38)]
TUser = class
private
  FID: string; 
  FLogin: string;
  FPass: string;
public
  property ID: string read FID write FID;
  property Login: string read FLogin write FLogin; 
  property Pass: string read FPass write FPass;
end;

I mean to use JoinColumn on TPerson to set User.


Btw, this way not works. I get the error:


Dynamic SQL Error
SQL error code = -206
Column unknown
C.USER_USER_ID

You don't use internal foreign key columns in Aurelius. You only deal with objects. This is very very basic Aurelius usage and understanding. I suggest you try some code there, with SQLite, and if you are having problems, just send the exact code you are trying, if possible some compilable project to reproduce the problem.

Ok Wagner, thanks.