How to Generate JPA Entities from database tables with Eclipse and Spring Boot?

Introduction

In eclipse, we can generate tables from existing entities. Which is done by defining the spring JPA hibernate ddl-auto property in YAML or property configuration. Below is the example configuration for application.properties:

spring.jpa.hibernate.ddl-auto=update

There are other values you can define, including create, create-drop, validate, update, and none. These values are used to tell how the schema tool management will manipulate database schema which includes tables.

However, if you’re wondering, that there is a way you can generate JPA entities from database tables. The answer is yes, you can do that. Or else, if your database contains a lot of tables. But, you don’t want to waste your time creating an entity class for each table. Therefore, this is the right article for you.

For more information about JPA, you can check out this link: http://lgeratech.com/2020/06/implement-spring-data-jpa-in-restful-web-service/

There are JPA tools in Eclipse IDE that we can use to generate entities. In this article, you will learn how to set up JPA tools. And how is it going to interact with the database to generate class entities.

What you need

  • MySQL database
  • Existing database and tables (make sure you have existing tables before we start the configuration)
  • MySQL Connection Driver
  • Eclipse IDE
  • Spring Boot Project

Install Dali JPA Tools

Skip this section if you already installed Dali JPA Tools, in Help menu, select Install New Software. And then type the filter to search for JPA tools:

Once you installed the JPA tools, it will suggest to restart the eclipse IDE.

Convert to JPA Project

After the eclipse is restarted, follow the step below to convert your project to JPA project:

  • In Package Explorer tab, look for the project you need, then right click.
  • Then select Configure and click Convert to JPA Project…
  • Once clicked, the Project Facets selection is opened, select the Java and JPA facets and click Next.
  • To the next dialog, add src folder and then click Next.
  • Once done, the JPA Facet configuration dialog will open.
  • In Platform drop down list select EclipseLink.
  • Then select Disable Library Configuration in JPA implementation drop down list.
  • In order to generate entity classes, you must have existing tables and database.
  • Click the Add Connection… link in the Connection section to add your existing database
  • Once clicked, the New Connection Profile will open.
  • Select MySQL for the example connection profile.
  • Enter the Name of your connection and optional for the Description field.
  • Click Next button.
  • In the next tab, we must add MySQL to connect to the database. Click the Plus icon at the right side of Driver drop down list.
  • Once clicked, another dialog will be opened.
  • In the Name/Type tab, Enter the name and the type of the driver, or click a template on the Available driver template list.
  • Once done, go to the next tab which is JAR list to add the MySQL jar file.
  • After you added the jar file, click Ok button.
  • For the final connection setup, enter the details of your database.
  • After you fill up the details, you can test the connection by clicking the Test Connection button.
  • Once the connection is succeeded, click finished button.
  • And the Finish button for JPA Facet configuration tab.

Generate Entities from Tables

Once you’ve done converting your project to JPA project, you can now generate entities form tables:

  • Right click to your project, then select JPA Tools and click Generate Entities from Tables…
  • Select the tables you want to generate.
  • Once you have selected the tables, click Finish button.

Once you’ve clicked the finish button, you will see in src folder of the project the model package with entity classes that auto-generated by JPA Tools.

Conclusion

Sometimes we need to use tools to create something, specially in it comes to the tight schedules.

Using tools, it’s not mean being a lazy programmer or developer, it means you are working smarter rather than working harder. But sometimes it is good to have both in order to achieve your goals successfully and efficiently.

2 thoughts on “How to Generate JPA Entities from database tables with Eclipse and Spring Boot?

  • Buy CBD
    August 2, 2020 at 9:24 pm

    hello!,I like your writing very much! share we communicate extra approximately your article on AOL?
    I require a specialist in this area to solve my problem.

    Maybe that is you! Having a look ahead to see you.

    my website Buy CBD

  • moncler jackets
    August 10, 2020 at 6:20 am

    Aw, this was a really nice post. In idea I wish to put in writing like this moreover ?taking time and actual effort to make an excellent article?but what can I say?I procrastinate alot and on no account appear to get something done.

Leave a Reply

Your email address will not be published. Required fields are marked *.

*
*

This site uses Akismet to reduce spam. Learn how your comment data is processed.

BCF Theme By aThemeArt - Proudly powered by WordPress.
BACK TO TOP