Introduction to Hibernate

Introduction to Hibernate

Hibernate is a framework that helps to communicate with the database. It's also called an 'ORM' tool.

What is ORM?

ORM stands for Object Relational Mapping, where java objects are mapped with database tables, syncing one row with one record of java object is also called ORM

Hibernate helps us to write the persistence logic, a logic that is written to perform persistence operations like CRUD. To write persistence logic in java we have

  1. Technology: JDBC

  2. Framework: hibernate, spring data jpa

Even though we have JDBC what is the need for an ORM tool?

Limitations of JDBC

  1. if we write persistence logic using JDBC then we have to write SQL queries by the following syntax, queries are specific to databases, which doesn't make it portable across multiple databases

  2. If we use JDBC there's a lot of boilerplate code (code which repeats multiple times) for example load and register drive, establish a connection, create prepared statements, execute the query, process the resultset, handle the exception, closing the resource

  3. JDBC throws only one exception which is SQLException and it's a checked exception so we need handling logic otherwise code would not compile. and we don't have a detailed hierarchy of exceptions related to a different problem.

  4. JDBC ResultSet object is not serializable so we can't send it over the network

  5. while closing the JDBC connection object we need to analyze the code a lot otherwise it would result in NullPointerException

  6. we know that java is OOP based language so if we want to send a student object to the database it's not possible with JDBC because DB queries always accept the values not directly the object

  7. JDBC doesn't have good support for transactional management

  8. JDBC supports only positional parameters, it doesn't support named parameters which becomes difficult for the user to inject the values

  9. to use JDBC strong knowledge of SQL is required

  10. JDBC doesn't support versioning, and timestamp as an inbuilt feature

  11. while developing persistence logic using JDBC we cant enjoy oops features like inheritance, polymorphism, and composition because JDBC doesn't allows object as input values in SQL queries

To overcome all above problems we use ORM tool

Hibernate is a tool implemented for JPA specifications given by sunmicrosystems

JPA->Java Persistence Api (set of rules and guidlines to implment ORM)