Who says you can’t do rapid application development in Java? Part Two: Creating our entities, migrating the database, and allowing users to add reviews

In Part One of this tutorial, I covered  how to install JHipster and  use it to generate a Monolithic Spring Boot API with an Angular front end. The app that JHipster generated already includes a lot of great authentication and authorization features, but now it’s time to add the entities that will make our app unique.

JHipster uses Liquibase to manage database migrations, so feel free to add a migration and entity manually if you feel comfortable doing  so. But to follow our theme of rapid application prototyping, we will use JDL Studio, a web application that lets you visualize your database schema and then import it into JHipster. The CLI for JHipster will then allow you to generate the appropriate migrations, entities, JPA repositories, AND front-end components.

The purpose of our application is to create, read, edit, and delete information about pets, pet foods, and reviews of how well each food works for each pet. So let’s start with three basic entities:

entity Pet {
name String
description String
slug String

entity Food {
name String
description String

entity FoodReview {
title String
body String
rating FoodRating

In other words, we create an entity by typing “entity <EntityName>”, and then we add some curly braces to define the entity’s fields. Each field is defined by writing a lowercase field name, followed by an upper-CamelCase field type. You may notice that one of our field types is an undefined FoodRating. Add that as an enum below:

enum FoodRating {



Our application will need some relationships between our entities if we want to have any meaningful way of browsing reviews for a particular pet and food. Create them with this snippet:

relationship ManyToOne {
FoodReview{author} to User,
FoodReview{food} to Food{review},
FoodReview{pet} to Pet{foodReview}

As you can see, we define all our relationships in one place. JDL Studio works best with ManyToOne relationships, but feel free to create join tables any time you need to simulate a ManyToMany relationship. We define each relationship by typing <EntityName for the “many” side>{<alias for the owning entity>} to <EntityName for the owning entity>. For our use case, we only needed three relationships to start:

  1. A user can have many food reviews, and is referred to as the reviews’ author. So we type: FoodReview{author} to User
  2. A food can have many reviews. So we type: FoodReview{food} to Food{review}
  3. A pet can have many food reviews. So we type FoodReview{pet} to Pet{foodReview}

You may have noticed that we haven’t set up any association between pets and foods. That means we won’t yet be able to query for all the reviews associated with the foods that a particular pet ate. Sure, we can use FoodReview as a join-table between pets and foods, but what if we want to specify additional data about the pet-food relationship, without depending on a user’s review? Use your judgment here, but I consider this use-case complex enough, and important enough, that it deserves to be programmed at a more-granular level in a later blog post. For now, we’ll start with an MVP that can get some reviews, foods, and pets onto the page.

Once you’ve typed out your entities and relationships, you can specify the types of Java service classes and pagination we want each entity to use:

service all with serviceImpl

paginate Food, FoodReview with pagination
paginate Pet with infinite-scroll

When you’re done, download this JDL file into a new directory called “src/main/scripts”. Then run this command to import the JDL file:

jhipster import-jdl src/main/scripts/jhipster-jdl.jh

You will be prompted about some file conflicts, such as in the liquibase migration config, the Angular front end navbar component, and the Angular entity module file. To keep things simple, say yes to all of these prompts.

Open up your web application again, log in as an admin user, and check out the “entity” dropdown in the navbar: you now have CRUD pages for food, pets, and food reviews, with all the associations available in the forms!

Try creating some food and pet entities, and then head over to the Food Review form. So far so good, but you’ll see that in the pet and food dropdowns, you’ll only see id numbers, not the names of the pets and foods you can select. Not to mention that you’ll probably want to just browse to a food or pet’s page and be able to add a review right there, and have the association filled in automatically.

In other words, our app is perfectly functional for a savvy admin, but we have some work to do to improve the experience for an end user. It’s time to roll up our sleeves and start coding some TypeScript and Java. Stay tuned for Part Three:

Leave a Reply

Your email address will not be published. Required fields are marked *