Reverse Engineer your DB schema using JBoss Forge

[Updated] In this article we will show a more advanced example of Java EE Application using JBoss Forge 3 capabilities to reverse engineer the Entities from the Database.

So this tutorial will use the Hibernate plugin for Forge and reverse engineer a MySQL database into the Java domain. Start by creating a database let's say demoforge and add two tables in it, containing a One-To-Many relationship in it:

CREATE database demoforge;

USE demoforge;

CREATE TABLE department (
department_id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
department_name VARCHAR(45) NOT NULL,
PRIMARY KEY (department_id)
);
 
CREATE TABLE employee (
employee_id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
employee_name VARCHAR(45) NOT NULL,
employee_salary INTEGER UNSIGNED NOT NULL,
employee_department_id INTEGER UNSIGNED NOT NULL,
PRIMARY KEY (employee_id),
CONSTRAINT FK_employee_1 FOREIGN KEY FK_employee_1 (employee_department_id)
REFERENCES department (department_id)
ON DELETE CASCADE
ON UPDATE CASCADE
);				

Here's a view of our simple schema:

jboss forge hibernate

Now let's start Forge:

/bin/forge					

Then, create a new project (params are optional though you might add them to automate the project creation):

project-new --named demo-forge --stack JAVA_EE_7 --type war

A Web project named jpa-demo will be created, using the Java EE 7 API and defaults to Maven project structure. In order to enable reverse engineering we need at first set up the JPA with the jpa addon:

jpa-setup --data-source-name MySQLDS --db-type MYSQL

Now let's reverse engineer out schema: (customize the Path where the JDBC Driver is located):

jpa-generate-entities-from-tables --jdbc-url jdbc:mysql://localhost/demoforge --user-name jboss --user-password jboss --driver-location /tmp/mysql-connector-java-5.1.18-bin.jar --hibernate-dialect org.hibernate.dialect.org.hibernate.dialect.MySQL5Dialect --driver-class com.mysql.jdbc.Driver --database-tables *

So with the above command, we will reverse enginner all tables contained in the schema demoforge into Entities. Good, now add some faces scaffolding for the GUI:

scaffold-generate --targets org.demo.forge.model.* --web-root demo-forge --provider Faces

Here is the project structure generated:

forge reverse enginner

And this is the persistence.xml file that was automatically created:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="forge-default" transaction-type="JTA">
    <description>Forge Persistence Unit</description>
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:/MySQLDS</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.format_sql" value="true"/>
      <property name="hibernate.transaction.flush_before_completion" value="true"/>
    </properties>
  </persistence-unit>
</persistence>

Follow us on Twitter