sqlToJPA is a tool for automatically generating Java JPA (Java Persistence API) entity classes from SQL schema files. It parses CREATE TABLE statements, maps SQL data types to Java types, and creates fully annotated Java classes with fields, getters, setters, constructors, relationships, and equals/hashCode methods. The project includes three implementations:
- sqlToJPAPython: A Python-based implementation (
sqlToJPAPython/directory). - sqlToJPAJava: A Java-based implementation (
sqlToJPAJava/directory). - sqlToJPAcpp: A C++-based implementation (
sqlToJPAcpp/directory).
All implementations produce identical Java JPA entity classes, offering flexibility to use Python, Java, or C++ depending on your environment or preference.
- Converts SQL data types to appropriate Java types (e.g.,
BIGINTtoLong,VARCHARtoString,TIMESTAMPtoLocalDateTime). - Supports primary keys (
@Id,@GeneratedValue), foreign keys (@ManyToOne), and constraints likeNOT NULL. - Handles
AUTO_INCREMENTand default values in the generated entities. - Generates each entity in a separate
.javafile under the specified package and output directory. - Includes proper JPA annotations (
@Entity,@Table,@Column,@JoinColumn, etc.). - Supports relationships between tables based on foreign key constraints.
The following Mermaid diagram illustrates the workflow of the sqlToJPA tool, showing how each implementation processes an SQL schema file to produce Java JPA entity classes:
graph LR
A[SQL Schema File<br/>schema.sql] -->|Lê o SQL de| B(sqlToJPA<br/>Java Application)
B -->|Gera Entidades JPA| C[Java JPA Entities<br/>e.g., Customer.java, Product.java]
C -->|Salva em| D[Diretório de Pacote<br/>e.g., src/main/java/com/example/entities]
- Python 3.6 or higher (uses
pathlib,dataclasses, and type hints). - No external Python dependencies required.
- Generated Java classes require Java 8 or higher (due to
java.timeclasses likeLocalDateTime) and JPA (javax.persistence) for compilation.
- Java 8 or higher (uses
java.timeclasses likeLocalDateTime). - No external dependencies beyond the standard Java library and JPA (
javax.persistence).
- C++17 or higher (uses
<filesystem>and other C++17 features). - A C++ compiler supporting C++17 (e.g., g++ 7.0 or later).
- No external C++ dependencies required.
- Generated Java classes require Java 8 or higher and JPA (
javax.persistence) for compilation.
- A SQL schema file containing
CREATE TABLEstatements.
- Clone the repository:
git clone https://github.com/mmaunze/sqlToJPA.git cd sqlToJPA - Ensure you have the required tools installed:
- Python 3.6+ for
sqlToJPAPython. - JDK 8+ for
sqlToJPAJava. - C++17-compliant compiler (e.g., g++) for
sqlToJPAcpp.
- Python 3.6+ for
- Place your SQL schema file (e.g.,
schema.sql) in the project directory or a subdirectory.
The tool can be run using any of the three implementations (Python, Java, or C++). All produce identical Java JPA entity classes.
- Navigate to the Python implementation directory:
cd sqlToJPAPython - Run the generator:
python sql_parser_jpa_generator.py schema.sql com.example.entities ./generated-classes
schema.sql: Path to the input SQL schema file.com.example.entities: Package name for the generated Java classes (optional, defaults tocom.example.entities)../generated-classes: Output directory for the generated files (optional, defaults to./generated-entities).
- Navigate to the Java implementation directory:
cd sqlToJPAJava - Compile the Java source file:
javac SQLParserJPAGenerator.java
- Run the generator:
java SQLParserJPAGenerator schema.sql com.example.entities ./generated-classes
- Parameters are the same as for the Python version.
- Navigate to the C++ implementation directory:
cd sqlToJPAcpp - Compile the C++ source file:
g++ -std=c++17 sql_parser_jpa_generator.cpp -o sql_parser_jpa_generator
- Run the generator:
./sql_parser_jpa_generator schema.sql com.example.entities ./generated-classes
- Parameters are the same as for the Python and Java versions.
All implementations create a directory structure matching the package name (e.g., ./generated-classes/com/example/entities) and generate one .java file per table.
Create a file named schema.sql with the following content to test any implementation:
CREATE TABLE users (
id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
role_id INT,
FOREIGN KEY (role_id) REFERENCES roles(id)
);
CREATE TABLE roles (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL
);Run any implementation, e.g., for C++:
cd sqlToJPAcpp
g++ -std=c++17 sql_parser_jpa_generator.cpp -o sql_parser_jpa_generator
./sql_parser_jpa_generator ../schema.sql com.example.entities ../generated-classesOr for Python:
cd sqlToJPAPython
python sql_parser_jpa_generator.py ../schema.sql com.example.entities ../generated-classesOr for Java:
cd sqlToJPAJava
javac SQLParserJPAGenerator.java
java SQLParserJPAGenerator ../schema.sql com.example.entities ../generated-classesThis will generate two files:
./generated-classes/com/example/entities/Roles.java./generated-classes/com/example/entities/Users.java
Roles.java: Contains an entity withInteger idandString name, with proper@Id,@GeneratedValue, andnullable = falseannotations.Users.java: Contains an entity withLong id,String username,String email,LocalDateTime createdAt,Integer roleId, and a@ManyToOnerelationship toRoles.
package com.example.entities;
import java.io.Serializable;
import javax.persistence.*;
@Entity
@Table(name = "roles")
public class Roles implements Serializable {
// ... (fields, getters, setters, etc.)
}package com.example.entities;
import java.io.Serializable;
import java.time.LocalDateTime;
import javax.persistence.*;
@Entity
@Table(name = "users")
public class Users implements Serializable {
// ... (fields, relationships, getters, setters, etc.)
}- The tool assumes standard SQL syntax for
CREATE TABLEstatements. Complex schemas with non-standard syntax may require adjustments. - Foreign key relationships are mapped as
@ManyToOne. Support for other relationship types (e.g.,@OneToMany) may be added in future versions. - If a column definition cannot be parsed, a warning is logged to the console, and the column is skipped.
- The output directory and package structure are created automatically if they don't exist.
- The generated Java classes require a Java environment with JPA to compile and run.
Contributions are welcome! Feel free to submit issues or pull requests to improve any implementation, such as adding support for more SQL types, relationship types, or annotations like Lombok or JPA validation. Please specify whether your contribution targets sqlToJPAPython, sqlToJPAJava, sqlToJPAcpp, or multiple implementations.
This project is licensed under the MIT License.