Hibernate Introduction

Hibernate Overview

Topics

  • What is Hibernate?
  • Benefits of Hibernate
  • Code Snippets

What is Hibernate?

Your 
Java 
Hibernate

At high level you will have Java Application, it will make use of Hibernate framework for saving and retrieving data from the database.

Benefits of Hibernate

  •  Hibernate handles all the low-level SQL
  • Minimize the amount of JDBC code you have to develop
  • Hibernate provides the Object to Relational Mapping (ORM)
Object-To-Relational Mapping ()RM 
The developer defines mapping between Java class and database table 
Java Class 
int 
• String 
- lastName : 
• email String 
Database Table 
Hibernate 
-O VAACHAA(45) 
0 last_name VARCHAR(45) 
o email vAæHAR(4S)

Entity (Java) = Table (Sql)

Mapping can be done via configuration file (XML file) or Java Annotations

No need to write sql quires just call the special method (session) which is provided by hibernate.

“theID” is the primary key which session returns after saving the java object into db

Saving a Java Object with Hibernate 
// create Java object 
Student theStudent neu "Doe", "john@1uv2code.com"); 
// save it to database 
int theld (Integer) session. ; 
Hibernate will store the 
data into the database. 
SQL insert

While retrieving Hibernate will do all the low level work of doing the query, getting the actual data, constructing the object and returning it back to your program.

RetrieGng a Java ()bjeet "ith Ilibernate 
// create Java object 
Student theStudent new "Doe", "john@Iuv2code.com"); 
// save it to database 
int theld (Integer) session. save(theStudent); 
// now retrieve from database using the primary key 
Student mystudent session.get(student.class, field); 
Hibernate will query 
the table for given id

For getting all of the Students objects from the table we can make use of something called “Hibernate Query Language (HQL)“.

Querying for Java Objects 
Query query = session.createQuery("from Student"); 
students: query. list(); 
Retums a list of 
Student objects 
from the database

Hibernate and JDBC

Hibernate ORM is an object-relational mapping tool for the Java programming language. It provides a framework for mapping an object-oriented domain model to a relational database.

Hibernate relationship with JDBC (Java Database Connectivity)

  • Hibernate actually uses JDBC for all database communications
  • Hibernate is another layer of abstraction on top of  JDBC
  • So when your application uses Hibernate framework, your app will store and retrieve objects using hibernate api, in the background hibernate does all of the low level JDBC work and submitting sql queries and so on.
  • Hibernate does  a lot of low level work for you, but In the background it all goes to the standard JDBC Api.
  • When we config hibernate to talk to the Database we will actually configure to make use of JDBC driver to communicate to the database.
Hibernate 
Уош 
Java 
Арр 
ЛВС

Leave a comment