Download presentation
Presentation is loading. Please wait.
1
Session-02
2
Steps hibernate.cgf.xml hibernate.reveng.xml Test Class POJO hbm
Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database POJO Run hbm Test Class
3
Session factory object
Test Class Call configure( ) Create Configure class object Return cfg Call buildSessionFactory( ) cfg Return Session factory object Call openSession( ) sc Return Session object Call begin Transaction( ) s Return Transaction object Call save ( ) Session object s emp object Call commit( ) Data Saved into table tx
4
hibernate.cfg.xml Hibernate requires to know in advance where to find the mapping information that defines how your Java classes relate to the database tables. Hibernate also requires a set of configuration settings related to database and other related parameters. All such information is usually supplied as a standard Java properties file called hibernate.properties, or as an XML file named hibernate.cfg.xml.
5
Continue...
6
How to get Information about Sql Query running in background?
7
Cfg File
8
Example-1
9
SessionFactory ? The SessionFactory is a factory of session and client of ConnectionProvider. The SessionFactory is an expensive object to create. It, like the Configuration object, is usually created during application start up. It should be created once and kept for later use. Generally, an application has single SessionFactory and can be shared by all the application threads. It holds second level cache (optional) of data.
10
Continue...
11
Session ? The session object provides an interface between the application and data stored in the database. A session wraps a JDBC connection. Think of the Session as a gateway to your database. The instances are light weighted and can be created and destroyed without expensive process. Hibernate Session object represents a single unit-of-work for a given data store and is opened by a SessionFactory instance. Java objects is stored in databases using Session object. It holds a first-level cache (mandatory) of data.
12
Continue...
13
Configuration ? It represents a configuration or properties file for Hibernate. The Configuration object is usually created once during application initialization. The Configuration object reads and establishes the properties Hibernate uses to get connected to a database and configure itself for work. A Configuration object is used to create a SessionFactory and then typically is discarded.
14
Continue...
15
TransactionFactory It is a factory of Transaction. It is optional.
16
Transaction The transaction object specifies the atomic unit of work. It is optional.
17
Continue...
18
ConnectionProvider It is a factory of JDBC connections.
It abstracts the application from DriverManager or DataSource. It is optional.
19
SQL Dialects in Hibernate
For connecting any hibernate application with the database, you must specify the SQL dialects. There are many Dialects classes defined for RDBMS in the org.hibernate.dialect package.
20
How to Create Table using Hibernate?
21
Continue...
22
Example-2
23
Hibernate with Annotation
The hibernate application can be created with annotation. There are many annotations that can be used to create hibernate application @Table etc. All the JPA annotations are defined in the javax.persistence.* package. Hibernate EntityManager implements the interfaces and life cycle defined by the JPA specification. The core advantage of using hibernate annotation is that you don't need to create mapping (hbm) file. Here, hibernate annotations are used to provide the meta data.
24
Steps hibernate.cgf.xml JPA Test Class POJO Add Lib Create cfg file
Entity POJO Class Add JPA POJO JPA Run Test Class
25
Continue...
26
Continue...
27
Session factory object
Test Class Call configure( ) Create Annotation Configure class object Return Annotation (cfg) Call buildSessionFactory( ) cfg Return Session factory object Call openSession( ) sc Return Session object Call begin Transaction( ) s Return Transaction object Call save ( ) Session object s emp object Call commit( ) Data Saved into table tx
28
Continue...
29
Example-3
30
Hibernate Query Language (HQL)
Hibernate Query Language (HQL) is same as SQL (Structured Query Language) but it doesn't depends on the table of the database. Instead of table name, we use class name in HQL. So it is database independent query language.
31
Advantage of HQL database independent supports polymorphic queries
easy to learn for Java Programmer
32
Query Interface It is an object oriented representation of Hibernate Query. The object of Query can be obtained by calling the createQuery() method Session interface.
33
Example of HQL to get all the records
34
Example-4
35
Example of HQL to get records with pagination
36
Example of HQL update query
37
Example of HQL delete query
38
HQL with Aggregate functions
Example to get total salary of all the employees
39
Example to get maximum salary of employee
40
Summary!!
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.