Generate the JPA annotated entities from the database tables using hibernate tools

Lets take a look at some graphical step by step procedure to generate java annotated entities from the already available database tables. This can be achieved using Hibernate Tools from JBoss.

Here are some pre-requisites to be taken care of

Setup: 
1. JBOSS Tools eclipse update site
http://download.jboss.org/jbosstools/updates/stable/kepler/
2. Search & Select hibernate tools from the listing
3. Install Hibernate tools
4. Download the hibernate dependencies
http://sourceforge.net/projects/hibernate/files/hibernate4/4.2.7.Final/

Install the Hibernate Tools from the eclipse Install New Software option and go on selecting and accepting the next options:

JPA_InstallHibernateTools

 

Lets start by creating a basic JPA project and set it up for magic.

1. Create a new JPA project and add JPA facet configuration

Create e new JPA project 

JPA_createproject

 

Select the JPA Facet as Hibernate JPA. Disable JPA configuration

JPA_facetselection

Add a MySQL connection profile and test it for connectivity to your database

JPA_dbconnection

Add the MySQL driver and also Select annotated discovery of beans. The final facet page will look like this:

JPA_facetselectionfinal

 

2. Add hibernate dependencies to the build path

Go to the java build path configuration from project properties. Make sure you have downloaded the relevant dependencies and configured it as below.

JPA_buildpath

 

The final project structure shall look as per the below snapshot

JPA_projectstructure

 

 

3. I did get a classpath error and I found that I should not be using JPA but core as a Hibernate setting console configuration

Error:  [Classpath]: Could not create JPA based Configuration

Solution: Go to the Hibernate Settings from project properties and select details on your console configurations and set it as per the snapshot here.

JPA_hibernate_settingsfinal

4. Generate the entities from the table in your configured database

This is the final step where you generate the java entities by selecting the JPA tools option

JPA_generateentities

Here you have to select the package the generated entities should target along with the console configuration that we already setup before.

JPA_generateentities1

As soon as you click Finish here the processing starts and the package should get populated with entities mapped to the tables in the configured database.

We get the generated User.java entity bean here

This is a great help to kickstart your development you wont want to really start writing each annotation and associations manually.

GitHub Link: https://github.com/vishwakarmarhl/HibernateTests.git

 

 

 

 

Advertisements

One thought on “Generate the JPA annotated entities from the database tables using hibernate tools

  1. mdbikerdude says:

    For those wanting to do this without JBOSS – using the standalone open source CAM Editor (Windows/Mac/LINUX) – with simple drag and drop mapping – see the tutorial here: https://www.youtube.com/watch?v=7Wf966lPHHM

    CAM Editor also allows you to design a new database from scratch – then generate both the SQL DLL and Hibernate JPA directly.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s