How to generate Spring boot entity classes from database

  • by

Entity class for a database table can be generated from tools plugged into your IDE. I am sharing you here on how to generate entity classes using IntelliJ IDEA. Let’s get started!

Remember that there are ton of ways to generate entity classes for the database you have. They are:

  • Easy way: Use an IDE and generate entity classes.
  • Online way: Google for “Online POJO from SQL generator”. Like this one.
  • Hard way: Write by yourself like an expert.

Let’s see one by one – except the last one :p

Using IntelliJ IDEA to generate entity classes from the database

I assume that you use IntelliJ IDEA. The steps are as follows:

  • Use Spring Initializr and get your spring boot project skeleton online.
  • Import and initialize the downloaded skeleton into IDEA (IntelliJ IDEA).
  • Generate or write JPA persistence.xml file or similar. Make sure it is correct.

When you have finished with the steps, we need to perform the following bigger yet simple sub-tasks. I have written in separate sections down from here to illustrate them all clearly with screenshots.

Create a data source in IDEA

Please see: If you are little mislead/confused in this section, you can still follow the detailed article here.

In this step you will be adding a simple data source that will connect to your database. Under “Database” tool window click on “New” icon to get started. When the screen loads up the option, follow the below:

DDL Data Source

Select your database (DB) under “Data Source” option. You are now given with two options to specify your target database. One way is by giving “Data Source by URL” and other by “Data Source by Path”.

Choosing MySQL as data source from IntelliJ

If you have a database service and you got a URL of it (like Oracle, MySQL, MSSQL or PostgreSQL or of similar kind) enter the URL in the input.

If you have a database of type “file” (say for example, Derby, H2, HSQLDB and SQLite or something similar to this kind) enter the path under the appropriate input box and proceed.

Adding JPA/Hibernate Facet

Use this link to add and enable Hibernate plugin in IDEA. https://www.jetbrains.com/help/idea/2016.1/enabling-hibernate-support.html?origin=old_help#d1785882e332

Generate entities using IDEA

Now you should be able to generate entities using IDEA. What you want to do here is choose “Generate by Database Schema“.

Once you have enabled JPA or Hibernate support and created persistence units or session factories, you can define mapping between the Java objects and data sources.

To map Java objects to a data source

In the Persistence tool window, expand the desired module node. Right-click the desired persistence unit or session factory to generate mappings for, and choose Generate Persistence Mapping on the context menu.

On the submenu, select the desired schema. You can generate persistence mapping by:

  • Hibernate mappings
  • Entity beans
  • Database schema

You can also create an online generator tool like this one.

Please see: I’m reading on the feedback provided here for this post. Feedback last read on : Jan 20, 2020.

How useful was this post?

Click on a star to rate it!

Average rating 1.5 / 5. Vote count: 17

We are sorry that this post was not useful for you!

Let us improve this post!

Leave a Reply

Welcome to CloudiKnow.com blog, a platform to share knowledge and solution experience related to public cloud is base design of this site.

You can start the cloud journey right from here!
Please bookmark this page by pressing simply CTRL+D.