JHipster Series 2: Add Entities into JHipster Application
By: Mascova Blog
Tags: Java, Spring Boot, Angular, Jhipster

In the last story, we’ve talked about how JHipster can generate the code fast, easy in no time for you to bootstrapped your application.

Adding Entities Into Your Existing Application

In previous scenario, the application just run empty. No data we can add, edit, or delete. In this scenario, we can try to add entity (this is what we called for a set of data view, logic, and storage with Create Read Update Delete capabilities) into your existing (empty) application, also in no time!

Entity-Relationship Definition

First, since in this example we’re using a SQL Database, you probably need to design your data first in the form of ERD (Entity Relationship Diagram) before going to code.

Just for the sample, we are going to represent to structure of a Kingdom in the format of data relationship, like this:

For every kingdom has a population, let’s call it citizen, is what moves the economy in the kingdoms. Citizen needs some Goods to be consumed, let’s say like food, clothes, raw material for building their house, and any other kind goods. So we can said that it has relationship with an entity Goods.

Citizen can consume one or more Goods, and Goods also can be consumed by all variety of Citizen, this is what we call a ManyToMany relationships.

Goods can be categorized into a smaller definition of words, let’s just like like food, apparel, raw material, etc. Good can only be has one Category and must have one, and Category can have en empty list of Good (since it’s probably a new Category just being created), but Category can have many list of Goods, this is what we call OneToMany relationships.

Goods must have a price, since it will be traded. Goods can only have one price but there is a time that Goods haven’t got a price tag yet (since it just being produced), but Price is definitely belong only to one Goods and must have a relationship with Goods. This is what we call a OneToOne relationships.

Coin is our tools of exchange when we do trade later. This Coin entity belong to the Kingdom, there is no relationship whatsoever of it being with Citizen. In the future, when Goods is buyed or selled, the Coin will also added and substracted accordingly. Even though it have some sort connection with Goods, it does not have relationship between both two. For example: Good can be added, but the Coin amount doesn’t added up. Or Coin is added, but since it is a donation and not from selling, Goods amount doesn’t changed.

Let’s keep Coin a solitary Entity for this moment, as there is a chance that we can add entities later that will probably make us to revamp the relationship again.

JHipster Definition Language

JHipster have what is we called JDL (JHipster Definition Language). It is basically a descriptor file defining your application, deployment, and also entity and relationship (yes, you can even define your application without have to going through generator like we described in previous story). Our objectives is to change (since we already have a freshly new and empty application, so we assume it has already have JDL file in it’s project directory).

Let’s go toJHipster Online again and go to Design Entities Menu:

Press Create a new JDL model button in the upper-right of the page. You will see the editor (you can select template to default sample if you want to see the example):

After satisfied seeing the example and understanding a little bit about JDL entities field, we can erase the example codes and try to write all of our entities designed before, just a bare one first:

Let’s us add fields like code and name to Citizen, Good, and Category since it’s probably need ones. We can added some timestamp by adding createdDate and modifiedDate to Citizen, Good, Category, and Price.

We define sellPrice and buyPrice for Price. We really haven’t had any more imagination on Coin entities rather than it will only saving a single values, which is value (what our currently coin we have currently at the exact moment).

Our current diagram so far:

After defining the entities, we can start add the relationship as we defined in our previous diagram:

Our final (yet) diagram:

Let us saved it for now. Click the Disk icon to save it:

Give it with our project name:

After it saved, you can choose our saved project in

You can also download the JDL file if you want to save it in local or import it through CLI:

Appying Changes Into Our Application

Let’s go toJHipster Online again and go to Design Entities Menu. Find the row where elastickingdom project is and click Apply button:

Choose your previous git project and click “Yes, apply this JDK model” button:

If the apply process is success, you can see the result as git branch information below:

Let’s try run that particular branch mentioned above (jhipster-entities-elastickingdom-51002588417588) by pulling it to our local:

and check it out:

After a successful checkout, don’t forget to clean the project:

mvn clean

and probably recreate the database in PostgreSQL. Theorically, JHipster can update the database automatically. But there will happened a lot when your database is locked by the liquibase, it is probably simpler by recreating the database (make sure the database is empty again, since there will be a change in the database structure).

After all is set, run the application:

After successful deployment:

Go to http://localhost:8080/ to see the update:

logged is using admin user:

after a successful login:

You can see that the Entities menu now have the entities that you’ve created before:

Try to explore the entities! (JHipster has deliberately populate a fake data for you).