Way2Java

Reverse Engineering Hibernate Example

Reverse Engineering Hibernate?

Reverse of hbm2ddl is reverse engineering. Lot of confusion. Yes, really. To get clarity, let us know the answers for some more questions.

Do you know the following property in hibernate.cfg.xml file?

update

The above property tag is optional in xml file. The property is if the table is not available in the database, Hibernate creates a new table and saves the Java persistent object. Depending on the JavaBean fields and their data types, a new table is created automatically; here, JavaBean is present but table is not present. This we have seen in our second program (with all execution screens step-by-step) – Employee.

Is it possible to create a JavaBean depending on the table columns (that is, table is present but JavaBean is not present), just reverse of hbm2ddl?

Yes. It is absolutely possible. This is called reverse engineering Hibernate. This is one of the strengths of Hibernate. Finally to say, creating automatically a JavaBean class, configuration (cfg) file and properties (hbm) file depending upon the columns of the database table is known as reverse engineering. Your job is just to have an empty table and a client program to insert records.

1. Creating Officer table

For this, an Officer table should be created before starting any work (because, in reverse engineering, table should be existing). Following columns and data types are chosen.

empid number(4) primary key
empname varchar2(15)
empsal number(6,2)

Your SQL query for the above fields is

SQL> create table Officer (empid number(4) primary key, empname varchar2(15), empsal number(6,2));

2. Go to MyEclipse Database Explorer perspective (right top corner of MyEclipse)

Right click on SNRaoConnection (how to create SNRaoConnection is shown in Create Database Connection Permanently) and select open connection. You get a window like this.

Enter tiger in Password text box and say "OK".

3. Your works starts with "Connected to SNRaoConnection"

In the DB Browser (like Package explorer, left panel), you will see like this.

Select SCOTT –> TABLE –> OFFICER

Right click on the OFFICER table –> choose Hibernate Reverse Engineering.

You get the screen like this.

4. Hibernate Reverse Engineering screen

a) Choose a src folder in Java src folder.

b) Select the check boxes "Create POJO" and also of "Java Data Object". See the above screen. Select "Finish" button (not Next button).

Select "OK" button. Click "Run at Background" button.

5. Now you can observe in the next screen, the creation of cfg and hbm etc files by Hibernate.

6. Hibernate capabilities are automatically added (if not add them as usual clicking over the project).

You are now in MyEclipse Hibernate perspective (you come into it implicitly while running the above steps).

Now change to MyEclipse Java Enterprise perspective.

7. Open hibernate.cfg.xml file by double clicking over it in Package explorer (left panel).

7. Write a client program and run.

Before writing the client program, observe the files created by the Hibernate reverse engineering process. This is very important to write the client program.

Observe the following screen of temp project in the Package Explorer.

Observe the above screen, reverse engineering creates the following files;

a) Officer.java (Officer is the name of the table)
b) OfficerId.java
c) AbstractOfficer.java
d) AbstractOfficerId.java

Observations from the above screen.

Important observation is we must pass an object of OfficerId to the constructor Officer. The parameters of OfficerId constructor are short, String and Double.

Would you like to know the relation between the above 4 classes?

Following class signatures give you the answer.

  1. public abstract class AbstractOfficer implements java.io.Serializable
  2. public class Officer extends AbstractOfficer implements java.io.Serializable
  3. public abstract class AbstractOfficerId implements java.io.Serializable
  4. public abstract class AbstractOfficer implements java.io.Serializable

Keeping in mind of all these observations, now write the client program and execute.

File Name: OfficerClient.java

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class OfficerClient 
{
  /**
    * @param args
  */
  public static void main(String[] args) 
  {
    // TODO Auto-generated method stub
		
    Configuration c = new Configuration();
    c.configure("/hibernate.cfg.xml");
    c.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle9Dialect"); 
		
    SessionFactory sf = c.buildSessionFactory();
    Session s = sf.openSession();
		
    Transaction t = s.beginTransaction();
		
    try
    {
      OfficerId oid = new OfficerId(new Short((short)1235),"S N Rao 5", new Double(8888.88));
      Officer off = new Officer(oid);
      s.save(off); 
      s.flush(); 
      t.commit();
      System.out.println("Record inserted using reverse engineering");
    }
    catch(Exception e) 
    {
      t.rollback();
    }
  }
}

Now the object off is saved in Officer table.