PoshJosh's Blog

Add entities/tables to an existing Jhipster based project

September 03, 2020

What is Jhipster

Jhipster is a full stack application code generator. Simply put, use Jhipster to generate application code for a Spring Boot (with Java or Kotlin), Micronaut, Quarkus, Node.js, or .NET backend, as well as Angular, React or Vue front-end.

Part of the process of generating application code involves generating domain logic including entity classes. The recommended way is to use a .jdl file. JDL stands for JHipster Domain Language. It is a JHipster-specific domain language where you can describe all your applications, deployments, entities and their relationships in a single file (or more than one) with a user-friendly syntax.

Here is an example of a jdl for a blog.

application {
  config {
    baseName blog,
    applicationType monolith,
    packageName com.jhipster.demo.blog,
    prodDatabaseType mysql,
    cacheProvider hazelcast,
    buildTool maven,
    clientFramework react,
    useSass true,
    testFrameworks [protractor]
  }
  entities *
}

entity Blog {
  name String required minlength(3)
  handle String required minlength(2)
}

entity Post {
  title String required
  content TextBlob required
  date Instant required
}

entity Tag {
  name String required minlength(2)
}

relationship ManyToOne {
  Blog{user(login)} to User
  Post{blog(name)} to Blog
}

relationship ManyToMany {
  Post{tag(name)} to Tag{entry}
}

paginate Post, Tag with infinite-scroll

You can find samples of JDL here

Quickly get started with Jhipster

Although this post is not a get started guide, you can quickly get started with Jhipster thus:

  • Install Java, Git and Node.js, if not installed on you machine.

  • Install JHipster by running the command: npm install -g generator-jhipster

  • Create a new directory and go into it: mkdir myApp && cd myApp

  • Run JHipster and follow instructions on screen: jhipster

  • Model your entities with JDL Studio and download the resulting jhipster-jdl.jh file

  • Generate your entities with jhipster import-jdl jhipster-jdl.jh

Yes we earlier said .jdl and now .jh. Well both are supported.

Modify existing entities

Now suppose you want to modify

entity Tag {
  name String required minlength(2)
}

to

entity Tag {
  name String required minlength(3)
}
  • Modify the .jdl file

  • Run command mvnw clean

  • Run command jhipster import-jdl <YOUR_JDL_FILE>.jdl

  • Respond to the prompt(s)

  • Run command mvnw

  • You may get the following error

Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [com/myapplication/config/LiquibaseConfiguration.class]: Invocation of init method failed; nested exception is liquibase.exception.ValidationFailedException: Validation Failed:
     1 change sets check sum
          config/liquibase/changelog/20200808122400_added_entity_Tag.xml::20200808122400-1::jhipster was: 8:a970f5129cedf190a460aca4917f56c5 but is now: 8:13347cabb349df27960176ab29f30179

Before we resolve the error, a few notes.

  • Note: Use mvnw for windows machines, and ./mvnw for linux

  • Jhipster uses liquibase to carryout and track

changes to the domain.

  • Do not edit the entity files directly as the changes will confuse liquibase.

Resolve errors which occured while modifying existing entities

Problem

When we use liquibase all entity changes that happen later should be captured as separate changelogs. However, Jhipster overwrites the changelog files. Therefore the entities liquibase file’s checksum changes as it has new content now. And in your database, there is a table called DATABASECHANGELOG which stores which all changeLogs were applied and this has checksum data.

As a result of the foregoing, when you start your application you get a liquibase.exception.ValidationFailedException because your latest liquibase changeLog of modified entity’s checksum is different from the last time (database will have a checksum for this liquibase file for previous verison) you ran.

Running command: mvn liquibase:clearCheckSums is not the right approach most of the time. This actually clears all checksums in the database so that you lose track of the changes that had happened which is usually not intended. This feature of liquibase makes sense when for example you want to rollback the new changes you applied. If you clear checksums and run the application it will compute new checksums; you lose the track and can lead to problems, if care is not taken.

Solutions

  • Check the changelog file for the entity. In this case the entity we edited

was Tag and the liquibase file is located at: src/main/resources/config/liquibase/changelog/20200808122400_added_entity_Tag.xml

  • Revert the file back to before the changes were made. Git is helpful for

achieving this.

  • Run mvn compile liquibase:diff to capture the change you made to the entity

in liquibase. Or you could add the changes manually.

  • The command your ran i.e liquibase:diff generates a changelog file in folder

src/main/resources/config/liquibase/changelog/. Check that the changelog file contains the change as is. You could also edit, this file manually.

  • Add the changelog file to the master file located at src/main/resources/config/liquibase/master.xml

  • Run your application


Written byChinomso IkwuagwuExcélsior

Limited conversations with distributed systems.

Modifying legacy applications using domain driven design (DDD)

Gherkin Best Practices

Code Review Best Practices

Hacking Cypress in 9 minutes

Some common mistakes when developing java web applications

How to make a Spring Boot application production ready

SQL JOINS - A Refresher

Add Elasticsearch to Spring Boot Application

Add entities/tables to an existing Jhipster based project

CSS 3 Media Queries - All over again

Maven Dependency Convergence - quick reference

Amazon SNS Quick Reference

AWS API Gateway Quick Reference

Amazon SQS Quick Reference

AWS API Gateway Quick Reference

AWS Lambda Quick Reference

Amazon DynamoDB - Quick Reference

Amazon Aurora

Amazon Relational Database Service

AWS Database Services

AWS Security Essentials

Amazon Virtual Private Cloud Connectivity Options

Summary of AWS Services

AWS Certified Solutions Architect - Quick Reference

AWS CloudFront FAQs - Curated

AWS VPC FAQs - Curated

AWS EC2 FAQs - Curated

AWS Achritect 5 - Architecting for Cost Optimization

AWS Achritect 4 - Architecting for Performance Efficiency

AWS Achritect - 6 - Passing the Certification Exam

AWS Achitect 3 - Architecting for Operational Excellence

AWS Achitect 2 - Architecting for Security

AWS Achitect 1 - Architecting for Reliability

Amazon DynamoDB Accelerator (DAX)

Questions and Answers - AWS Certified Cloud Architect Associate

Questions and Answers - AWS Certified Cloud Architect Associate

AWS Connectivity - PrivateLink, VPC-Peering, Transit-gateway and Direct-connect

AWS - VPC peering vs PrivateLink

Designing Low Latency Systems

AWS EFS vs FSx

AWS Regions, Availability Zones and Local Zones

AWS VPC Endpoints and VPC Endpoint Services (AWS Private Link)

AWS - IP Addresses

AWS Elastic Network Interfaces

AWS Titbits

Jenkins on AWS - Automation

Jenkins on AWS - Setup

Jenkins on AWS - Best practices

Introduction to CIDR Blocks

AWS Lamda - Limitations and Use Cases

AWS Certified Solutions Architect Associate - Part 10 - Services and design scenarios

AWS Certified Solutions Architect Associate - Part 9 - Databases

AWS Certified Solutions Architect Associate - Part - 8 Application deployment

AWS Certified Solutions Architect Associate - Part 7 - Autoscaling and virtual network services

AWS Certified Solutions Architect Associate - Part 6 - Identity and access management

AWS Certified Solutions Architect Associate - Part 5 - Compute services design

AWS Certified Solutions Architect Associate - Part 4 - Virtual Private Cloud

AWS Certified Solutions Architect Associate - Part 3 - Storage services

AWS Certified Solutions Architect Associate - Part 2 - Introduction to Security

AWS Certified Solutions Architect Associate - Part 1 - Key services relating to the Exam

AWS Certifications - Part 1 - Certified solutions architect associate

AWS Virtual Private Cloud (VPC) Examples

Curated info on AWS Virtual Private Cloud (VPC)

Notes on Amazon Web Services 8 - Command Line Interface (CLI)

Notes on Amazon Web Services 7 - Elastic Beanstalk

Notes on Amazon Web Services 6 - Developer, Media, Migration, Productivity, IoT and Gaming

Notes on Amazon Web Services 5 - Security, Identity and Compliance

Notes on Amazon Web Services 4 - Analytics and Machine Learning

Notes on Amazon Web Services 3 - Managment Tools, App Integration and Customer Engagement

Notes on Amazon Web Services 2 - Storages databases compute and content delivery

Notes on Amazon Web Services 1 - Introduction

AWS Auto Scaling - All you need to know

AWS Load Balancers - How they work and differences between them

AWS EC2 Instance Types - Curated

Amazon Web Services - Identity and Access Management Primer

Amazon Web Services - Create IAM User

Preparing Jenkins after Installation

Jenkins titbits, and then some

Docker Titbits

How to Add Chat Functionality to a Maven Java Web App

Packer - an introduction

Terraform - an introduction

Versioning REST Resources with Spring Data REST

Installing and running Jenkins in Docker

Automate deployment of Jenkins to AWS - Part 2 - Full automation - Single EC2 instance

Automate deployment of Jenkins to AWS - Part 1 - Semi automation - Single EC2 instance

Introduction to Jenkins

Software Engineers Reference - Dictionary, Encyclopedia or Wiki - For Software Engineers