Declaring Entity Relationships

There are two kinds of relationships:

  • OneToOne - is used to represent 1:1 relationships between entities.

  • ManyToOne - is used to represent N:1 relationships between entities.

The following relationship DOM tags are recognized and optional:

  • [affects.primary.key] : [true or false] indicates that the primary key of the first end of the relationship is affected by this relationship (default false).

  • [java.isReferenceInverted] : [true or false] indicates whether the relationship should be inverted or not in the generated java code. By default, relationships are inverted.

When a relation is affecting the primary key, the relation is implicitly required (not nullable).

A relation that is not affecting the primary key is by default optional (nullable). To declare that a relation is not nullable, you need to use the required keyword as follows:

  • Precedence { second required } to Activity { predecessors }

For example, the following sample code declares a OneToOne relationship between two entities:

entity Resource {
  // DOM [primary.keys] : [id]
  id String required,
  name String
}

entity ResourceCapacity {
  quantity Integer
}

relationship OneToOne {
  // DOM [affects.primary.key] : [true]
  ResourceCapacity{resource} to Resource{capacity}
}           

This declaration affects the entities as follows:

  • the Resource entity has three attributes (an id, a name of type String and a capacity of type ResourceCapacity).

  • the primary key of a Resource entity is built from id. The ResourceCapacity entity has two attributes (quantity of type Integer and resource of type Resource).

  • the primary key of a ResourceCapacity entity is built from the primary key of the attached Resource.

The following example declares a ManyToOne relationship between two entities:

entity Activity {
     // DOM [primary.keys] : [id]
     id String required,
     name String,
     durationInHours Integer
}
   
entity Precedence {
}

relationship ManyToOne {
  // DOM [affects.primary.key] : [true]
  Precedence{first required} to Activity{successors},
  // DOM [affects.primary.key] : [true]
  Precedence{second required} to Activity{predecessors}
}

This declaration affects the entities as follows:

  • the Activity entity has five attributes (id and name of type String, durationInHours of type Integer, while successors and predecessors are arrays of type Precedence)

  • the Precedence entity has two attributes (first and second of type Activity) that cannot be null

  • the primary key of a Precedence entity is built from the primary keys of the attached first and second activities.

    When using the generated java code, setting an activity in the first attribute of a precedence will automatically add the precedence in the successors list of that activity. Similarly, removing a precedence from the successors list of an activity will automatically set the first attribute of that precedence to null.