Hibernate ORM overview

Автор

Неизвестен

Источники

http://www.tutorialspoint.com/hibernate/orm_overview.htm

http://www.tutorialspoint.com/hibernate/hibernate_overview.htm

ORM Overview

What is JDBC?

JDBC stands for Java Database Connectivity and provides a set of Java API for accessing the relational databases from Java program. These Java APIs enables Java programs to execute SQL statements and interact with any SQL compliant database.

JDBC provides a flexible architecture to write a database independent application that can run on different platforms and interact with different DBMS without any modification.

Pros of JDBC

- clean and simple SQL processing;
- good performance with large data;
- very good for small applications;
- simple syntax so easy to learn.

Cons of JDBC

- complex if it is used in large projects;
- large programming overhead;
- no encapsulation;
- hard to implement MVC concept;
- query is DBMS specific.

Why Object Relational Mapping (ORM)?

When we work with an object-oriented systems, there's a mismatch between the object model and the relational database. RDBMSs represent data in a tabular format whereas object-oriented languages, such as Java or C# represent it as an interconnected graph of objects. Consider the following Java Class with proper constructors and associated public function:

public class Employee {

    private int id;
    private String first_name;
    private String last_name;
    private int salary;

    public Employee() {}

    public Employee(String fname, String lname, int salary) {
        this.first_name = fname;
        this.last_name = lname;
        this.salary = salary;
    }
    public int getId() {
        return id;
    }
    public String getFirstName() {
        return first_name;
    }
    public String getLastName() {
        return last_name;
    }
    public int getSalary() {
        return salary;
    }
}

Consider above objects need to be stored and retrieved into the following RDBMS table:

create table EMPLOYEE (
    id INT NOT NULL auto_increment,
    first_name VARCHAR(20) default NULL,
    last_name VARCHAR(20) default NULL,
    salary INT default NULL,
    PRIMARY KEY (id)
);

First problem, what if we need to modify the design of our database after having developed few pages or our application? Second, Loading and storing objects in a relational database exposes us to the following five mismatch problems.

The Object-Relational Mapping (ORM) is the solution to handle all the above impedance mismatches.

What is ORM?

ORM stands for Object-Relational Mapping (ORM) is a programming technique for converting data between relational databases and object oriented programming languages such as Java, C# etc. An ORM system has following advantages over plain JDBC

S.N. Advantages
1 Lets business code access objects rather than DB tables.
2 Hides details of SQL queries from OO logic.
3 Based on JDBC 'under the hood'
4 No need to deal with the database implementation.
5 Entities based on business concepts rather than database structure.
6 Transaction management and automatic key generation.
7 Fast development of application.

An ORM solution consists of the following four entities:

S.N. Advantages
1 An API to perform basic CRUD operations on objects of persistent classes.
2 A language or API to specify queries that refer to classes and properties of classes.
3 A configurable facility for specifying mapping metadata.
4 A technique to interact with transactional objects to perform dirty checking, lazy association fetching, and other optimization functions.

Java ORM Frameworks:

There are several persistent frameworks and ORM options in Java. A persistent framework is an ORM service that stores and retrieves objects into a relational database.

Enterprise JavaBeans Entity Beans
Java Data Objects
Castor
TopLink
Spring DAO
Hibernate
And many more

Hibernate Overview

Hibernate is an Object-Relational Mapping(ORM) solution for JAVA and it raised as an open source persistent framework created by Gavin King in 2001. It is a powerful, high performance Object- Relational Persistence and Query service for any Java Application.

Hibernate maps Java classes to database tables and from Java data types to SQL data types and relieve the developer from 95% of common data persistence related programming tasks.

Hibernate sits between traditional Java objects and database server to handle all the work in persisting those objects based on the appropriate O/R mechanisms and patterns.

pic1

Hibernate Advantages

Hibernate takes care of mapping Java classes to database tables using XML files and without writing any line of code.
Provides simple APIs for storing and retrieving Java objects directly to and from the database.
If there is change in Database or in any table then the only need to change XML file properties.
Abstract away the unfamiliar SQL types and provide us to work around familiar Java Objects.
Hibernate does not require an application server to operate.
Manipulates Complex associations of objects of your database.
Minimize database access with smart fetching strategies.
Provides Simple querying of data.

Supported Databases

Hibernate supports almost all the major RDBMS. Following is list of few of the database engines supported by Hibernate.

HSQL Database Engine
DB2/NT
MySQL
PostgreSQL
FrontBase
Oracle
Microsoft SQL Server Database
Sybase SQL Server
Informix Dynamic Server

Supported Technologies

Hibernate supports a variety of other technologies, including the following:

XDoclet Spring
J2EE
Eclipse plug-ins
Maven