Upload
bennett-jefferson
View
217
Download
0
Embed Size (px)
Citation preview
Session-02
Steps
Create Table
Add Lib
Create cfg file
hibernate.cgf.xmlCreate reverse eng. file
hibernate.reveng.xmlCreate hbm & POJO file from database
POJO
hbmTest Class
Run
Test ClassCreate Configure class object
Call configure( )Return cfg
cfgCall buildSessionFactory( )
Return Session factory object
scCall openSession( )
Return Session object
sCall begin Transaction( )
Return Transaction object
sCall save ( ) Session object
emp object
txCall commit( ) Data Saved
into table
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.
Continue...
How to get Information about Sql Query running in background?
Cfg File
Example-1
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.
Continue...
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.
Continue...
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.
Continue...
TransactionFactory
• It is a factory of Transaction. It is optional.
Transaction
• The transaction object specifies the atomic unit of work. It is optional.
Continue...
ConnectionProvider
• It is a factory of JDBC connections. • It abstracts the application from DriverManager or DataSource.• It is optional.
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.
How to Create Table using Hibernate?
Continue...
Example-2
Hibernate with Annotation
• The hibernate application can be created with annotation.
• There are many annotations that can be used to create hibernate application
such as @Entity, @Id, @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.
Steps
Add Lib
Create cfg file
hibernate.cgf.xmlEntity POJO Class
POJO
Test Class
Run
Add JPA
JPA
Continue...
Continue...
Test ClassCreate Annotation Configure class object
Call configure( )Return Annotation (cfg)
cfgCall buildSessionFactory( )
Return Session factory object
scCall openSession( )
Return Session object
sCall begin Transaction( )
Return Transaction object
sCall save ( ) Session object
emp object
txCall commit( ) Data Saved
into table
Continue...
Example-3
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.
Advantage of HQL
1. database independent
2. supports polymorphic queries
3. easy to learn for Java Programmer
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.
Example of HQL to get all the records
Example-4
Example of HQL to get records with pagination
Example of HQL update query
Example of HQL delete query
HQL with Aggregate functions
Example to get total salary of all the employees
Example to get maximum salary of employee
Summary!!