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

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.

If you created your application with MongoDB instead of a more classical SQL/JPA solution, JHipster will still generate your entities correctly (you will have MongoDB documents instead of JPA entities), but of course you won't be able to have relationships between your entities.

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: