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 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 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).

The "entity" sub-generator will help you to create most of this code, but it isn't magical, so you will need customize some of the generated code.

Let's create the "Foo" entity, which has a text field and a date field.

Generate the entity

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

yo jhipster:entity foo

If you have followed our development guide and have your application running with "hot reload", everything should just work out of the box: your running application should be updated, as well as your database and even your browser. Just go to http://localhost:9000/#/foo to use the generated default CRUD page.

If you didn't use hot reload, you will have a little more work to do (see the next part on Liquibase for more information), and of course you will need to restart your application to see your changes.

Check the database table

JHipster uses Liquibase to update its database schema. The Liquibase configuration is located in the src/main/resources/config/liquibase folder.

If you have used hot reload in the previous step, JHipster has automatically created a new file named src/main/resources/config/liquibase/changelog/db-changelog--002.xml.

If you didn't use hot reload, you have to create this file yourself (that's why we recommend using hot reload):

  • Add a new Liquibase configuration file called src/main/resources/config/liquibase/changelog/db-changelog--002.xml
  • Add a new "changeSet" XML node (and don't forget to increment its version!)
  • Describe this new "changeSet": you can look at the existing "changeSet", or go to the Liquibase documentation for more information

Customize the generated JPA code

The generated entity has:

  • An auto-generated ID
  • A sample text field called "sampleTextAttribute"
  • A sample date field called "sampleDateAttribute"

Of course, you will have to customize those fields for your specific entities.

Review the generated Java code

JHipster has created a Spring Data JPA repository called FooRepository and a Spring MVC REST Controller called FooResource.

Those files contain all the basic CRUD operations, and don't need to be modified if your needs are simple.

Of course, if you want some more complex business behaviors, you might need to add a Spring @Service class, using the service sub-generator.

Review the generated JavaScript files

JHipster has created three JavaScript files for you:

  • An AngularJS router called src/main/webapp/scripts/foo/router.js
  • An AngularJS controller called src/main/webapp/scripts/foo/controller.js
  • An AngularJS service called src/main/webapp/scripts/foo/service.js

Those files have been automatically added to your index.html file, just after the other existing AngularJS scripts.

Customize the generated HTML view

JHipster has created a foos.html view, which has the basic CRUD operations.

This view will be where most of the work will occur, depending on the attributes you have added to your entity, and your business requirements.

You're done!

You can test your "foo" controller on http://localhost:8080/#/foo.

It should look like this: