show
hide

Java Hibernate Tutorial Part 1

Get started with JAVA Hibernate


Hibernate simple example :-


Now to start with Hibernate and practice my video you will need to have the following


1. Any relational database I am using Oracle XE 10g(you can use the one of your choice)


2. Java IDE the one I am using is eclipse indigo


3. Hibernate 3.5.6 final version which you can download from here


4. Now you also need to have few extra jars so for you to avoid going searching them I kept all the jar files that you have to use in one zip :here


In my video I have given a simple example and explained it in detail which will help us to understand the hibernate framework concepts in an easy understandable way .Also start with hibernate Framework. In the Example which i will be explaining in the video I have a table Employee table in my Oracle Database as


ID EName Mobile Email

And I will write a program to insert a record in this table. What we will be doing here is , hibernate by definition is an ORM model that is an object relation model , and thus we will map our table to a POJO class. Now what is a POJO class. A bean class without events is called a POJO class. You will understand this better once you see the video tutorial below.

Hibernate Configuration file (Hibernate.cfg.xml)







oracle.jdbc.driver.OracleDriver


jdbc:oracle:thin:@localhost:1521:XE


org.hibernate.dialect.Oracle10gDialect


Sameer


Sameer



False


true
true
true






        

import java.io.Serializable;
public class Employee implements Serializable{
       private int id ;
       private String name;
       private long mobile;
       private String email;
       public Employee(){}
       public int getId() {
              return id;
       }
       public void setId(int id) {
              this.id = id;
       }
       public String getName() {
              return name;
       }
       public void setName(String name) {
              this.name = name;
       }
       public long getMobile() {
              return mobile;
       }
       public void setMobile(long mobile) {
              this.mobile = mobile;
       }
       public String getEmail() {
              return email;
       }
       public void setEmail(String email) {
              this.email = email;
       }
}
        

                 
        
        
        
        
               
        
        
        
        
        
        
        
                 

Main class(InsertEmp.java)
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class InsertEmp {
public static void main(String[] args) {
// TODO Auto-generated method stub
Configuration cfg = new Configuration();
cfg.configure(“Hibernate.cfg.xml”);
SessionFactory sf = cfg.buildSessionFactory();
Session s = sf.openSession();
Transaction tx = s.beginTransaction();
Employee emp = new Employee();
emp.setId(2);
emp.setName(“peter”);
emp.setMobile(888);
emp.setEmail(“peter@gmail”);
s.save(emp);
s.flush();
tx.commit();
s.close();
}
}
        
Press Esc to close