Creating an entity

Introduction

Once you have created your application, you will want to create entities. For example, you might want to create the Book entity, which represents a book. For this, 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

The "entity" sub-generator will create all the necessary files, and provide a CRUD front-end to the entity.

If you created your application with MongoDB instead of a more classical SQL/JPA solution, JHipster will still generate your entity correctly (you will have a MongoDB document instead of a JPA entity).

Let's create the "Book" entity, which has a title, description, publication date, and price.

Generate the entity

Create the entity with the sub-generator. For instance, for creating the "Book" entity, please type:

yo jhipster:entity book

You must answer the next questions, asking for each field name and type. For example, the "Book" has:

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

This will generate all the required files.

Check the generated CRUD front-end

Run the generated test suite, with mvn test, which will test the new Book entity.

Launch the application (for example with mvn spring-boot:run), log in and select the "Book" entity in the "entities" menu.

Check the database table, 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: