Hibernate First Example

Here we explain the first example of hibernate. Let us suppose that there is a POJO class Customer which has attributes-cid,cname, email, phone and city. The corresponding table is    
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| cid   | int(11)      | NO   | PRI |         |       |
| cname | varchar(255) | YES  |     | NULL    |       |
| email | varchar(255) | YES  |     | NULL    |       |
| phone | bigint(20)   | YES  |     | NULL    |       |
| city  | varchar(255) | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+

There is hibernate mapping file which is corresponding to Customer class call customer.hbm.xml. This mapping file is use to map the class to table and property to column.

There is a hibernate configuration file called hibernate.cfg.xml. This file is use to configure the database.

The other files are HibernateFirstTest.java which is client class, HibernateTemplete and HibernateUtil.

Here we can see the directory structure of files in Eclipse as below

Now we can see the all with file name:

Customer.java

package com.javaforecast4u;

public class Customer
{
          private int cid;
          private String cname;
          private String email;
          private long phone;
          private String city;
         
          public Customer(){}
          public Customer(String cname,String email,long phone,String city)
          {   
              super();
                   this.cname=cname;
                   this.email=email;
                   this.phone=phone;
                   this.city=city;
                  
          }
          public int getCid() {
                   return cid;
          }
          public void setCid(int cid) {
                   this.cid = cid;
          }
          public String getCname() {
                   return cname;
          }
          public void setCname(String cname) {
                   this.cname = cname;
          }
          public String getEmail() {
                   return email;
          }
          public void setEmail(String email) {
                   this.email = email;
          }
          public long getPhone() {
                   return phone;
          }
          public void setPhone(long phone) {
                   this.phone = phone;
          }
          public String getCity() {
                   return city;
          }
          public void setCity(String city) {
                   this.city = city;
          }
         
         
}

Customer.hbm.xml

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

<hibernate-mapping package="com.javaforecast4u">
<class name="Customer" table="customerss" lazy="false">
<id name="cid" column="cid" type="int">
<generator class="increment"/>
</id>
<property name="cname"/>
<property name="email"/>
<property name="phone" type="long"/>
<property name="city" />
</class>
</hibernate-mapping>

HibernateFirstTest.java

package com.javaforecast4u;
import com.javaforecast4u.util.*;

class HibernateFirstTest
{
 public static void main(String as[])
 {
   //1.Saving Object
          Customer c1=new Customer("arbind","arbind.sharma2@gmail.com",88884744,"Bangalore");
          Integer it=(Integer)HibernateTemplate.save(c1);
          System.out.println(it.intValue());
         
          //1.Loading Records by P.K
          Customer c=(Customer)HibernateTemplate.load(Customer.class,5);
System.out.println(c.getCid()+"\t"+c.getCname()+"\t"+c.getEmail()+"\t"+c.getPhone()+"\t"+c.getCity());
 
          //3.Deleting Records by P.K
          HibernateTemplate.delete(Customer.class,13);
         
          //4.Updating Records by P.K
          c=(Customer)HibernateTemplate.load(Customer.class,9);
    c.setCname("arbind");
    c.setEmail("arbind.sharma2@gmail.com");
    HibernateTemplate.update(c);
 
 }
}

HibernateUtil.java

package com.javaforecast4u.util;
import org.hibernate.*;
import org.hibernate.cfg.*;

public class HibernateUtil
{
 static SessionFactory sessionFactory=null;
 static
 {
          Configuration cfg=new Configuration();
          cfg=cfg.configure();
          sessionFactory=cfg.buildSessionFactory();
 }
 public static SessionFactory getSessionFactory()
 {
           return sessionFactory;
 }
}

HibernateTemplate.java

package com.javaforecast4u.util;
import java.io.Serializable;
import org.hibernate.*;

public class HibernateTemplate
{
  public static Object save(Object obj)
  {
          Object o=null;
          try
          {
            SessionFactory sf=HibernateUtil.getSessionFactory();
            Session session=sf.openSession();
            Transaction tx=session.beginTransaction();
            o=session.save(obj);
            tx.commit();
            session.close();
          }
          catch(Exception e)
          {
            e.printStackTrace();  
          }
          return o;
  }
 
  public static Object load(Class cls,Serializable s)
  {
          Object o=null;
          try
          {
            SessionFactory sf=HibernateUtil.getSessionFactory();
            Session session=sf.openSession();
            Transaction tx=session.beginTransaction();
            o=session.load(cls,s);
            tx.commit();
            session.close();
          }
          catch(Exception e)
          {
            e.printStackTrace();  
          }
          return o;
  }
 
  public static void delete(Class cls,Serializable s)
  {
          try
          {
            SessionFactory sf=HibernateUtil.getSessionFactory();
            Session session=sf.openSession();
            Transaction tx=session.beginTransaction();
            Object o=session.load(cls,s);
            session.delete(o);
            tx.commit();
            session.close();
          }
          catch(Exception e)
          {
            e.printStackTrace();  
          }
  }
 
  public static void update(Object obj)
  {
          try
          {
            SessionFactory sf=HibernateUtil.getSessionFactory();
            Session session=sf.openSession();
            Transaction tx=session.beginTransaction();
            session.update(obj);
            tx.commit();
            session.close();
          }
          catch(Exception e)
          {
            e.printStackTrace();  
          }
  }
}

Hibernate.cfg.xml

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

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/javaforecast4u</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="com/javaforecast4u/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>





1 comment :