Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Associations

Associations define relationships between entities. They determine how objects reference each other and control behavior on deletion.

Association Types

TypeMDL KeywordCardinalityDescription
ReferenceReferenceMany-to-OneMany objects on the FROM side reference one object on the TO side
ReferenceSetReferenceSetMany-to-ManyObjects on both sides can reference multiple objects

CREATE ASSOCIATION

[/** <documentation> */]
CREATE ASSOCIATION <Module>.<Name>
  FROM <ParentEntity>
  TO <ChildEntity>
  TYPE <Reference|ReferenceSet>
  [OWNER <Default|Both|Parent|Child>]
  [DELETE_BEHAVIOR <behavior>]

Reference (Many-to-One)

A Reference association means each object on the FROM side can reference one object on the TO side. Multiple FROM objects can reference the same TO object.

/** Order belongs to Customer (many-to-one) */
CREATE ASSOCIATION Sales.Order_Customer
  FROM Sales.Customer
  TO Sales.Order
  TYPE Reference
  OWNER Default
  DELETE_BEHAVIOR DELETE_BUT_KEEP_REFERENCES;

ReferenceSet (Many-to-Many)

A ReferenceSet association allows multiple objects on both sides to reference each other:

/** Order has many Products (many-to-many) */
CREATE ASSOCIATION Sales.Order_Product
  FROM Sales.Order
  TO Sales.Product
  TYPE ReferenceSet
  OWNER Both;

Owner Options

The owner determines which side of the association can modify the reference.

OwnerDescription
DefaultChild (FROM side) owns the association
BothBoth sides can modify the association
ParentOnly parent (TO side) can modify
ChildOnly child (FROM side) can modify

Delete Behavior

Controls what happens when an associated object is deleted.

BehaviorMDL KeywordDescription
Keep referencesDELETE_BUT_KEEP_REFERENCESDelete the object, set references to null
CascadeDELETE_CASCADEDelete associated objects as well
/** Invoice must be deleted with Order */
CREATE ASSOCIATION Sales.Order_Invoice
  FROM Sales.Order
  TO Sales.Invoice
  TYPE Reference
  DELETE_BEHAVIOR DELETE_CASCADE;

Naming Convention

Association names typically follow the pattern Module.FromEntity_ToEntity:

Sales.Order_Customer       -- Order references Customer
Sales.Order_Product        -- Order references Product
Sales.Order_Invoice        -- Order references Invoice

DROP ASSOCIATION

Remove an association:

DROP ASSOCIATION Sales.Order_Customer;

See Also

  • Domain Model – overview of domain model concepts
  • Entities – the entities that associations connect
  • Generalization – inheritance (a different kind of relationship)