Creating an entity

Please check our video tutorial on creating a new JHipster application!


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:

  • A database table
  • A Liquibase change set
  • A JPA Entity
  • A Spring Data JPA Repository
  • A Spring MVC REST Controller, which has the basic CRUD operations
  • An AngularJS router, a controller and a service
  • An HTML view
  • Integration tests, to validate everything works as expected
  • Performance tests, to see if everything works smoothly

If you have several entities, you will likely want to have relationships between them. For this, you will need:

  • A database foreign key
  • Specific JavaScript and HTML code for managing this relationship

The "entity" sub-generator will create all the necessary files, and provide a CRUD front-end for each entity (see project structure).

Entity fields

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:

  • You cannot use Java reserved keywords (as your code will not compile)
  • You cannot use PostgreSQL reserved keywords (as your database schema update will fail)


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:

  • Spring MVC REST controllers (using the @Valid annotation)
  • Hibernate/JPA (entities are automatically validated before being saved)

Validation information will also be used to generate more precise database column metadata:

  • Required fields will be marked non-nullable
  • Fields which have a maximum length will have the same column length

Validation has a few limitations:

  • We don't support all validation options from AngularJS and Bean Validation, as we only support those which are common to both APIs
  • Regular Expression patterns don't work the same in JavaScript and in Java, so if you configure one, you might need to tweak one of the generated patterns
  • JHipster generates unit tests that work for generic entities, without knowing your validation rules: it is possible that the generated tests do not pass the validation rules. In that case, you will need to update the sample values used in your unit tests, so that they pass the validation rules.

Entity relationships

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:

Generating an entity a second time

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:

  • You want to reset your code to its original state
  • You have updated JHipster, and would like to have your entity generated with the new templates
  • You have modified the .json configuration file (the format is quite close to the questions asked by the generator, so it's not very complicated), so you can have a new version of your entity
  • You have copy/pasted the .json file, and want a new entity that is very close to the copied entity


This is a short tutorial on creating two entities (a Author and a Book) which have a one-to-many relationship.

Generate the "Author" entity

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:

  • a "name" of type "String"
  • a "birthDate" of type "LocalDate"

Then answer the questions concerning the relationships, the author has:

  • A one-to-many relationship with the "book" entity (which doesn't exist yet)

Generate the "Book" entity

yo jhipster:entity book

Answer the next questions concerning the fields of this entity, the book has:

  • a "title", of type "String"
  • a "description", of type "String"
  • a "publicationDate", of type "LocalDate"
  • a "price", of type "BigDecimal"

Then answer the questions concerning the relationships, the book:

  • Has many-to-one relationship with the "author" entity
  • And this relationship uses the "name" field (from the Author entity) to be displayed

Check the generated code

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.

Improve the generated code

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.

You're done!

Your generated CRUD page should look like this: