What is the need of Hibernate?

Persistence is data that outlives the process that created it.

Relational Databases:
  1. Represent the data in two-dimensional tables.
  2. Tables can relate to each other through the use of primary and foreign keys.
  3. Primary keys uniquely identify a given row in table.
  4. Foreign keys refer to a primary key stored in another table.
If we are working with object-orientated programming and relational databases, we will surely have noticed that these are two different paradigms.
  • The relational model deals with relations, tuples and sets - it is very mathematical by nature
  • The object-orientated paradigm however deals with objects, their attributes and associations to each other.

  • Object-relational gap: When we want to make objects persistent using a relational database we will notice: There is a gap between these two paradigms, it is called object-relational gap.

    Object-Relational Mapper:

    Is responsible to map the relational data to the objects and fill the object relational gap He is responsible for solving the following mismatches in Object relational mapping.

    1. Identity:

      In java we have two types of identity.

      A. Identical (==): If two objects points the same memory location we says that two objects are identical.

      e.g - Object ob1 = new String ("hello");

      Object ob2=ob1;

      Hear ob2 & ob1 are reference to same object in a memory location so they are identical.

      B. Equal (.equals ()): If we compare objects using equal () method if it returns true then that two objects are equal.

      e.g - String s1 = new String ("hello"); String s2 = new String ("hello");

      Hear s1 == s2 returns false. s1.equals(s2) returns true. So s1 & s2 are equal and not identical.

      Primary Key: But in RDBMS we have single identity by using primary key. If two rows contain same primary key then they are identical and equal.

    2. Granularity:>

      Object-Relational Mapper

      In Object Oriented Model we declare some common types in the form of objects like Address. We reuse that common objects in another complex objects like Customer in our entire application. But in RDBMS we can’t map this reusable objects directly to the tables. We require declaring the individual columns for each simple type and map the common object fields to the individual columns in a table.

    3. Inheritance:

      Object-Relational Inheritance

      In object model we have inheritance concept here an object may have multilevel inheritance. But in RDBMS we can’t have inheritance concept. We must have customer table along with all fields.

    4. Polymorphism (sub types):

      Object-Relational Polymorphism

      In Object model we have polymorphism concept. By using polymorphism at runtime we associate with an instance of any of the sub class of that base class.

      Since RDBMS doesn’t provide inheritance and polymorphism there is no mechanism for runtime insertion.

    5. Associations:

      In RDBMS an association is represented as a foreign key column, with copies of key values in several tables. Here associations are bi-directional. Where as while representing these relations in the object model we require maintaining association between objects with the help of object references. But these references are unidirectional. So here we may require to maintaining two associations.

    6. Object Graph Navigation:

      From the relational data which is retrieved from the RDBMS building the object tree is one issue, finding the exact part of the tree required and loading the corresponding data and the 2’nd issue is navigating through the object tree and finding out the exact portion of the tree which is modified and making the changes to be effected into the RDBMS. Object graph navigation requires the joins between tables. The number of tables included in the join determines the depth of the object graph navigation.

    Why ORM:

  • Productivity:

    ORM eliminates much of the database related code in you application. ORM saves and retrieves complex objects to and from database without single line of Jdbc code. So we more concentrate on the business problems rather than Jdbc code.

  • Maintainability:

    By using ORM our application contains fewer line of code (LOC). It makes the system more understandable.

  • Vendor Independence:

    An ORM abstracts our application from the underlying SQL database and SQL dialect

  • Performance:

    Hand-coded (JDBC) persistence can always be at least as fast than automated persistence (ORM). But the people implemented ORM concentrate much more time to investigate performance optimizations than us.

  • Less error-prone code