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.
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 only work for SQL databases. If you created your application with MongoDB or Cassandra instead, JHipster will still generate your entities correctly (you will have MongoDB documents or Cassandra tables instead of JPA entities), but of course you won't be able to have relationships between your entities.
You can create one-to-one
, one-to-many
, many-to-one
and many-to-many
relationships. Please note that all relationships are bi-directional, and that as we use a database (and generate the necessary foreign keys), you will need to generate your entities in a certain order. For example, if there is a "client" who has a one-to-many relationship to an "order", you need to generate the client entity first, so that the "order" foreign key to the "client" table can be set up.
JHipster will ask you the name of your relationship: by default, this is the name of the other entity, but you can write another name if you want to have multiple relationships between two entities.
Please note that the user
entity, which is handled by JHipster, is specific. You can do many-to-one
relationships to this entity (an "order" can have a many-to-one relationship to the "user"). This will generate a specific query in your new entity repository, so you can filter your entity on the current security user, which is a common requirement.
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: