Blog
All Blog Posts | Next Post | Previous PostCrash Course TMS Aurelius - Associations (Foreign Keys)
Monday, February 18, 2013
Besides mapping tables to classes and table columns to fields/properties, Aurelius also maps relationships (foreign keys) to object associations. One nice thing about Aurelius is that such associations are defined in a very simple way: just references to other objects. Consider the following classes with respective mapping:type [Entity, Automapping] TCountry = class private FId: integer; FName: string; public property Id: integer read FId write FId; property Name: string read FName write FName; end; [Entity, Automapping] TCustomer = class private FId: integer; FName: string; FCountry: TCountry; public property Id: integer read FId write FId; property Name: string read FName write FName; property Country: TCountry read FCountry write FCountry; end;
function CreateCustomerWithCountry(Manager: TObjectManager): integer; var Customer: TCustomer; USACountry: TCountry; begin USACountry := TCountry.Create; USACountry.Name := 'USA'; Customer := TCustomer.Create; Customer.Name := 'John'; Customer.Country := USACountry; Manager.Save(Customer); Result := Customer.Id; end;
It's also very simple to retrieve an object and its associations from database. Consider the following code that takes a customer id and returns the name of the country associated with the customer:
function GetCountryNameFromCustomer(Manager: TObjectManager; CustomerId: integer): string; var Customer: TCustomer; begin Customer := Manager.Find<TCustomer>(CustomerId); if Customer <> nil then Result := Customer.Country.Name else Result := ''; end;
Associations are a core feature of any ORM framework and this small example is a very simple one. Aurelius has many features related to associations, many ways of dealing with them, saving, retrieving, etc. But the purpose of this blog post is just to explain the concept. Feel free to ask questions in comment about what else you would like to be better explained in a future blog post.
To make it even more clear, I will post here the SQL statements executed by Aurelius when the code above was executed, so you can easily relate the objects with the underlying database. The statements used here were executed in an SQL Server database (syntax will be different if using another database).
The following statements were executed to create the tables so you can have an idea of the database structure (code to create the database is not explicit in this post):
CREATE TABLE COUNTRY ( ID INTEGER IDENTITY(1,1) NOT NULL, NAME VARCHAR(255) NOT NULL, CONSTRAINT PK_COUNTRY PRIMARY KEY (ID)); CREATE TABLE CUSTOMER ( ID INTEGER IDENTITY(1,1) NOT NULL, NAME VARCHAR(255) NOT NULL, COUNTRY_ID INTEGER NULL, CONSTRAINT PK_CUSTOMER PRIMARY KEY (ID)); ALTER TABLE CUSTOMER ADD CONSTRAINT FK_CUSTOMER_COUNTRY_COUNTRY_ID FOREIGN KEY (COUNTRY_ID) REFERENCES COUNTRY (ID)
INSERT INTO COUNTRY (NAME) VALUES (:A_NAME); A_NAME = "USA" (ftString) SELECT CAST(IDENT_CURRENT('COUNTRY') AS INT); INSERT INTO CUSTOMER ( NAME, COUNTRY_ID) VALUES ( :A_NAME, :A_COUNTRY_ID); A_NAME = "John" (ftString) A_COUNTRY_ID = "1" (ftInteger) SELECT CAST(IDENT_CURRENT('CUSTOMER') AS INT)
SELECT A.ID AS A_ID, A.NAME AS A_NAME, A.COUNTRY_ID AS A_COUNTRY_ID, B.ID AS B_ID, B.NAME AS B_NAME FROM CUSTOMER A LEFT JOIN COUNTRY B ON (B.ID = A.COUNTRY_ID) WHERE A.ID = :p_0 p_0 = "1" (ftInteger)
Wagner Landgraf
This blog post has not received any comments yet.
All Blog Posts | Next Post | Previous Post