- • Quickstart
- • Documentation
- • Documentation DevOps
- › Spring Data
- • MongoDB
- • Spring REST
- • IntelliJ Integration
Last updated: 2022-03-28
Generating JPA Entities from an existing SQL Script
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.
To get to the import, we first start a new project via the Start Project 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.
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,
@Setter are added as annotations instead.
This brings us to the Entities tab, where the SQL import can be started.
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.
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.
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.
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.
No registration required