Hibernate Introduction with Simple Hibernate example

Introduction to Hibernate

Hi friends ,today I am going to start one of the important framework related to databases.We always think and heard of many relational databases and related query language.For handling a big complex schema,  tables and queries the person needs to have decent knowledge about the specific Database and its related query language.At the same time, the code DAO layer becomes tightly coupled with the database queries even the schema.This becomes very difficult and time taking for a Java developer to  first learn the database query language to implement the DAO layer. Hibernate replaces these overheads and provides ORM solution to java.

High-Level Architecture

Hibernate comes as a layer between the Relational database and the Java application.Hence ,the application interact with hibernate instead of database directly.
Hibernate High Level Architecture
Here I am created  a simple hibernate example which includes all the pojo,orm  and hibernate-configuration which explain how the hibernate structure looks like.

Sample Code

The component required to implement hibernate  are

  • Pojo Classes with or without annotation.
  • ORM configuration file(Now replaced by annotation)
  • hibernate.cfg.xml

StudentInfo.java

package com.nik.test

public class StudentInfo implements java.io.Serializable { 

    private long studentId; 
    private String studentName; 

    public Student() { 
    } 

    public StudentInfo(String studentName) { 
        this.studentName = studentName; 
    } 

    public long getStudentId() { 
        return this.studentId; 
    } 

    public void setStudentId(long studentId) { 
        this.studentId = studentId; 
    } 

    public String getStudentName() { 
        return this.studentName; 
    } 

    public void setStudentName(String studentName) { 
        this.studentName = studentName; 
    } 

}


The ORM(object relational mapping) file is as follows:
Student.hbm.xml

<?xml version="1.0"?>
<!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.nik.test.StudentInfo" table="STUDENT">
    <meta attribute="class-description">
    This class contains the course details.
    </meta>
    <id name="studentId" type="long" column="student_id">
        <generator class="native"/>
    </id>
    <property name="studentName" type="string" column="student_name" not-null="true" />
</class>

</hibernate-mapping>

Hibernate configuration file is as follows:
hibenate.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 name="">
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost/testdb</property>
  <property name="hibernate.connection.username">root</property>
  <property name="connection.password">password</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="hibernate.show_sql">true</property>
  <property name="hbm2ddl.auto">create</property>
  <mapping class="com.nik.test.StudentInfo" />
    
 </session-factory>
</hibernate-configuration>

 

I hope this will help a beginner of hibernate to understand the basics .Stay tuned for more hibernate post.Till then Happy Coding!!

Leave a Reply

Your email address will not be published. Required fields are marked *