Hibernate

1) Introduction to JDBC
2) JDBC Architecture 
3) Interaction of JDBC with Database
4) Introduction to Hibernate 
5) Hibernate Architecture
6) Hibernate Communication with RDBMS 
7) Advantage of Hibernate over JDBC

Introduction to JDBC:    JDBC stands for Java Database Connectivity allows developers to connect, query and update 
a database using the Structured Query Language.

 

JDBC Architecture:

 Interaction with RDBMS
General steps:

1) Load the RDBMS specific JDBC driver because this driver actually communicates with the database.
 2) Open the connection to database which is then used to send SQL statements and get results back.
 3) Create JDBC Statement object. This object contains SQL query.

4) Execute statement which returns resultset(s).

5) ResultSet contains the tuples of database table as a result of SQL query.
6)Process the result set.
7)Close the connection.
Example: Retrieve list of employees from Employee table using JDBC.

String url = “jdbc:odbc:” + dbName;
List<Employee> employeeList = new ArrayList<Employee>();
class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Connection con = DriverManager.getConnection(url);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM Employee");
while ( rs.next() )
{
Employee eb = new Employee();
eb.setName(rs.getString("name"));
eb.setSalary(rs.getFloat("salary"));
employeeList.add(eb);
}
con.close();

Problems with JDBC:

 

1)Have you ever feel irritated by repeating the same lines of code over and over again in your application for fetching data from the database?
2) Are you struggling to map objects to your database tables?
3) Is it difficult for you to implement oops with your JDBC code?
4) Does it takes too much of rework while migrating from one database to another?
5) Have you ever found it difficult to create associations between tables using JDBC?
6) database independent Problem
7) caching mechanism Problem
8) we handle All checked exceptions in try catch

9)Getting pagination in hibernate is quite simple.
10)Jdbc does not supports Inheritance, Associations, Collections
11) JDBC does not supports relationships like One-To-Many,One-To-One, Many-To-Many-to-Many, Many-To-One
12)Jdbc does not support Automatic Table and schema generations in database
13) Jdbc does not Concurrency Support

What is Hibernate?

Hibernate is a free open source object-relational mapping library for Java which is designed to map objects to an RDBMS and to implement the object-oriented programming concepts in a relational database.
Hibernate Workflow=>
 Hibernate communication with RDBMS
General steps:

1) Load the Hibernate configuration/properties 2) ile and create configuration object.
3) Create session factory from configuration object
4) Get one session from this session factory.

One Time things--------

5) Create HQL query.
6) Execute query to get list containing Java objects.
Example: Retrieve list of employees from Employee table using Hibernate.
         Hibernate

Session session = sessionFactory.openSession();
Query query = session.createQuery("from Employee”);
List<Employee> finalList = query.list();

      JPA
List<Employee> findAll();

​
                                 How to Connect with Database in hibernate.

spring.datasource.url=jdbc:mysql://localhost:3306/demo
spring.datasource.username=root
spring.datasource.password=xxx

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=create-drop/update/create
spring.jpa.show-sql=true
logging.level.org.hibernate.SQL=DEBUG

Core Annotations

@Entity
@Table
@Column
@Id
@GeneratedValue

@one to one

@one to many

@Many to many

Hibernate

By Sagar Mal Shankhala