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 PostgreSQL:
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:
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 spring-boot:run
), 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: