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 (defaultfalse
). -
[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 (anid
, aname
of typeString
and acapacity
of typeResourceCapacity
). -
the primary key of a
Resource
entity is built fromid
. TheResourceCapacity
entity has two attributes (quantity
of typeInteger
andresource
of typeResource
). -
the primary key of a
ResourceCapacity
entity is built from the primary key of the attachedResource
.
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
andname
of typeString
,durationInHours
of typeInteger
, whilesuccessors
andpredecessors
are arrays of typePrecedence
) -
the
Precedence
entity has two attributes (first
andsecond
of typeActivity
) that cannot be null -
the primary key of a
Precedence
entity is built from the primary keys of the attachedfirst
andsecond
activities.When using the generated java code, setting an activity in the
first
attribute of a precedence will automatically add the precedence in thesuccessors
list of that activity. Similarly, removing a precedence from thesuccessors
list of an activity will automatically set thefirst
attribute of that precedence tonull
.