hbm2ddl HibernateSessionFactory Hibernate

hbm2ddl HibernateSessionFactory Hibernate

hbm2ddl HibernateSessionFactory

Second Program on Hibernate – Employee

Note: It is advised to though the previous first Program – Student as this is continuation of the previous where new features are added.

The new features in this program are:

  1. Creating table (by name Employee) in the database using hbm2ddl.auto option (in the first program, we have created our own table (called school) in the database, now Hibernate creates Employee table automatically for us).
  2. Writing the properties show_sql etc. and mapping the file hbm.xml from the wizard (in the first program, this is typed manually in the XML file).
  3. All the programs are placed in our own package snrao (earlier placed in the default package of MyEclipse).
  4. Creating HibernateSessionFactory through wizard; we use this in client program (earlier in Student program, SessionFactory is created through coding using buildSessionFactory() in client program. Now we go for Session object directly).

Important Note: The two XML files, cfg and hbm must be either in src folder or in your own package.

The screens are the same (of Student) upto just before "Adding Hibernate Capabilities".

Fields in the Employee class are

int empid;
String empname;
double empsalary;
long empmobile;

I intentionally took different data types to know what columns are created with what data types in the table by Hibernate automatically. I observed when the client program is executed, at the end, Hibernate created a table Employee (on the name of the class) itself with the following columns (on the names of fields) and SQL data types; see the screen. empid is chosen as primary key.


In this application, all the bean and client programs are placed in a package called snrao. It is very necessary to create HibernateSessionFactory (the default one) in the package, it is here snrao.

Create the bean program as you did in the previous First Program: Student. Following is the code generated automatically by Hibernate (when only fields are given).

1. Adding hbm.xml file

Before going further, copy one hbm.xml file from anywhere (say, first Student program) into src folder and rename it as Employee.hbm.xml. Remember, this file should be placed in src folder. The package snrao is a subfolder of src.

2. Adding Hibernate libraries

Right click on the project –> MyEclipse –> Add Hibernate Capabilities

You will see the following screen.

hbm2ddl HibernateSessionFactory

Select the radio button Hibernate 3.2 (keep all the selected by default as it is) and click "Next" button. You see the following screen.

hbm2ddl HibernateSessionFactory

Keep the default settings as it is and click over "Browse" button and select snrao package to place the Hibernate.cfg.xml file. You get the next screen.

3. Creating Hibernate.cfg.xml file

hbm2ddl HibernateSessionFactory

Browse the DB Driver and select OIV (OIV is the name of the database connection. Actually a new connection OIV is not required and its place you can use the first program connection SNRaoConnection) to connect to the database. Click "Next" Button.

4. Creating HibernateSessionFactory interface

In the first program – Student, HibernateSessionFactory interface was created manually in the client program. Let us create it through the wizard.

hbm2ddl HibernateSessionFactory

Browse for snrao package by clicking the button "Browse" of Java Package option.

Click "Finish" button. You get the next screen.

5. Adding show_sql and hbm2ddl.auto in cfg.xml file

hbm2ddl HibernateSessionFactory

On the right side there are two options Properties and Mappings with text areas.

a) Properties

Click over "Add" button of Properties option.

hbm2ddl HibernateSessionFactory

Select show_sql from the dropdown list of Property option. In the Value text field write true and say "OK". Again repeat for hbm2ddl.auto and write the value as update.

b) Mappings

Select Mappings option.

Click "add" button and select the Employee.hbm.xml file.

Now altogether three tags are added to cfg file – two from Properties and one from Mapping.

Following is the Hibernate.cfg.xml file generated.

Following is the Properties file: Employee.hbm.xml. This is not generated but handwritten.

Observe, the table name and columns names are missing in the <id> and <property> tags. They are given by Hibernate while creating the table. But, it is necessary to write all the field names.

Now write the client program in the snrao package and run.

Following is the Client program: EmployeeClient.java

HibernateSessionFactory is obtained by default, as you have created it in 4th step). This avoids creation of Configuration object and manual loading of hibernate.cfg.xml file (in the first program, Student, you did these manually).

Observe a new table, Employee (on the name of the bean) is created in the database and record inserted.

hbm2ddl HibernateSessionFactory

Observe, the columns are not aligned properly as it looks. It is because the Java string is converted to varchar2(255) and float to 126 digits. The columns are spread a long way. This can be arrested by giving length="10" attribute in the mapping file as follows.

5 thoughts on “hbm2ddl HibernateSessionFactory Hibernate

Leave a Reply

Your email address will not be published. Required fields are marked *