Home Servlets JSP XML Struts Hibernate JPA EJB3 Jaxb Ant log4j
ORM Overview
Hibernate Overview
Hibernate Basic Example
Hibernate Example in Eclipse
Web Application using hibernate

Steps to create first Hibernate Application

By on January 21, 2014

Basic Hibernate Application :

Here, we are going to create the basic example in Hibernate. For creating the first hibernate application, we need to follow following steps:

  • Create Database Table
  • Create the Persistent(Pojo) class
  • Create the mapping file for Persistent class
  • Create the Configuration file(.cfg file).
  • Copy the jar files to classpath.
  • Create the Test class that retrieves/stores/update the persistence object.

Tools & technologies used in this article :

  • JDK 1.6
  • Hibernate 3.2.3.GA
  • MySQL 5.0

Create a new Database Schema and Table in MySQL :

Since I am demonstrating this application on MySQL database server, please ensure you have MySQL database installed on your machine. Refer My SQL Installation tutorial for installation of MySQL. Now after installation login to MySQL database using username and password from : Start -> Programs -> MySQL -> MySQL server 5.1 -> MySQL Command Line Client .Now follow the steps mentioned below

Step 1 : Create the database employeedb

From the command prompt type the query Create database employeedb;

Step 2 : Switch to created database

Type use employeedb;

Step 3 : Create table named EMPLOYEE

Now type the following queries in mywql console.

CREATE TABLE EMPLOYEE (
EMP_Id INT PRIMARY KEY AUTO_INCREMENT,
EMP_NAME VARCHAR(50),
EMP_EMAIL VARCHAR(60),
EMP_PHONE INT(15));

Create the Persistence Class(POJO) :

A simple POJO class should have a no-arg constructor so that hibernate can create the instance of the Persistent class by newInstance() method.

Employee.java

package com.jwt.hibernate;

public class Employee {

private int id;
private String empName;
private String email;
private int phone;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getEmpName() {
return empName;
}

public void setEmpName(String empName) {
this.empName = empName;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public int getPhone() {
return phone;
}

public void setPhone(int phone) {
this.phone = phone;
}

}

Create the mapping file for Persistent class :

  • Mapping file is the heart of hibernate application.
  • The mapping file name conventially, should be class_name.hbm.xml. There are many elements of the mapping file.But its not mandatory you can assign any name.
  • The mapping file contains mapping from a pojo class name to a table name and pojo class variable names to table column names.
Let's create the mapping file for the Employee class:

employee.hbm.xml

Create the Configuration file :

The configuration file contains informations about the database and mapping file.

hibernate.cfg.xml


comments powered by Disqus
   Recent Post
EJB Overview
Features of EJB 3.0
Installation Of JBoss Tools in Eclipse
Installation Of JBoss AS in Eclipse
All Rights Reserved ® javawebtutor.com