generator-pyhipster

JDL

Using files

You can use JDL files to generate entities:

and Voilà, you are done!

If you work in a team, perhaps you would like to have multiple files instead of one. We added this option so that you don’t manually concatenate all the files into one, you have to run:

pyhipster jdl my_file1.jdl my_file2.jdl

Datatypes

The following data types are available in JDL

Entity Definition

The entity declaration is done as follows:

entity <entity name> {
  <field name> <field type> <validation>
}

Basic example

entity A

This is equivalent to:

entity A(a) {}

The former the simpler form, without specifying a “body” (braces for fields) and a table name.


With fields

entity A {
  name String
  age Integer
}

With field validations

entity A {
  name String required
  age Integer
}

Blob declaration

JHipster gives a great choice as one can choose between an image type or any binary type. JDL lets you do the same. Create a custom type (see DataType) with the editor, name it according to these conventions:

And you can create as many DataTypes as you like.

Enumerations

Enumerations are types with fixed values:

enum Type {
  A,
  B(b)
}
entity E {
  name Type
}

Notice how enumeration’s values are optional. They only have one validation: required.

Basic example

enum Country {
  BELGIUM,
  FRANCE,
  ITALY
}

And its use:

enum Country {}
entity A {
  country Country
}

Relationships

Relationships between entities are also available and are declared with the relationship keyword.

entity A
entity B

relationship OneToOne {
  A{a} to B{b}
}

Here’s what we can see:

There are four relationship types:

Multiple relationship bodies

If you’re tired of having n relationships of the same type in your JDL file, don’t worry! There’s a solution.

Take this JDL sample for instance:

relationship OneToOne {
  A to B
}
relationship OneToOne {
  B to C
}
relationship OneToOne {
  C to D
}
relationship OneToOne {
  D to A
}

The solution consists in having every relationship body inside on relationship declaration, like this:

relationship OneToOne {
  A to B,
  B to C,
  C to D,
  D to A
}

This syntax is really useful when:


Syntax

Relationship declaration is done as follows:

relationship (OneToMany | ManyToOne | OneToOne | ManyToMany) {
  <from entity>[{<relationship name>[(<display field>)]}] to <to entity>[{<relationship name>[(<display field>)]}]
}

Examples

Basic example

relationship OneToOne {
  A to B
}

Note that this example is the same as:

relationship OneToOne {
  A{b} to B{a}
}

Not specifying an injected field is the short form of having a bidirectional relationship.

Another example:

relationship OneToOne {
  A{b} to B
}

This will generate a unidirectional relationship. You can only find entity B through entity A, but you cannot find entity A through entity B.


With injected fields

relationship ManyToMany {
  A{b} to B{a}
}

This is a bidirectional relationship, meaning that both entities will be generated with an “instance” of the other entity.

Commenting

Commenting Entities

Commenting is possible in the JDL for entities and fields, and will generate documentation (Javadoc or JSDoc, depending on the backend).

/**
 * This is a comment
 * about a class
 * @author Someone
 */
entity A {
  /**
   * This comment will also be used!
   * @type...
   */
   name String
   age Integer // this is yet another comment
}

The JDL possesses its own kind of comment:

Therefore, anything that starts with // is considered an comment for JDL. Please note that the JDL Studio directives that start with # will be ignored during parsing.

Please note, commas are not mandatory but it’s wiser to have them so as not to make mistakes in the code.

Commenting

Adding comments for relationships is possible:

relationship OneToOne {
  /** This comment will be put before b in entity A*/
  A{b}
  to
  /** This comment will be put before a in entity B*/
  B{a}
}

The same commenting rules as entities are applied here.