Sunday, March 15, 2015

Forward engineer (generate POJO) from ER model


Series:







Setup Java SDK


Download. Install. Both SDK and the JRE.

Set up eclipse


Download zip file. Unzip the file.

Increase heap size in eclipse.ini:

-Xms40m

-Xmx512m

 

Note: The memory is used by Eclipse not that of JVM that is launched for the program. The JVM settings are configured elsewhere in the project (in the run configuration).

Validate: Help à About Eclipse à Installation Details

Set up hibernate tools


In Eclipse: Help à Install New Software à Add new (HibernateTools, http://download.jboss.org/jbosstools/updates/stable/luna/) à Select JBoss Web and Java EE Development à Hibernate Tools

(optional though highly recommended) Setup a Maven Web Project


Eclipse à New à Project à Maven Project àà select arch type “maven-archtype-webapp”

This creates all the necessary folder structure and helps resolve all dependencies.

Setup up MySQL jdbc driver


Include a dependencies in pom.xml

                              <dependency>

                                             <groupId>mysql</groupId>

                                             <artifactId>mysql-connector-java</artifactId>

                                             <version>${mysqljdbc.version}</version>

                              </dependency>

 

Alternatively….

Download zip file. Unzip. In Eclipse Windows à Preference à Java à Build Path à User Libraries à New à Add a name (e.g. MySQL) à Select MySQL à Add External Jars à Browse to the unzipped folder à select mysql-connector-java-5.1.34-bin.jar

Configure Maven pom for Hibernate (and Tools) dependencies


                              <dependency>

                                             <groupId>org.hibernate</groupId>

                                             <artifactId>hibernate-core</artifactId>

                                             <version>${hibernate.version}</version>

                              </dependency>

                              <dependency>

                                             <groupId>org.hibernate</groupId>

                                             <artifactId>hibernate-entitymanager</artifactId>

                                             <version>${hibernate.version}</version>

                              </dependency>

                              <dependency>

                                             <groupId>org.slf4j</groupId>

                                             <artifactId>slf4j-api</artifactId>

                                             <version>${slf4.version}</version>

                              </dependency>

                              <dependency>

                                             <groupId>org.slf4j</groupId>

                                             <artifactId>slf4j-log4j12</artifactId>

                                             <version>${slf4.version}</version>

                              </dependency>

                              <dependency>

                                             <groupId>javassist</groupId>

                                             <artifactId>javassist</artifactId>

                                             <version>${javaassist.version}</version>

                              </dependency>

                              <dependency>

                                             <groupId>commons-logging</groupId>

                                             <artifactId>commons-logging</artifactId>

                                             <version>${commonlogging.version}</version>

                              </dependency>

                              <!-- for Hibernate tools -->

                              <dependency>

                                             <groupId>c3p0</groupId>

                                             <artifactId>c3p0</artifactId>

                                             <version>${c3p0.version}</version>

                              </dependency>

Configure persistence.xml


This file is not needed if Spring is used. But Hibernate Tools need this file.

You will have to declare all your @Entity classes in this file…which is a pain.

Also, you will have to configure JDBC configuration which is duplicated when you use Spring (context.xml).

We will Unit test the Entities POJO using SpringTestCase and DBUnit in next post.

Save this file “src\main\resources\META-INF” folder.


Note of hibernate.reveng.xml


This file can be pre-created or is created in the process of using HibernateTools. This file is present in the same folder as persistence.xml.

This is where you tell hibernate what all tables you want to forward engineer to POJO besides other things.


Hibernate Perspective


Configure Properties

Open Hibernate Perspective

Configure Hibnerate Configuration.






validate DB connection


Code Generate (POJO)


Launch the code generate wizard.


Create or reuse hibernate.reveng.xml

In “reveng.xml” Select window above à Create New à Select Path à Choose tables you want to generate POJO


Clean up after code generation
Use search and replace….:)
·        Notepadd++, TextPad, etc. search in files and replace.
·        Or Cygwin, save your files in a directory in cygwin and issue sed.
·        Or VMPlayer with CentOS, ave your files in a shared directory and issue sed.
 
Remove:
1.       @Stateless from DAOs (*Home classes)
2.      import javax.ejb.Stateless; (*Home classes)
Optional clean up after code generation
Remove (catalog = “asdf”)

 
 
Double check persistence.xml

For the @Entity classes (code generation overwrites this file).
 

 
Validate


Done with code generation (POJO) for persistence layer and DAO layer J

No comments:

Post a Comment