Generating jooQ Code Using Testcontainers and Flyway
Using Testcontainers and Flyway allows me to generate jOOQ code without having to depend on Postgres server.
Using Testcontainers and Flyway allows me to generate jOOQ code without having to depend on Postgres server.
What is jOOQ ? jOOQ is a Java library that lets you to generate Java code from the database and lets you write type-safe SQL queries. jOOQ transforms tables in a database into Java classes, which we can use to retrieve or update data. Using jOOQ Library Inititalize Database Tables Execute the SQL statements in src/resources/initial_table.sql file on a Postgres server to create users, categories, transacations tables. initial_table.sql content 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 DROP TABLE IF EXISTS users CASCADE; DROP TABLE IF EXISTS categories CASCADE; DROP TABLE IF EXISTS transactions CASCADE; CREATE TABLE IF NOT EXISTS users ( id BIGINT NOT NULL PRIMARY KEY, username TEXT, email TEXT, updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ); CREATE TABLE IF NOT EXISTS categories ( id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY(START WITH 200000000) , user_id BIGINT REFERENCES users(id), name TEXT, active BOOLEAN NOT NULL DEFAULT TRUE, type TEXT, updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ); CREATE TABLE IF NOT EXISTS transactions ( id BIGINT NOT NULL PRIMARY KEY, user_id BIGINT REFERENCES users(id), category_id BIGINT REFERENCES categories(id), amount BIGINT, transaction_date DATE NOT NULL DEFAULT CURRENT_DATE, note TEXT, updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ); Add jOOQ Dependency To use jOOQ in Spring Boot, we declare a dependency to spring-boot-starter-jooq:...
Repository The repository for the backend implementation of Tabunganku is available here. Technology I develop the backend using Java 17 with Spring Boot, and Postgres 15.3 as the data store. Other Java libraries that I use in this project: spring-dotenv: a library to load environment variables from .env file. I use it to load database information and others. MapStruct: a library to create mapping between two Java Beans. I use it to transform entity objects into DTO objects....