How to Generate an Entity from a Database Table using IntelliJ Idea


Introduction

Working with real projects, some occasions we have to deal with new tables of the existing database. So, when we work with ORM tools such as JPA, Hibernate, we have to generate the ORM Entity Object from the existing table. So, IntelliJ IDEA IDE provides an option to fulfil this purpose.

So, let's have a look at how to generate an Entity from an. existing table in the database using IntelliJ IDEA.

Firstly, there are 2 major pre requirement you need to perform this task. Those are :
    1. You need to have an Entity Manager Factory defined for the given database in the project.
    2. You need a Database  Connection Created for the given database in the IntelliJ IDE inside Database            tab displayed in the right side of the IDE. (Please refer our article on How to  Configure a Datasource         in IntelliJ IDEA for further details)


After completing the above pre requirements, you are ready for our task. So, let's have a look at the steps to perform this task.


Step 1:

Firstly as the first step, you need to enable the persistence tab in IntelliJ IDEA. You can simply navigate to View -> Tool Windows -> and Select Persistence option. Then Persistence tab'll display at the left side corner of your IDE. If you already have Persistence tab, skip this step and move to the next step.



Step 2:

As the second step, go to the persistence tab on the left hand side of the IDE and select the relevant Entity Manager Factory you are going to use to generate the entity. And then select and right click on the Entity Manager Factory and go to Generate Persistence Mapping -> By database Schema.




Step 3:

After performing the step 2, a modal will be displayed as below. It'll. ask to select the DataSource, the package name of the entity should be generated and under Database Schema Mapping table it'll display all the tables available in the selected database and you need to select the required tables which you require to generate as entity objects. After all, click ok button and IntelliJ IDEA will generate an Entity inside the entity package you entered above.
 


So, those are the simple steps to perform this task. Hope you learned something and enjoyed this article.

Keywords: database  database generation  ER Diagrams  entity relationship diagrams  java ide  intellij idea


Buy website traffic cheap

Post a Comment

2 Comments

  1. Great job keep it up..

    ReplyDelete
    Replies
    1. SoftEther VPN Client Manager - M I family

      Delete