Friday, January 11, 2013

Basic Hibernate Application with Annotations


Hibernate is Object Relational mapping (ORM) tool which is open source and distributed under the GNU Lesser General Public License.
It is mainly used to map the class object with the relational database.It also Provides data query and data retrieval  functionality.
In hibernate database schema is maintained by either xml file or using Annotations.

Project View After Completion

File->New->Project->Java Project->click next
again Click next->Finish
In pop up window Open Associative Perspective , Select No.

Hibernate Environment Setting

Right click over project on Project Explorer and goto properties->Java Build Path->Libraries
Add Required Hibernate jars, JRE system Library, Apache Tomcat server by click on Add Library and Add External Jar ojdbc14.jar by click on Add External Jars.
For Details Read How to set Hibernate Environment



Create Configuration file
Right click over Project's src folder
New->File
Click next and write configuration file name then click finish

Create Model Class
Right click over src folder
New->class
click finish.

hibernate.cfg.xml

This is a file that contains information about Oracle database(or any other database you want to use) Driver and connection information and mapping class information. 
We can also use create property in place of update.
create is a property which drops the table if it already in database and creates new one but update makes the changes in the table if its already there and if table is not there then creates a new one.
 <?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="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>  
   <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>  
   <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>  
   <property name="hibernate.connection.username">hr</property>  
   <property name="hibernate.connection.password">hr</property>  
   <property name="hibernate.show_sql">true</property>  
   <property name="hibernate.hbm2ddl.auto">update</property>  
   <mapping class="blog.webideaworld.in.Student"/>  
  </session-factory>  
 </hibernate-configuration>  

Student.java
This is a POJO class and it is working as Persistent class for Hibernate and the same java class we are using to store object of Student class using Hibernate Framework.
we are also using Annotations such as @Entity,@table,@Id etc.

If you want to give class name as a table name then remove @Table code from below code.
If we don't use Hibernate Annotations in our project then we have to create mapping file(.hbm file) 

 package blog.webideaworld.in;  
 import javax.persistence.Entity;  
 import javax.persistence.GeneratedValue;  
 import javax.persistence.Id;  
 import javax.persistence.Table;  
 import org.hibernate.Session;  
 import org.hibernate.SessionFactory;  
 import org.hibernate.cfg.Configuration;  
 @Entity  
 @Table(name="MyStudent")  
 public class Student {  
      @Id  
      @GeneratedValue  
      private int id;  
   private String name;  
   private int rollno;  
 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 int getRollno() {  
      return rollno;  
 }  
 public void setRollno(int rollno) {  
      this.rollno = rollno;  
 }  
 public static void main(String args[]){  
           Student st =new Student();  
           st.setName("jack");  
        st.setRollno(44);  
        @SuppressWarnings("deprecation")  
        SessionFactory sessionFactory     =new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();  
        Session session=sessionFactory.openSession();  
        session.beginTransaction();  
        session.save(st);  
        session.getTransaction().commit();  
      }  
 }  

Annotation @GeneratedValue uses different types are following:

To Generate Getters and Setters Right Click inside Student.java then select Source -> Generate Getters and Setters
To Run this Project Right Click inside Student.java, select Run As -> Java Application

Download Code Link 1
Download Code Link 2

Output:



No comments:

Popular Posts