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
Resourceentity has three attributes (anid, anameof typeStringand acapacityof typeResourceCapacity). -
the primary key of a
Resourceentity is built fromid. TheResourceCapacityentity has two attributes (quantityof typeIntegerandresourceof typeResource). -
the primary key of a
ResourceCapacityentity 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
Activityentity has five attributes (idandnameof typeString,durationInHoursof typeInteger, whilesuccessorsandpredecessorsare arrays of typePrecedence) -
the
Precedenceentity has two attributes (firstandsecondof typeActivity) that cannot be null -
the primary key of a
Precedenceentity is built from the primary keys of the attachedfirstandsecondactivities.When using the generated java code, setting an activity in the
firstattribute of a precedence will automatically add the precedence in thesuccessorslist of that activity. Similarly, removing a precedence from thesuccessorslist of an activity will automatically set thefirstattribute of that precedence tonull.