Creating an entity

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

Introduction

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

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 are only available for SQL databases. It is a fairly complex subject, which has its own documentation page: Managing relationships.

Data Transfer Objects (DTOs)

By default JHipster entities do not use DTOs, but they are available as an option. Here is the documentation: Using DTOs.

Pagination

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

Tutorial

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: