Square CSS

Tuesday, January 22, 2019

Hibernate Tutorial : Introduction

Hibernate is an open source lightweight, Object Relational Mapping framework to interact Java classes with databases. It implements the specifications of JPA as a persistence layer.

Benefits 


Following are the benefits of using hibernate:

  • It is a lightweight framework which increases the efficiency of a project.
  • It helps in the interaction of java model class with the table of a database.
  • It maintains the first-level cache thus increasing the efficiency.
  • It implements the specifications of JPA thus helps to avoid the use of database-specific language.
  • It helps in writing queries both in native and hibernate query language.

Object Relational Mapping


Object Relational Mapping is a technique through which an object can interact with the database. 
The java code creates an object and the fields of this object get mapped with the column of the table in the database.


The different field of an object will be mapped with the different columns of a table. Internally this process is achieved with the help of JDBC (Java Database Connectivity).

Conclusion


In this tutorial, information is provided about the overview of Hibernate. In the next tutorial, the setup regarding hibernate will be elaborated.



 ☛ Next >> Hibernate Tutorial: Setting Up Environment

                    This series of tutorials is for Hibernate. The tutorial will cover ... 


Monday, January 21, 2019

Hibernate Tutorial: Setting Up Environment

This series of tutorials is for Hibernate. The tutorial will cover from setting up Hibernate to fetching the details from the database using criteria. This series of tutorials will provide in-depth knowledge of Hibernate Framework with examples associated with them.





Introduction


Hibernate is an open source ORM (Object Relational Mapping) framework which provides database connectivity for JAVA. It maps Java POJO (Plain Old Java Object) or model classes to the database tables and performs most of the database persistence tasks.


Setting Up Environment


Prerequisites

One should have little knowledge of Hibernate, Eclipse, and Maven(how the dependencies are created using pom.xml).

Setup

In this setup, the project is created using Maven, using Eclipse as IDE.Go to New > Maven Project then click on Next.





Click on the option to Create a simple project and click on Next.


Provide the Group id, Artifact id, Name, and Description then click on Finish.


Now provide the following dependencies in the pom.xml file.






<dependencies>
     
      <dependency>
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>            
             <version>5.1.15</version>
       </dependency>

       <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.6.3.Final</version>
      </dependency>

      <dependency>
           <groupId>javassist</groupId>
           <artifactId>javassist</artifactId>
           <version>3.12.1.GA</version>
      </dependency>

      <dependency>
           <groupId>ch.qos.logback</groupId>
           <artifactId>logback-core</artifactId>
           <version>0.9.28</version>
      </dependency>

      <dependency>
           <groupId>ch.qos.logback</groupId>
           <artifactId>logback-classic</artifactId>
           <version>0.9.28</version>
           <scope>test</scope>
      </dependency>

       <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.5</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.6.4</version>
        </dependency>

</dependencies>







Go to src/main/resources and create a hibernate.cfg.xml file.
Here ".cfg" depicts that the file is a configuration file for hibernate.
Inside this file provide the following data for hibernate configuration.






<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">   
<hibernate-configuration>

       <session-factory>
   
               <property name = "hibernate.dialect">
                             org.hibernate.dialect.MySQLDialect
               </property>
               <property name = "hibernate.connection.driver_class">             
                             com.mysql.jdbc.Driver
               </property>
               <property name = "hibernate.connection.url">
                             jdbc:mysql://localhost:3306/playjava
               </property>
               <property name = "hibernate.connection.username">
                             root
               </property>
               <property name = "hibernate.connection.password">
                             playjava
               </property>
               <property name="hibernate.show_sql">
                             true
               </property>
               <property name="hbm2ddl.auto">
                            update
               </property>

               <mapping class="com.sumit.playjava.Demo"/>

    </session-factory>

</hibernate-configuration>



When the POJO is required to be mapped to the database using hibernate then the file name should be provided in the mapping class. As an example in the configuration file, Demo.java file is created in the package com.sumit.playjava at path src/main/java.
The class file should be annotated with @Entity to consider hibernate as the mapping file.

Conclusion


This is about the setting up of hibernate environment. In the next post tutorial will be about setting data in the database.

Some Algorithms

Algorithm: Tower of Hanoi

Tower of Hanoi consists of three towers called as pegs with n number of rings. Rings are of different size.  Conditions to be fulfill...

Popular Posts