Generate JPA Entities using Hibernate Maven Plug-in


In this post, I will show working example of how to generate Hibernate JPA entities using Hibernate Maven Plug-in.

Situation might come in day to day work, where you have to work with existing database and you decided to use JPA for persistence. JPA needs entity classes. Coding entity classes manually though good for any programmer but always not be most efficient. There are many tools and plug-ins available for entities generation. Many IDEs like JDeveloper, Eclipse also support it.

Here we explore how to generate JPA entities using Hibernate hbmtoJava tools integrated with Maven.Integration of such tasks with Maven helps a lot because, it becomes part of build process and if something is broken, it immediately pops up during build.

Let's get started.

I will be using Dell DVD Store sample PostgreSQL database for this demonstration, same one I used in my post, Database Schema representation using SchemaSpy - PostgreSQL Example

Project structure I used is simple Java project built with Maven.


My POM file looks like below

<?xml version="1.0"?>
<project
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
 xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.blog.hbm.example</groupId>
 <artifactId>blogDbProject</artifactId>
 <version>1.0-SNAPSHOT</version>
 <name>blogDbProject</name>
 <dependencies>
  <dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-entitymanager</artifactId>
   <version>4.3.5.Final</version>
  </dependency>
 </dependencies>
 <build>
  <plugins>
   <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>hibernate3-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
   <components>
           <component>
      <name>hbm2java</name>
      <implementation>jdbcconfiguration</implementation>
      <outputDirectory>src/main/java</outputDirectory>
    </component>
   </components>
   <componentProperties>
     <revengfile>src/main/resources/reveng.xml</revengfile>
     <propertyfile>src/main/resources/hibernate.properties</propertyfile>
     <packagename>com.blog.hbm.pojo</packagename>
     <jdk5>true</jdk5>
     <ejb3>true</ejb3>
   </componentProperties>
    </configuration>
    <dependencies>
      <dependency>
   <groupId>cglib</groupId>
   <artifactId>cglib-nodep</artifactId>
   <version>2.2.2</version>
      </dependency>
      <dependency>
   <groupId>postgresql</groupId>
   <artifactId>postgresql</artifactId>
   <version>9.1-901.jdbc4</version>
      </dependency>
    </dependencies>
   </plugin>
   <plugin>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>2.1</version>
      <configuration>
   <source>1.7</source>
   <target>1.7</target>
      </configuration>
   </plugin>
  </plugins>
 </build>
</project>

Most important point to note from this POM is the hibernate3-maven-plugin entry. This does the job to generate JPA entities when run with hmb2java goal.


  • hbm2java - This is component of  hibernate3-maven-plugin which generates the entities. Output directory element though not mandatory it is good to specify it. If not specified, entity source code is generated in target/hibernate3/generated-sources
  • revengfile - This is another important element. Here you mention path of reven.xml file. Content of this file look like below



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC
  "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >

<hibernate-reverse-engineering>
  <schema-selection match-schema="public"/>
</hibernate-reverse-engineering>

This file provides information about schema. If not specified default schema name matching to user name used to log on to DB.
  • propertyfile - Here location of hiberante.properties file goes. This file holds details of different parameters required to connect to database, in my case local postgreSQL DB.

hibernate.connection.driver_class=org.postgresql.Driver
hibernate.connection.url=jdbc:postgresql://localhost:5432/dellstore2
hibernate.connection.username=postgres
hibernate.connection.password=********
  • packagename - Here we mention complete package name in which entities are generated.
  • ejb3 - This option set to 'true' generates entities annotated with JPA annorations. If this is set to 'false' , simple JavaBeans are generated with no JPA annotations


This plug-in must be provided with dependency jar which hold database driver classes. As this post uses PostgreSQL DB as example, dependency of postgresql jar is provided

<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>

To generate entities, use hbm2java goal as below,


Once successful execution of goal, entities are generated in output location provided.





That is it. Here I demonstrated how how to generate Hibernate JPA entities using Hibernate Maven Plug-in. Hope this helps.



3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  2. Wow, What an Outstanding post. I found this too much informatics. It is what I was seeking for. I would like to recommend you that please keep sharing such type of info.If possible, Thanks. Salesflow

    ReplyDelete