This sample project illustrates how to build a simple CRUD application using spring data jpa for the persistence layer.
The application also illustrates how to replace the persistence layer for an alternative solution built with JOOQ using spring profiles feature.
The following is to illustrate some of the JOOQ basic features :
1, How to generate JOOQ Code DSL classes from existing jpa entities classes which then can be used to generate native queries, instead of using nest query strings.
2, How to generate JOOQ Code DSL classes from an existing database and use its DAOs to build a simple CRUD application
- Prerequisites and frameworks
- Get code
- Start docker container
- Create database and JOOQ stubs
- Install dependencies
- Start application
- Swagger endpoints
- How this repo is organized
In order to install and start the application please ensure that you have the following:
- Java JDK (v17+)
- Maven (v3+)
- Kotlin (1.8.20) (via Maven plugin)
- Docker (Postgres)
- Spring Boot 3+
- JOOQ 3.18.X
- Open API for documenting REST endpoints and contracts
- Spock (Groovy) for testing
- REST assured for system testing
- Open API for documenting REST endpoints and contracts
The code can be found in GitHub repo https://github.com/garytxo/jooq-demo
Clone repository locally using
git clone [email protected]/garytxo/spring-jpa-jooq.git
cd jooq-demo-docker-setup
make start-services
cd jooq-demo-docker-setup
make stop-services
cd jooq-demo-docker-setup
make restart-services
cd jooq-demo-docker-setup
make start-localstack-service / stop-localstack-service / restart-localstack-service
cd jooq-demo-docker-setup
make start-postgres-db-service / stop-postgres-db-service / restart-postgres-db-service
Create the empty database using the following
psql -h localhost -p 5436 -U admin -c "CREATE DATABASE demo_tech_blog";
psql -h localhost -p 5436 -U admin -c "CREATE DATABASE demo_tech_blog_test";
If you need to DROP the database execute
psql -h localhost -p 5436 -U admin -c "DROP DATABASE demo_tech_blog";
psql -h localhost -p 5436 -U admin -c "DROP DATABASE demo_tech_blog_test";
JOOQ Repository metadata is useful to be type-safe when working against DB and in order to be notified by compiler
when code doesn't match what is present on DB tables. This metadata is generated by jooq
via maven
plugin testcontainers-jooq-codegen-maven-plugin
which build during the package phase.
cd server
mvn clean install -DskipTests -Dgib.d=true
cd server
mvn clean verify
In order to start the tech-blog application using the JPA persistence layer you can run the following command in the terminal
cd server
mvn clean install -DskipTests=true
java -jar -Dspring.profiles.active=jpa tech-blog-app/target/tech-blog-app-0.0.1-SNAPSHOT-app.jar
Or if you want to start the application using the jooq persistence layer then use the following
cd server
mvn clean install -DskipTests=true
java -jar -Dspring.profiles.active=jooq tech-blog-app/target/tech-blog-app-0.0.1-SNAPSHOT-app.jar
Once the above apps are up and running you can access each of the following campaign application
swagger endpoints : http://localhost:7001/api-tech-blog/swagger-ui/index.html
Before starting to install and start the applications it makes senses to discuss the project submodules and explain what they are and certain rules that we need to adhere whilst working on them.
This module is the common (or shared) database persistence module for performing database action on the data_transfer data model.
The module is responsible for managing the database migrations (flyway) which are only execute in the tech-blog-app .
Additionally, the tech-blog-data
has a dependency on tech-blog-data-jooq
This module contains JOOQ autogenerated classes that are generate from the tech blog database data model which are used all the CRUD actions. Furthermore, it contaings some code generation strategies
This is the main spring boot application that will contain all the logic to handle
and has dependencies on modules tech-blog-data
This module contain all the testing for all modules,
tech-blog-data
, tech-blog-data-jooq
and tech-blog-app
This is the aggregate jacoco report which we supply to sonarqube for code coverage.
The module
Note that the JPADatabase offers a quick win by integrating with simple JPA defined schemas very quickly. It has its caveats. For best results its recommended going DDL first (and generate both jOOQ code and JPA model from that), because it will be much easier to put your schema change management under version control, e.g. via Flyway or Liquibase.
The following section explain how to install ASDF and manage our JDKs for local development
First install asdf via brew
brew install asdf
Now add java plugin
asdf plugin-add java https://github.com/halcyon/asdf-java.git
Add the setting java_macos_integration_enable to yes on .asdfrc file enables this integration.
echo "java_macos_integration_enable = yes" >> ~/.asdfrc
To set JAVA_HOME in your shell's initialization add the following: For zsh shell add the following to the bottom on the .zshrc
. ~/.asdf/plugins/java/set-java-home.zsh
now can list all the JDKS versions
asdf list-all java
Install a candidate listed from the previous command like this:
asdf install java corretto-17.0.8.8.1
Select an installed candidate for use like this:
asdf global java corretto-17.0.8.8.1
https://www.marcobehler.com/guides/jooq
https://vladmihalcea.com/jooq-facts-from-jpa-annotations-to-jooq-table-mappings/
https://blog.astradot.com/why-we-use-jooq-instead-of-hibernate/
Checkout the multset query and record that we can use with jooq: https://www.youtube.com/watch?v=UuNrcBgRGXw https://github.com/simasch/jooq-quickstart
https://www.baeldung.com/spring-boot-support-for-jooq
https://dzone.com/articles/implementing-infinite-scroll-in-jOOQ
https://thorben-janssen.com/hibernate-jooq-a-match-made-in-heaven/ https://www.jooq.org/doc/latest/manual/sql-execution/alternative-execution-models/using-jooq-with-jpa/
https://blog.gitguardian.com/8-easy-steps-to-set-up-multiple-git-accounts/