Once you have created your application, you will want to create entities. For example, you might want to create an Author and a Book entity. For each entity, you will need:
If you have several entities, you will likely want to have relationships between them. For this, you will need:
The "entity" sub-generator will create all the necessary files, and provide a CRUD front-end for each entity (see project structure).
For each entity, you can add as many fields as you want. You will need to input the field names and their types, and JHipster will generate for you all the required code and configuration, from the AngularJS HTML view to the Liquibase changelog.
Those fields cannot contain reserved keywords in the technologies you are using. For example, if you use MySQL:
JHipster supports many field types. This support depends on your database backend, so we use Java types to describe them: a Java String
will be stored differently in Oracle or Cassandra, and it is one of JHipster's strengths to generate the correct database access code for you.
String
: A Java String. Its default size depends on the underlying backend (if you use JPA, it's 255 by default), but you can change it using the validation rules (putting a max
size of 1024, for example).Integer
: A Java Integer.Long
: A Java Long.Float
: A Java Float.Double
: A Java Double.BigDecimal
: A java.math.BigDecimal
object, used when you want exact mathematic calculations (often used for financial operations).LocalDate
: A java.time.LocalDate
object, used to correctly manage dates in Java.ZonedDateTime
: A java.time.ZonedDateTime
object, used to correctly manage dates and times in Java.Boolean
: A Java Boolean.enum
: A Java Enumeration object. When this type is selected, the sub-generator will ask you what values you want in your enumeration, and it will create a specific enum
class to store them.byte[]
: A Blob object, used to store some binary data. When this type is selected, the sub-generator will ask you if you want to store some generic binary data, or an image object. Images will be handled specifically on the AngularJS side, so they can be displayed to the end-user.Validation can be set up for each field. Depending on the field type, different validation options will be available.
Validation will be automatically generated on:
Bean validation will then be used to automatically validate domain objects when they are used in:
@Valid
annotation)Validation information will also be used to generate more precise database column metadata:
Validation has a few limitations:
Entity relationships are only available for SQL databases. It is a fairly complex subject, which has its own documentation page: Managing relationships.
By default JHipster entities do not use DTOs, but they are available as an option. Here is the documentation: Using DTOs.
Please note that pagination is not available if you created your application with Cassandra. Of course this will be added in a future release.
Pagination uses the Link header, as in the GitHub API. JHipster provides a custom implementation of this specification on both the server (Spring MVC REST) and client (AngularJS) sides.
When the entity is generated, JHipster provides 4 pagination options:
The entity configuration is saved in a specific .json file, in the .jhipster
directory. So if you run again the sub-generator, using an existing entity name, your entity will be re-generated.
You might want to do this for the following reasons:
TIP : to regenerate all your entities at once, you can use the following commands (remove the --force
to have questions asked when files have changed).
for f in `ls .jhipster`; do yo jhipster:entity ${f%.*} --force ; done
for %f in (.jhipster/*) do yo jhipster:entity %f --force
This is a short tutorial on creating two entities (a Author and a Book) which have a one-to-many relationship.
As we want to have a one-to-many relationship between Authors and Books (one author can write many books), we need to create the Author first. At the database level, JHipster will then be able to add a foreign key on the Book table, linking to the Author table.
yo jhipster:entity author
Answer the next questions concerning the fields of this entity, the author has:
Then answer the questions concerning the relationships, the author has:
yo jhipster:entity book
Answer the next questions concerning the fields of this entity, the book has:
Then answer the questions concerning the relationships, the book:
Run the generated test suite, with mvn test
, which will test the Author entity and the Book entity.
Launch the application (for example with mvn
), log in and select the "Author" and "Book"
entities in the "entities" menu.
Check the database tables, to see if your data is correctly inserted.
The generated files contain all the basic CRUD operations, and don't need to be modified if your needs are simple.
If you want to modify the generated code or the database schema, you should follow our development guide
If you want some more complex business behaviors, you might need to add a Spring @Service
class, using the service sub-generator.
Your generated CRUD page should look like this: