2017 June Release

Defining relationsPermanent link for this heading

To define a connection between two classes use the keyword relation. This relation can have 1:1, 1:n or n:m cardinality. A relation can have its own properties.

A special case is a “self-relation”, when the two related classes are identic.

Syntax

// relation without properties
// 1:1 relation
relation<classreferencea, classreferenceb> relationreference;

// relation with properties
// 1:n relation, use [] to denote cardinality
relation<classreferencea, classreferenceb[]> relationreference {
  ...
}

//self-relation
relation<classreference> relationreference;


In order to facilitate the use of relations app.ducx implicitly defines the necessary attributes to have access from the relation instance to the class instances and from class instances to the relation instance. For the relation<ClassA, ClassB> Relation the implicitly defined attributes have the following names:

Name

Container

Type

relation_classa

Relation

ClassA

relation_classb

Relation

ClassB

classa_relation

ClassA

Relation

classb_relation

ClassB

Relation

Table 9: Implicitly defined attributes for a relation

In case of the self-relation relation<Class> Relation:

Name

Container

Type

relation_class1

Relation

Class

relation_class2

Relation

Class

class_relation

Class

Relation

Table 10: Implicitly defined attributes for a self-relation

If ClassA is marked as multiple, then classb_relation is a list: instances of ClassB can have a set of Relation instances with different instances of ClassA.

Example

objmodel APPDUCXSAMPLE@200.200
{
  import COOSYSTEM@1.1;

  //1:n relation:
  //more persons are hired at a company, one person can be hired at one company
  relation<Company, Person[]> Employee {
    date membersince;
    integer contractid;
  }

  //n:m relation:
  //more persons can work on a project, one person can work on multiple projects
  relation<Project[], Person[]> ProjectMember;

  // self-relation
  // 1:1 relation: each person can be married to exactly one other person
  relation<Person> Marriage;


For instance, assume at a company several employees can be hired. This can be modeled as a 1:n relation called Employee between the Company and Person class. In our case, the contractid and membersince properties could describe the Employee relation.