session-02. steps create table add lib create cfg file hibernate.cgf.xml create reverse eng. file...
TRANSCRIPT
![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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/1.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/2.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/3.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/4.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/5.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/6.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/7.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/8.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/9.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/10.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/11.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/12.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/13.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/14.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/15.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/16.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/17.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/18.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/19.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/20.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/21.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/22.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/23.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/24.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/25.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/26.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/27.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/28.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/29.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/30.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/31.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/32.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/33.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/34.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/35.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/36.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/37.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/38.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/39.jpg)
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](https://reader037.vdocuments.us/reader037/viewer/2022103123/56649d8b5503460f94a72253/html5/thumbnails/40.jpg)
Summary!!