The relationship between entities can be declared using this syntax:
relationship <type> {
<from entity>[{<relationship name>[(<display field>)] <validation>*}]
to
<to entity>[{<relationship name>[(<display field>)] <validation>*}]
}
The <type> is one fromĀ OneToMany, ManyToOne, OneToOne, or ManyToMany and as the name suggests, declares the relationship type between <from entity> and <to entity>.
<from entity> is the name of the owner entity of the relationship or the source. <to entity> is the destination of the relationship.
<relationship name> is optional and can be used to specify the field names to create for the relationship in the domain object. <display field> can be specified in braces to control the field of the entity to be shown in the drop-down menu on the generated web page, by default the ID field will be used. <validation> can be specified on the <from entity> or <to entity> and is optional. Currently, only required is supported.
OneToMany and ManyToMany relationships are always bidirectional in JHipster. In case of ManyToOne and OneToOne relationships, it is possible to create both bidirectional and unidirectional relationships. For unidirectional relationships, just skip theĀ <relationship name> on the destination/to entity.
Multiple relationships of the same type can be declared within the same block, separated by a comma.
An example relationship declaration would look like the following:
entity Book
entity Author
entity Tag
relationship OneToMany {
Author{book} to Book{writer(name) required},
Book{tag} to Tag
}
The user is an existing entity in JHipster and it is possible to have certain relationships with the user. Many-to-many and one-to-one relations can be declared, but the other entity must be the source or owner. Many-to-one relations are also possible with a user entity.