Skip to content

Commit 7f938f0

Browse files
authored
Merge pull request #356 from domaframework/feat/doma-codegen-plugin
Add example-codegen-plugin project
2 parents 1190c11 + 7e327eb commit 7f938f0

File tree

8 files changed

+296
-106
lines changed

8 files changed

+296
-106
lines changed

README.md

Lines changed: 1 addition & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -10,112 +10,7 @@ This repository includes the following examples:
1010
* [example-criteria](example-criteria) - Uses the Criteria API.
1111
* [example-jpms](example-jpms) - Uses the Java Platform Module System (JPMS).
1212
* [example-geometric-type](example-geometric-type) - Uses Geometric types of PostgreSQL.
13-
14-
ER diagram
15-
---------------------
16-
17-
The ER diagram for the database used in the example projects is shown below.
18-
19-
```mermaid
20-
erDiagram
21-
DEPARTMENT {
22-
INTEGER id PK "not null"
23-
VARCHAR name "not null"
24-
INTEGER version "not null"
25-
}
26-
EMPLOYEE {
27-
INTEGER id PK "not null"
28-
VARCHAR name "not null"
29-
INTEGER age "not null"
30-
INTEGER salary
31-
VARCHAR job_type
32-
TIMESTAMP hiredate
33-
INTEGER department_id
34-
INTEGER version "not null"
35-
TIMESTAMP inserttimestamp
36-
TIMESTAMP updatetimestamp
37-
}
38-
USER {
39-
INT id PK "auto_increment"
40-
VARCHAR name "not null"
41-
VARCHAR email "unique not null"
42-
TIMESTAMP created_at "default current_timestamp"
43-
INT version "default 0 not null"
44-
}
45-
ROLE {
46-
INT id PK "auto_increment"
47-
VARCHAR name "unique not null"
48-
INT version "default 0 not null"
49-
}
50-
USER_ROLE {
51-
INT id PK "auto_increment"
52-
INT user_id "not null"
53-
INT role_id "not null"
54-
INT version "default 0 not null"
55-
}
56-
PRODUCT {
57-
INT id PK "auto_increment"
58-
VARCHAR name "not null"
59-
DECIMAL price "not null"
60-
INT stock_quantity "not null"
61-
TIMESTAMP created_at "default current_timestamp"
62-
INT version "default 0 not null"
63-
}
64-
CATEGORY {
65-
INT id PK "auto_increment"
66-
VARCHAR name "unique not null"
67-
INT version "default 0 not null"
68-
}
69-
PRODUCT_CATEGORY {
70-
INT id PK "auto_increment"
71-
INT product_id "not null"
72-
INT category_id "not null"
73-
INT version "default 0 not null"
74-
}
75-
ORDER {
76-
INT id PK "auto_increment"
77-
INT user_id "not null"
78-
TIMESTAMP order_date "default current_timestamp"
79-
VARCHAR status "not null"
80-
INT version "default 0 not null"
81-
}
82-
ORDER_ITEM {
83-
INT id PK "auto_increment"
84-
INT order_id "not null"
85-
INT product_id "not null"
86-
INT quantity "not null"
87-
DECIMAL price "not null"
88-
INT version "default 0 not null"
89-
}
90-
PAYMENT {
91-
INT id PK "auto_increment"
92-
INT order_id "unique not null"
93-
DECIMAL amount "not null"
94-
TIMESTAMP payment_date "default current_timestamp"
95-
INT version "default 0 not null"
96-
}
97-
REVIEW {
98-
INT id PK "auto_increment"
99-
INT user_id "not null"
100-
INT product_id "not null"
101-
INT rating "check: 1-5"
102-
TEXT comment
103-
TIMESTAMP created_at "default current_timestamp"
104-
INT version "default 0 not null"
105-
}
106-
107-
EMPLOYEE }|..|| DEPARTMENT : belongs_to
108-
USER_ROLE }|..|| USER : "user_id"
109-
USER_ROLE }|..|| ROLE : "role_id"
110-
PRODUCT_CATEGORY }|..|| PRODUCT : "product_id"
111-
PRODUCT_CATEGORY }|..|| CATEGORY : "category_id"
112-
ORDER }|..|| USER : "user_id"
113-
ORDER_ITEM }|..|| ORDER : "order_id"
114-
ORDER_ITEM }|..|| PRODUCT : "product_id"
115-
PAYMENT ||--|| ORDER : "order_id"
116-
REVIEW }|..|| USER : "user_id"
117-
REVIEW }|..|| PRODUCT : "product_id"
118-
```
13+
* [example-codegen-plugin](example-codegen-plugin) - Uses [Doma CodeGen Plugin](https://github.com/domaframework/doma-codegen-plugin).
11914

12015
Clone this repository
12116
---------------------

example-codegen-plugin/.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.classpath
2+
.factorypath
3+
.project
4+
.settings
5+
/.apt_generated
6+
/bin
7+
/build
8+
.gradle
9+
.idea
10+
out
11+
/.apt_generated_tests/
12+
.vscode
13+
.sdkmanrc
14+
15+
src/main/java/example/generated/
16+
src/main/resources/META-INF/example/generated/
17+
src/test/java/example/generated/

example-codegen-plugin/README.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# example-codegen-plugin
2+
3+
A sample project demonstrating code generation using the Doma Codegen Plugin.
4+
5+
## Overview
6+
7+
This project demonstrates how to use the [Doma Codegen Plugin](https://doma.readthedocs.io/en/stable/codegen/) to automatically generate entity classes and DAO classes from database schemas.
8+
9+
It supports both MySQL and PostgreSQL databases and uses Testcontainers to automatically start databases during code generation.
10+
11+
## Project Structure
12+
13+
```
14+
example-codegen-plugin/
15+
├── build.gradle.kts # Build configuration file
16+
├── src/
17+
│ ├── main/
18+
│ │ ├── java/
19+
│ │ │ └── example/
20+
│ │ │ └── generated/ # Auto-generated code
21+
│ │ │ ├── mysql/
22+
│ │ │ │ ├── dao/ # MySQL DAO classes
23+
│ │ │ │ └── entity/ # MySQL entity classes
24+
│ │ │ └── postgresql/
25+
│ │ │ ├── dao/ # PostgreSQL DAO classes
26+
│ │ │ └── entity/ # PostgreSQL entity classes
27+
│ │ └── resources/
28+
│ │ ├── META-INF/ # Auto-generated SQL files
29+
│ │ ├── init_mysql.sql # MySQL initialization script
30+
│ │ └── init_postgresql.sql # PostgreSQL initialization script
31+
│ └── test/
32+
│ └── java/
33+
│ └── example/
34+
│ └── generated/ # Auto-generated test code
35+
```
36+
37+
## Generated Tables
38+
39+
Code is generated from the following four tables:
40+
41+
- `DEPARTMENT` - Department information
42+
- `ADDRESS` - Address information
43+
- `EMPLOYEE` - Employee information (with foreign key constraints to DEPARTMENT and ADDRESS)
44+
- `COMP_KEY_ADDRESS` - Address information with composite primary key
45+
46+
## Usage
47+
48+
### Code Generation
49+
50+
#### Generate code for MySQL
51+
```bash
52+
./gradlew domaCodeGenMysqlAll
53+
```
54+
55+
#### Generate code for PostgreSQL
56+
```bash
57+
./gradlew domaCodeGenPostgresqlAll
58+
```
59+
60+
### Clean up generated code
61+
```bash
62+
./gradlew clean
63+
```
64+
65+
### Run tests
66+
```bash
67+
./gradlew test
68+
```
69+
70+
## Configuration
71+
72+
The following configurations are set in `build.gradle.kts`:
73+
74+
### MySQL Configuration
75+
- Uses Testcontainers MySQL
76+
- Initialization script: `src/main/resources/init_mysql.sql`
77+
- Generated packages:
78+
- Entities: `example.generated.mysql.entity`
79+
- DAOs: `example.generated.mysql.dao`
80+
81+
### PostgreSQL Configuration
82+
- Uses Testcontainers PostgreSQL
83+
- Initialization script: `src/main/resources/init_postgresql.sql`
84+
- Generated packages:
85+
- Entities: `example.generated.postgresql.entity`
86+
- DAOs: `example.generated.postgresql.dao`
87+
88+
## Requirements
89+
90+
- Java 17 or higher
91+
- Docker (required for running Testcontainers)
92+
93+
## References
94+
95+
- [Doma Official Documentation](https://doma.readthedocs.io/)
96+
- [Doma Codegen Plugin](https://doma.readthedocs.io/en/stable/codegen/)
97+
- [Testcontainers](https://www.testcontainers.org/)
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
plugins {
2+
java
3+
alias(libs.plugins.doma.codegen) // Doma code generation plugin
4+
}
5+
6+
// Dependency configuration
7+
dependencies {
8+
// Test runtime dependencies
9+
testImplementation(platform(libs.testcontainers.bom)) // Testcontainers dependency management
10+
testImplementation(libs.jdbc.mysql) // MySQL JDBC driver
11+
testImplementation(libs.jdbc.postgresql) // PostgreSQL JDBC driver
12+
testImplementation(libs.testcontainers.mysql) // MySQL Testcontainers
13+
testImplementation(libs.testcontainers.postgresql) // PostgreSQL Testcontainers
14+
15+
// Doma code generation dependencies
16+
domaCodeGen(platform(libs.testcontainers.bom)) // Testcontainers dependency management for code generation
17+
domaCodeGen(libs.jdbc.mysql) // MySQL JDBC driver for code generation
18+
domaCodeGen(libs.jdbc.postgresql) // PostgreSQL JDBC driver for code generation
19+
domaCodeGen(libs.testcontainers.mysql) // MySQL Testcontainers for code generation
20+
domaCodeGen(libs.testcontainers.postgresql) // PostgreSQL Testcontainers for code generation
21+
}
22+
23+
// Doma code generation configuration
24+
domaCodeGen {
25+
// Base package name for generated code
26+
val basePackage = "example.generated"
27+
28+
// MySQL configuration
29+
register("mysql") {
30+
// Path to initialization script
31+
val initScript = file("src/main/resources/init_mysql.sql")
32+
// MySQL connection URL using Testcontainers with initialization script
33+
url.set("jdbc:tc:mysql:8.4.5:///test?TC_INITSCRIPT=file:${initScript.absolutePath}")
34+
user.set("test") // Database username
35+
password.set("test") // Database password
36+
// Entity class generation settings
37+
entity {
38+
packageName.set("$basePackage.mysql.entity") // Package name for entity classes
39+
}
40+
// DAO class generation settings
41+
dao {
42+
packageName.set("$basePackage.mysql.dao") // Package name for DAO classes
43+
}
44+
}
45+
// PostgreSQL configuration
46+
register("postgresql") {
47+
// Path to initialization script
48+
val initScript = file("src/main/resources/init_postgresql.sql")
49+
// PostgreSQL connection URL using Testcontainers with initialization script
50+
url.set("jdbc:tc:postgresql:13.21:///test?TC_INITSCRIPT=file:${initScript.absolutePath}")
51+
user.set("test") // Database username
52+
password.set("test") // Database password
53+
// Entity class generation settings
54+
entity {
55+
packageName.set("$basePackage.postgresql.entity") // Package name for entity classes
56+
}
57+
// DAO class generation settings
58+
dao {
59+
packageName.set("$basePackage.postgresql.dao") // Package name for DAO classes
60+
}
61+
}
62+
}
63+
64+
tasks {
65+
// Clean task to remove generated source files
66+
val cleanGeneratedSources by register<Delete>("cleanGeneratedSources") {
67+
description = "Deletes generated source files by Doma code generation"
68+
delete(
69+
fileTree("src/main/java") {
70+
include("example/generated/**")
71+
}
72+
)
73+
delete(
74+
fileTree("src/test/java") {
75+
include("example/generated/**")
76+
}
77+
)
78+
delete(
79+
fileTree("src/main/resources/META-INF") {
80+
include("example/generated/**")
81+
}
82+
)
83+
}
84+
85+
// Configure the main clean task to also clean generated sources
86+
clean {
87+
dependsOn(cleanGeneratedSources)
88+
}
89+
90+
// Configure test task to generate all source files before running tests
91+
test {
92+
dependsOn("domaCodeGenMysqlAll", "domaCodeGenPostgresqlAll")
93+
}
94+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
CREATE TABLE IF NOT EXISTS DEPARTMENT(DEPARTMENT_ID INTEGER NOT NULL PRIMARY KEY, DEPARTMENT_NO INTEGER NOT NULL UNIQUE,DEPARTMENT_NAME VARCHAR(20),LOCATION VARCHAR(20) DEFAULT 'TOKYO', VERSION INTEGER);
2+
CREATE TABLE IF NOT EXISTS ADDRESS(ADDRESS_ID INTEGER NOT NULL PRIMARY KEY, STREET VARCHAR(20) UNIQUE, VERSION INTEGER);
3+
CREATE TABLE IF NOT EXISTS EMPLOYEE(EMPLOYEE_ID INTEGER NOT NULL PRIMARY KEY, EMPLOYEE_NO INTEGER NOT NULL ,EMPLOYEE_NAME VARCHAR(20),MANAGER_ID INTEGER,HIREDATE DATE,SALARY NUMERIC(7,2),DEPARTMENT_ID INTEGER,ADDRESS_ID INTEGER, VERSION INTEGER, CONSTRAINT FK_DEPARTMENT_ID FOREIGN KEY(DEPARTMENT_ID) REFERENCES DEPARTMENT(DEPARTMENT_ID),CONSTRAINT FK_ADDRESS_ID FOREIGN KEY(ADDRESS_ID) REFERENCES ADDRESS(ADDRESS_ID));
4+
CREATE TABLE IF NOT EXISTS COMP_KEY_ADDRESS(ADDRESS_ID1 INTEGER NOT NULL, ADDRESS_ID2 INTEGER NOT NULL, STREET VARCHAR(20), VERSION INTEGER, CONSTRAINT PK_COMP_KEY_ADDRESS PRIMARY KEY(ADDRESS_ID1, ADDRESS_ID2));
5+
6+
INSERT INTO DEPARTMENT VALUES(1,10,'ACCOUNTING','NEW YORK',1);
7+
INSERT INTO DEPARTMENT VALUES(2,20,'RESEARCH','DALLAS',1);
8+
INSERT INTO DEPARTMENT VALUES(3,30,'SALES','CHICAGO',1);
9+
INSERT INTO DEPARTMENT VALUES(4,40,'OPERATIONS','BOSTON',1);
10+
INSERT INTO ADDRESS VALUES(1,'STREET 1',1);
11+
INSERT INTO ADDRESS VALUES(2,'STREET 2',1);
12+
INSERT INTO ADDRESS VALUES(3,'STREET 3',1);
13+
INSERT INTO ADDRESS VALUES(4,'STREET 4',1);
14+
INSERT INTO ADDRESS VALUES(5,'STREET 5',1);
15+
INSERT INTO ADDRESS VALUES(6,'STREET 6',1);
16+
INSERT INTO ADDRESS VALUES(7,'STREET 7',1);
17+
INSERT INTO ADDRESS VALUES(8,'STREET 8',1);
18+
INSERT INTO ADDRESS VALUES(9,'STREET 9',1);
19+
INSERT INTO ADDRESS VALUES(10,'STREET 10',1);
20+
INSERT INTO ADDRESS VALUES(11,'STREET 11',1);
21+
INSERT INTO ADDRESS VALUES(12,'STREET 12',1);
22+
INSERT INTO ADDRESS VALUES(13,'STREET 13',1);
23+
INSERT INTO ADDRESS VALUES(14,'STREET 14',1);
24+
INSERT INTO ADDRESS VALUES(15,'STREET 15',1);
25+
INSERT INTO EMPLOYEE VALUES(1,7369,'SMITH',13,'1980-12-17',800,2,1,1);
26+
INSERT INTO EMPLOYEE VALUES(2,7499,'ALLEN',6,'1981-02-20',1600,3,2,1);
27+
INSERT INTO EMPLOYEE VALUES(3,7521,'WARD',6,'1981-02-22',1250,3,3,1);
28+
INSERT INTO EMPLOYEE VALUES(4,7566,'JONES',9,'1981-04-02',2975,2,4,1);
29+
INSERT INTO EMPLOYEE VALUES(5,7654,'MARTIN',6,'1981-09-28',1250,3,5,1);
30+
INSERT INTO EMPLOYEE VALUES(6,7698,'BLAKE',9,'1981-05-01',2850,3,6,1);
31+
INSERT INTO EMPLOYEE VALUES(7,7782,'CLARK',9,'1981-06-09',2450,1,7,1);
32+
INSERT INTO EMPLOYEE VALUES(8,7788,'SCOTT',4,'1982-12-09',3000.0,2,8,1);
33+
INSERT INTO EMPLOYEE VALUES(9,7839,'KING',NULL,'1981-11-17',5000,1,9,1);
34+
INSERT INTO EMPLOYEE VALUES(10,7844,'TURNER',6,'1981-09-08',1500,3,10,1);
35+
INSERT INTO EMPLOYEE VALUES(11,7876,'ADAMS',8,'1983-01-12',1100,2,11,1);
36+
INSERT INTO EMPLOYEE VALUES(12,7900,'JAMES',6,'1981-12-03',950,3,12,1);
37+
INSERT INTO EMPLOYEE VALUES(13,7902,'FORD',4,'1981-12-03',3000,2,13,1);
38+
INSERT INTO EMPLOYEE VALUES(14,7934,'MILLER',7,'1982-01-23',1300,1,14,1);

0 commit comments

Comments
 (0)