MrBool
You must be logged in to give feedback. Click here to login
[Close]

You must be logged to download.

Click here to login

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Know what is Hibernate ORM Framework and how to work with it

This article will discuss about hibernate framework along with its usage and examples.

[close]

You didn't like the quality of this content?

Would you like to comment what you didn't like?

Introduction:

Hibernate can be used to make the database tables from classes of java applications. The data types present in the classes of java application can be converted to SQL data types in an easy manner by using the techniques present in this object-relational mapping library which is available for java language. Hibernate provides a framework to make the relational database system from object oriented domain model and conversion is done by following the practices available to develop the application using the functionality of this accessible library. In order to retrieve the data from the tables where there is a need to access the results from two or more tables present in the respective database system there is no need of any complex join and the developer does not wait for the results to appear in the front screen of the application and data appears in the front end in a fast manner. The status facility about a particular query and the available database system is provided by hibernate and so this helps in knowing that either the query entered was executed successfully or not and how many available records are present in the database system at a particular point of time in the respective manner.

Description:

Hibernate basically helps in interaction with the database system. It also helps in the creation of data, the manipulation of data and provides the access to the available data from the table in a successful manner. This object relational mapping library uses the java database connectivity (JDBC) API in order to interact and get access to the database system. There are separate functionalities available in order to query the database system and to retrieve the desired set of results from the set of data available in the database system that are accessible at a particular point in time. The respective required SQL calls are generated and special attempts are made in order to facilitate the developer to avoid the functionality and perform other operations manually but using hibernate in java, the user can finish the task early by getting the automatic results. The application developed will be able to run on any platform on which the java virtual machine (JVM) is available. The resulting application will be able to give the desired results in a successful manner which supports all the databases. The application will be completed after the proper checks of performance and the related issues and after the successful testing it is made to run in a successful manner on the required machine. There is a query language available from Hibernate known as Hibernate Query Language (HQL) and with the help of this language, it allows the queries written in SQL to access the data objects present in hibernate. HQL basically helps in the generation of database independent queries and it is different version of SQL known as the object-oriented. There was a need to change the queries if a particular database attached is changed but now with the usage of hibernate the problems are almost finished and the developer does not need to worry about the generation and manual work with the database system.

Overview:

In order to map classes to the tables the configuration are required to be handled and the related statements are entered in the XML file to complete this process and to ensure that the application will run in a successful manner and no problems will appear during the process. The proper usage of XML files makes hibernate to generate the skeletal code. Hibernate is used by entering the configuration information in the XML file in which the configurations are present and the database design is maintained in a proper manner with the help of these configurations. The development time is reduced when programming application using this library. In order to make a change in the database or in the table, there is a need to just change the properties of XML file where the configurations are present and the work is done in this simple manner.

Importance:

The tables are automatically created and the Hibernate API has also finished the maintenance problems which occurred at the time of data usage. The software developer now just keeps the focus on the front end logic of java based application and the manual creation of tables in a database system is no more required because the things are automated when Hibernate package is used. Hibernate being an object-relational mapping library can be used in the applications to be developed in Java programming language and by the usage of related service components available in J2EE. The application programming interface of Hibernate is present in the java package of “org.hibernate.*” to access the services available. This API does not need any tables from the database system and contains the ability to automatically generate the SQL at the time of initialization and it has the ability to perform much faster than the JDBC and shows the performance difference when application is executed in a successful manner. The access to database is minimized in order to retrieve and display the results from the table of database system. Hibernate is able to support any database management system. Some of the database systems include Microsoft SQL Server, MySQL, Oracle and Sybase SQL Server. Hibernate is a solution of Object Relational Mapping (ORM) available in Java. The required code to be written is less and easy so it can be easily maintained by the programmer which helps in the performance of an application. This API focuses on the access of data which is in the form of tables present in the database system. Hibernate minimizes the time spent with the manual handling of data using JDBC and SQL and is utilized on the development of an application by automatic generation of SQL queries. The configurations are needed to be handled in order to complete the automatic generation of SQL schema to the database system.

Screenshots:

This screen shows the service tab and the database table. The left side shows the hierarchy of files and the grouping of tables with respect to database.

Service tab and the database table

Figure 1: Service tab and the database table

The left side of this screen shows the total project files on which the work of Hibernate framework is done and classes and the files of configurations of this framework are also shown in the form of hierarchy.

Total project files

Figure 2: Total project files

This screen shows the main configuration file hibernate used to create table in the database and the insertion of data in the table. The codes are given after this in detail.

Main Configuration

Figure 3: Main Configuration

Implementation & Code:

The codes are given from the running web application and the names of files are also specified respectively.

Listing 1 Manage_Student_Details.java:

package hiberworkpack;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Manage_Student_Details {
     public static void main(String []args){
         System.out.println("INSERTING DATA IN TABLE");
        try{
            Configuration cf=new Configuration();
            cf.configure("hibernate.cfg.xml");
            SessionFactory fact;
            fact=cf.buildSessionFactory();
            Session ss=fact.openSession();
            Transaction tr=ss.beginTransaction();
            Student st=new Student();
            st.setindexidd(334);
            st.setstuid(4532);
            st.setstname("Uzair Khan Shahid");
            st.setstaddr("sjdnsdkjn");
            st.setdate("2013-02-23");
            st.setinstname("NUML");
            st.setdegree("BBA");
            ss.save(st); 
            System.out.println("Work completed");         
 tr.commit();            
            ss.close(); 
        }catch(Exception e){
            e.getMessage();            
        }
     }
}

First we make Configuration object and define the xml configuration file and make a SessionFactory object related to this configuration. This object is later used to obtain object for making a new session and hence a transaction.

Now simply we place the student values inside the setter method. We save this using the save method and then on commit the transaction to make this permanent.

Listing 2 Student.java:

package hiberworkpack;
public class Student {
    private int ida;
    private int stid;
    private String stname;
    private String staddress;
    private String stdate;
    private String institutename;
    private String stdegree; 
    public Student(){        
    }
    public Student(int s_id,int s_stid,String s_stname,String s_staddress,String s_stdate,String s_institutename,String s_stdegree){
        ida=s_id;
        stid=s_stid;
        stname=s_stname;
        staddress=s_staddress;
        stdate=s_stdate;
        institutename=s_institutename;
        stdegree=s_stdegree;
    } 
    public void setindexidd(int inid){
        ida=inid;
    }
    public void setstuid(int iniid){
        stid=iniid;
    }
    public void setstname(String stn){
        stname=stn;
    }
    public void setstaddr(String str){
        staddress=str;
    }
    public void setdate(String dd){
        stdate=dd;
    }
    public void setinstname(String inname){
        institutename=inname;
    }
    public void setdegree(String stdeg){
        stdegree=stdeg;
    } 
    public int getindexidd(){
        return ida;
    }
    public int getstuid(){
        return stid;
    }
    public String getstname(){
        return stname;
    }
    public String getstaddr(){
        return staddress;
    }
    public String getddate(){
        return stdate;
    }
    public String getinstname(){
        return institutename;
    }
    public String getdeg(){
        return stdegree;
    }
}

The Above represent a bean for setting and getting various student information values.

Listing 3 Student_Manage.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="hiberworkpack.Student" table="STUDENT">
      <id name="ida" column="ID">
          <generator class="assigned"></generator>
      </id>
      <property name="stid" column="ST_ID"></property>
      <property name="stname" column="ST_NAME"></property>
      <property name="staddress" column="ST_ADDRESS"></property>
      <property name="stdate" column="ST_DATEOFBIRTH"></property>
      <property name="institutename" column="ST_NAMEOFINSTITUTE"></property>
      <property name="stdegree" column="ST_DEGREE"></property>      
  </class>
</hibernate-mapping>

The Above will be used to map properties with respective column names in the database.

Listing 4 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="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
        <property name="hibernate.connection.url">jdbc:derby://localhost:1527/Management</property>
        <property name="hibernate.connection.username">use</property>
        <property name="hibernate.connection.password">system</property>
        <property name="hibernate.connection.pool_size">10</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.connection.autocommit">true</property> 
        <mapping resource="hiberworkpack/Student_Manage.hbm.xml"/>  </session-factory>
</hibernate-configuration>

This is the configuration file where we can define the connection information, credentials and set other important values.

Conclusion

So, After completing this, I hope you are clear about an overview on hibernate and are ready to make your very own first hibernate application.

Here I only discussed insertion operation but you can perform different operations. Hope you liked it.

See also:



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
Know how to keep MrBool Online
SUPPORT US
SUPPORT US
With your help, we can keep providing free content and helping you to be a better professional
support us
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually � in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits � in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login