40
Session-02

Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

Embed Size (px)

Citation preview

Page 1: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

Session-02

Page 2: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

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

Page 3: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

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

Page 4: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

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.

Page 5: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

Continue...

Page 6: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

How to get Information about Sql Query running in background?

Page 7: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

Cfg File

Page 8: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

Example-1

Page 9: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

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.

Page 10: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

Continue...

Page 11: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

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.

Page 12: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

Continue...

Page 13: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

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.

Page 14: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

Continue...

Page 15: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

TransactionFactory

• It is a factory of Transaction. It is optional.

Page 16: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

Transaction

• The transaction object specifies the atomic unit of work. It is optional.

Page 17: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

Continue...

Page 18: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

ConnectionProvider

• It is a factory of JDBC connections. • It abstracts the application from DriverManager or DataSource.• It is optional.

Page 19: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

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.

Page 20: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

How to Create Table using Hibernate?

Page 21: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

Continue...

Page 22: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

Example-2

Page 23: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

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.

Page 24: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

Steps

Add Lib

Create cfg file

hibernate.cgf.xmlEntity POJO Class

POJO

Test Class

Run

Add JPA

JPA

Page 25: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

Continue...

Page 26: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

Continue...

Page 27: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

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

Page 28: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

Continue...

Page 29: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

Example-3

Page 30: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

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.

Page 31: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

Advantage of HQL

1. database independent

2. supports polymorphic queries

3. easy to learn for Java Programmer

Page 32: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

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.

Page 33: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

Example of HQL to get all the records

Page 34: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

Example-4

Page 35: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

Example of HQL to get records with pagination

Page 36: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

Example of HQL update query

Page 37: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

Example of HQL delete query

Page 38: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

HQL with Aggregate functions

Example to get total salary of all the employees

Page 39: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

Example to get maximum salary of employee

Page 40: Session-02. Steps Create Table Add Lib Create cfg file hibernate.cgf.xml Create reverse eng. file hibernate.reveng.xml Create hbm & POJO file from database

Summary!!