Monday, 25 January 2016

Hibernate first Application

TestClient.java(For insert/update/delete operations)
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class TestClient {
public static void main(String[] args) {

Configuration cfg=new org.hibernate.cfg.Configuration();
   cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file
   
   //creating seession factory object
   SessionFactory factory=cfg.buildSessionFactory();
   
   //creating session object
   Session session=factory.openSession();
   
   //creating transaction object
   Transaction t=session.beginTransaction();
       
   Employee e1=new Employee();
   e1.setId(3888);
   e1.setFirstName("ravi");
   e1.setLastName("kumar");
   
   session.persist(e1);//persisting the object
   
   t.commit();//transaction is committed
   session.close();
   
   System.out.println("successfully saved");

}
}

TestClient.java (For selecting the records from DB)

import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class TestClient {
public static void main(String[] args) {

Configuration cfg=new org.hibernate.cfg.Configuration();
   cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file
   
   //creating seession factory object
   SessionFactory factory=cfg.buildSessionFactory();
   
   //creating session object
   Session session=factory.openSession();
   
   Query qry = session.createQuery("from Employee");

List l =qry.list();
System.out.println("Total Number Of Records : "+l.size());
Iterator it = l.iterator();

while(it.hasNext())
{
Object o = (Object)it.next();
Employee p = (Employee)o;
System.out.println("Employee id : "+p.getId());
System.out.println("Employee first Name : "+p.getFirstName());
System.out.println("Employee last Name : "+p.getLastName());
System.out.println("----------------------");
}
     session.close();
}

}

Employee.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>
  <class name="Employee" table="emp123">
    <id name="id">
     <generator class="assigned"></generator>
    </id>
         
    <property name="firstName"></property>
    <property name="lastName"></property>
         
  </class>
         
 </hibernate-mapping>

hibernate.cfg.xml

<?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>  
        <property name="hbm2ddl.auto">update</property>  
        <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>  
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:sysdba</property>  
        <property name="connection.username">scott</property>  
        <property name="connection.password">yogi</property>  
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>  
    <mapping resource="employee.hbm.xml"/>  
    </session-factory>  
</hibernate-configuration>   

Employee.java


public class Employee {
private int id;  
private String firstName,lastName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}

Class path jars: Right-click on HB app --> properties --> java build path --> libraries -->add external jars. Download path(http://hibernate.org/orm/downloads/)