Lucas León
Lucas León

Reputation: 642

How can I generate entities classes from database using Spring Boot and IntelliJ Idea?

I trying to make a project using the Spring Initializr wizard and I already have a database, so I want to generate entities classes using Spring Boot and IntelliJ Idea.

Upvotes: 36

Views: 82812

Answers (2)

Akshay Hiremath
Akshay Hiremath

Reputation: 1105

If you are using IntelliJ Idea's newer version (2022 or so) and not able to find the option in step 3 "Generate by Database Schema" in the accepted answer above then use the following:

Step 1: Create DataSource (as you create for database in Idea)

Step 2: Add JPA Hibernate Facet through File -> Project Structure ->Facets

Step 3: Generate Entities: In the Database window,

  1. Open the data connection to your DataSource,
  2. Open schemas and find tables that you need to create entity classes for Right-click
  3. Choose Tools -> Scripted Extensions -> Generate POJOs.groovy
  4. Select the generation path (ideally your package for storing entities) and proceed.

These steps are verified and found working in IntelliJ IDEA 2022.3.3 (Ultimate Edition)

Upvotes: 1

rapasoft
rapasoft

Reputation: 961

Prerequisites are:

  1. You have your Spring Boot project initialized correctly in IDEA
  2. JPA persistence.xml file or similar has been generated correctly

Then you have to do these things:

  1. Create a DataSource - here you will add a simple DataSource that will connect to your database. The setup should be intuitive - you only provide connection details and add DB drivers (IDEA can download them for you)
  2. Add JPA/Hibernate facet. You can do it like this or this.
  3. Now you should be able to generate entities using IDEA. What you want to do here is choose Generate by Database Schema. The dialog will let you select the tables you want to use, the rest should be up to you.

Upvotes: 30

Related Questions