Generating JPA Entities from an existing SQL Script
Tags: sql, springboot, jpa
Published: 2022-09-26 16:16:56

Let's assume we have an **existing database and want to create the JPA entities** from it. For this we can use Bootify's SQL import - and **get the classes including relations** configured suitable for the chosen database.

In Bootify's Free plan a Spring Boot application with its database schema can be created. However, the **JPA / Hibernate classes can also be used individually** - by using the annotations from the `javax.persistence` package they are independent of the chosen framework. To create the entities, the _SQL import_ can be utilized, which we will look at in the next section. First, however, we prepare our new project.

## Project preparation

To get to the import, we first start a new project via the [**Start Project**](https://bootify.io/) link in the header. In the _database settings_ the _target database_ can now be selected. Depending on the database, there are always some **special features that are automatically taken into account by Bootify** - e.g. for _PostgreSQL_ the table name `"user"` is a keyword and therefore has to be provided in quotes at the entity.

![](https://s3-eu-central-1.amazonaws.com/bootify-prod/ext/img/sqlImport/projectSetup.png "Preparing general settings of our new project")
_&nbsp; Preparing general settings of our new project_

Since we want to connect our application to an existing database, we set the schema generation to _"None (validate only)"_. This way, **Hibernate will not try to create or update the schema** on its own when starting the Spring Boot application. Instead, it will perform **a basic validation to detect mapping issues** between the entities and the actual schema early on. If necessary, manual adjustments will have to be made to the code afterwards.

All entities are generated with getters and setters. By **enabling the option for Lombok**, `@Getter` and `@Setter` are added as annotations instead.

# SQL Import

This brings us to the _Entities_ tab, where the SQL import can be started.

![](https://s3-eu-central-1.amazonaws.com/bootify-prod/ext/img/sqlImport/startImport.png "Starting the SQL import")
_&nbsp; Starting the SQL import_

In the following modal **the SQL script can be copied in**. There are many different (and conflicting) SQL dialects, so any problem with the input is immediately returned as a validation error. In this case we can fix them or start with smaller parts of the script to get a first valid result.

![](https://s3-eu-central-1.amazonaws.com/bootify-prod/ext/img/sqlImport/scriptError.png "Getting a validation error parsing our SQL")
_&nbsp; Getting a validation error parsing our SQL_ 

After we have successfully mastered this step, we can already see a **preview of the tables including their relationships** to each other. By performing the import, they are available in our project as entities and can be further customized if needed.

![](https://s3-eu-central-1.amazonaws.com/bootify-prod/ext/img/sqlImport/createdEntities.png "Our created entities")
_&nbsp; Our created entities_

When we're happy with the result, we can **explore and download the generated code**! The entities can be used individually or together with the generated Spring Boot application.

![](https://s3-eu-central-1.amazonaws.com/bootify-prod/ext/img/sqlImport/entityCode.png "Exploring the source code of our new entities")
_&nbsp; Exploring the source code of our new entities_

The SQL import is included in the Free plan of Bootify. **In the Professional plan advanced functions can be added** to the application - e.g. Flyway or Liquibase to generate the necessary setup and initial scripts.

[**&raquo; Start Project on Bootify.io**](https://bootify.io/)