Hibernate Tutorials

Why ORM (Object-Relational Mapping)?

  • When we work with an object-oriented system, there's a mismatch between the object model and the relational database. RDBMS represent data in a tabular format whereas object-oriented languages, such as Java or C# represent it as an interconnected graph of objects.
  • So many problems arise like: What if we need to modify the design of our database after having developed our application?
  • ORM is the solution to all these problems

What is ORM?
  • ORM is a mechanism that map java object to a corresponding database table. Basically you define a mapping between Java classes and database tables or class members and table columns and the ORM mapper takes care of the communication between your Java code and an underlying database.
  • So that instead of writing SQL statements to interact with your database, you use methods and properties of objects. 
  • Additionally you can still use native SQL statements and JDBC but for the more common use cases you don't have to worry about the database.  
  • Example:
          Instead of writing the following code snippet

          String sql = "SELECT ... FROM persons WHERE id = 10"
          DbCommand cmd = new DbCommand(connection, sql);
          Result res = cmd.Execute();
          String name = res[0]["FIRST_NAME"];
          We simple write:
          Person p = repository.GetPerson(10);
          String name = p.FirstName;

ORM Advantages

  • Lets business code access objects rather than DB tables.
  • Hides details of SQL queries from OO logic.
  • Implementation is based on JDBC
  • No need to deal with the database implementation. 
  • Entities based on business concepts rather than database structure. 
  • Transaction management and automatic key generation. 
  • Rapid development of an application.
  • List of ORM frameworks:
    • Hibernate
    • Toplink from oracle
    • iBatis
    • Open JPA
  • Note: ORM is slower than JDBC 

What is Hibernate?
  • Hibernate is an ORM solution for JAVA
  • 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 is an implementation of JPA (Java Persistence API)
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.
  • Lazy initialization in hibernate
    • Lazy initialization in Hibernate means fetching and loading the data, only when it is needed, from a persistent storage like a database. Lazy initialization improves the performance of data fetching and significantly reduces the memory footprint.

Hibernate Architecture

  • The Hibernate architecture is layered to keep you isolated from having to know the underlying APIs. Hibernate makes use of the database and configuration data to provide persistence services (and persistent objects) to the application.
  • Hibernate uses various existing Java APIs, like JDBC, Java Transaction API (JTA), and Java Naming and Directory Interface (JNDI).
Following is a detailed view of the Hibernate Application Architecture 

Hibernate Architecture

1 comment:

  1. Thanks Admin this was really a helpful post where i learned a lot from this one and my friend say's that oracle 11g DBA Online Trainingby Hyderabadsys helps us to gain the knowledge as 2 years experienced with their +15 faculty and Live projects. This may Help others too.

    Contact :
    India : +91 9030400777
    US : +1-347-606-2716
    Email: contact@Hyderabadsys.com