Powered by Blogger.

Simple Hibernate Example on Inserting Employee details using MySql

>> Wednesday, May 25, 2011

Hibernate is a solution for object relational mapping and a persistence management solution or persistent layer. This is probably not understandable for anybody learning Hibernate.

What you can imagine is probably that you have your application with some functions (business logic) and you want to save data in a database. When you use Java all the business logic normally works with objects of different class types. Your database tables are not at all objects.

Hibernate provides a solution to map database tables to a class. It copies the database data to a class. In the other direction it supports to save objects to the database. In this process the object is transformed to one or more tables.

Saving data to a storage is called persistence. And the copying of tables to objects and vice versa is called object relational mapping.

Example program on Hibernate.

Required jars to run simple hibernate program.

antlr-2.7.6
commons-collections-3.1
dom4j-1.6.1
hibernate3
hsqldb
javassist-3.4.GA
jta-1.1
slf4j-api-1.5.6
slf4j-simple-1.5.6

download the jar files of Hibernate.
To run the Hibernate program we need four elements or files.
They are:

1.PojoClass.java
2.hibernate.cfg.xml
3.pojoclass.hbm.xml
4.ClientFile.java (with main method)

In our Example the files are:


In our example we are using MySql database.For that we need the driver_class,connection_url,username,password,MySqldialect.
These are configured in hibernate.cfg.xml file to configure the MySql database.

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://192.168.1.101:3306/TEST</property>
        <property name="connection.username">test</property>
        <property name="connection.password">test</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Mapping files -->
        <mapping resource="com/javabynataraj/empdet.hbm.xml"/>

        
    </session-factory>
</hibernate-configuration>

Then by providing mapping to the java pojo class to database table we can do the operations whether inserting the data or updating or deleting using Session,SessionFactory,Transaction interfaces and Configuration class.

The pojo class Example:

package com.javabynataraj;

import java.io.Serializable;

public class Empdet implements Serializable{
 
 int eno;
 String ename;
 int esal;
 String eadd;
 
 public Empdet(){
  
 }
 
 public int getEno() {
  return eno;
 }
 public void setEno(int eno) {
  this.eno = eno;
 }
 public String getEname() {
  return ename;
 }
 public void setEname(String ename) {
  this.ename = ename;
 }
 public int getEsal() {
  return esal;
 }
 public void setEsal(int esal) {
  this.esal = esal;
 }
 public String getEadd() {
  return eadd;
 }
 public void setEadd(String eadd) {
  this.eadd = eadd;
 }
}

Only the data is going to inserted or updated scenarios we use Transaction ,for select query we don't use Transaction.

Write  a pojo class to get and set the values from database and the user.And we can do serialize the pojo class object .And we have to write a constructor for this class additionally.

We have taken here four variables based on table in database.
    int eno;
    String ename;
    int esal;
    String eadd;

And Map the pojo calss names and database table columns in empdet.hbm.xml as below given:
Java Persistence with Hibernate


<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="com.javabynataraj.Empdet" table="empdet">
 
 <id name="eno" type="int" column="eno">
  <generator class="increment"/>
 </id>
 <property name="ename" type="string" column="ename" />
 <property name="esal" type="int" column="esal" />
 <property name="eadd" type="string" column="eadd" />
</class>

</hibernate-mapping>

The final step is writing Clientfile(Empdetails.java)to insert employee details in the database table using pojo class.

package com.javabynataraj;

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

public class Empdetails {
 public static void main(String[] args) {
  
  Configuration conf = new Configuration().configure();
  SessionFactory factory = conf.buildSessionFactory();
  Session session = factory.openSession();
  Transaction tx = session.beginTransaction();
  
  Empdet edet = new Empdet();
  
  edet.setEno(1);
  edet.setEname("murali");
  edet.setEsal(5000);
  edet.setEadd("Chennai");
  
  session.save(edet);
  
  tx.commit();
  System.out.println("The data has been saved......");
 }

}

After running your Client file (Empdetails.java)as normal java program.then you will display with hibernate query and the last given print statement.
Given below..

The database table in MySql is:

based on given primary key generation the eno will generate automatically by increment order.

Download the above Example Here.

For Reference:
Harnessing Hibernate

Related Posts Plugin for WordPress, Blogger...
© javabynataraj.blogspot.com from 2009 - 2014. All rights reserved.