JBoss application server tutorials

  • Full Screen
  • Wide Screen
  • Narrow Screen
  • Increase font size
  • Default font size
  • Decrease font size

Reverse Engineer your DB schema using JBoss Forge

jboss forge hibernate tutorialIn this article we will show a more advanced example of Java EE 6 project using Forge where Entities are reverse engineered from the Database.

So this tutorial will use the Hibernate plugin for Forge and reverse engineer a MySQL database into the Java domain.

In order to work this example you need to place MySQL JDBC driver into FORGE_HOME/lib folder

Start by creating a database let's say hibernate and add two tables in it, containing a One-To-Many relationship in it:

CREATE database hibernate;

USE hibernate;

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 get back to Forge, start the shell and at first install the hibernate plugin for Forge with the following command:

forge install-plugin hibernate-tools					

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

new-project --named as7demo --topLevelPackage com.sample --projectFolder C:\forge-distribution-1.0.5.Final\projects\as7demo

Next add persistence to it:

persistence setup --provider HIBERNATE --container JBOSS_AS7 ;

Now let's reverse engineer out schema: (customize user/database/url to your case):

generate-entities configure-settings --dialect  org.hibernate.dialect.MySQLDialect \
--driver com.mysql.jdbc.Driver --url jdbc:mysql://localhost/hibernate --pathToDriver \
C:\forge-distribution-1.0.5.Final\lib\mysql-connector-java-5.1.18-bin.jar --user jboss

***INFO*** The command [generate-entities] takes [0] unnamed argument(s), but found [1].
***INFO*** Swallowed unknown token [configure-settings] for command [generate-entities].
 ? Enter the password for JDBC connection.

Found 2 tables in datasource
Generated java at C:\forge-distribution-1.0.5.Final\bin\as7forge\as7demo\src\main\java\com\sample\model\Department.java
Generated java at C:\forge-distribution-1.0.5.Final\bin\as7forge\as7demo\src\main\java\com\sample\model\Employee.java
Generated 2 java files.

Once entered the password for the JDBC Connection the Tables will be added. Good, now add some faces scaffolding for the GUI:

scaffold setup --scaffoldType faces;

And generate the CRUD view for each view using scaffold from-entity command:

scaffold from-entity com.sample.model.* --overwrite;

Now a few customizations to our project: at first we will modify persistence.xml which wrongly points to the default H2 database. Persistence.xml can be located here in our project:

C:\FORGE-DISTRIBUTION-1.0.5.FINAL\PROJECTS\SRC
+---main
¦   +---java
¦   ¦   +---com
¦   ¦       +---sample
¦   ¦           +---model
¦   ¦           ¦       Department.java
¦   ¦           ¦       Employee.java
¦   ¦           ¦
¦   ¦           +---view
¦   ¦                   DepartmentBean.java
¦   ¦                   EmployeeBean.java
¦   ¦                   ViewUtils.java
¦   ¦
¦   +---resources
¦   ¦   +---META-INF
¦   ¦           forge.xml
¦   ¦           persistence.xml

Here's a modified version of it which uses a MySQL datasource definition:

<?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:jboss/datasources/MySqlDB</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>



You are here Home