0% found this document useful (0 votes)
6 views

Module 6 Hibernate_Share

Uploaded by

parmar2100parmar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Module 6 Hibernate_Share

Uploaded by

parmar2100parmar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Hibernate

PMCA502L: Thilagavathi M, AP(Sr.), SCORE


.

Hibernate
 Hibernate is a Java framework that simplifies the development of Java
application to interact with the database.
 It is an open source, lightweight, ORM (Object Relational Mapping) tool.
 Hibernate implements the specifications of JPA (Java Persistence API) for
data persistence.
 ORM Tool - An ORM tool simplifies the data creation, data manipulation
and data access. It is a programming technique that maps the object to
the data stored in the database.

PMCA502L: Thilagavathi M, AP(Sr.), SCORE


Hibernate
 Advantages
 Open Source and Lightweight
 Fast Performance
 Database Independent Query (HQL)
 Automatic Table Creation
 Simplifies Complex Join
 Provides Query Statistics and Database Status
.

Core Objects of Hibernate Framework

The Hibernate architecture is


categorized in four layers.
 Java application layer
 Hibernate framework layer
 Backhand API layer
 Database layer

PMCA502L: Thilagavathi M, AP(Sr.), SCORE


.

Core Objects of Hibernate Framework


 SessionFactory
 The SessionFactory is a factory of session and client of ConnectionProvider. It holds second
level cache (optional) of data. The org.hibernate.SessionFactory interface provides factory
method to get the object of Session.

 Session
 The session object provides an interface between the application and data stored in the
database. It is a short-lived object and wraps the JDBC connection. It is factory of
Transaction, Query and Criteria. It holds a first-level cache (mandatory) of data. The
org.hibernate.Session interface provides methods to insert, update and delete the object.
It also provides factory methods for Transaction, Query and Criteria.

 Transaction
 The transaction object specifies the atomic unit of work. It is optional. The
org.hibernate.Transaction interface provides methods for transaction management.

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

 TransactionFactory
 It is a factory of Transaction. It is optional. PMCA502L: Thilagavathi M, AP(Sr.), SCORE
Internal API used by Hibernate
 JNDI (Java Naming and Directory Interface) is a Java API that provides naming
and directory functionality to applications written in Java. It allows Java
software clients to discover and look up data and objects via a name.
 JDBC is Java Database Connectivity API.
 The main thing here is that in a JNDI directory you're actually storing a JDBC
DataSource, so, you're simply using JDBC to obtain a Connection via JNDI
lookup.
 The Java Transaction API (JTA) allows applications to perform distributed
transactions, that is, transactions that access and update data on two or more
networked computer resources. The JTA specifies standard Java interfaces
between a transaction manager and the parties involved in a distributed
transaction system: the application, the application server, and the resource
manager that controls access to the shared resources affected by the
transactions.

PMCA502L: Thilagavathi M, AP(Sr.), SCORE


Session Interface Methods
Return Type Method
Void persist(Object object)
Make a transient instance persistent.
Serializable save(Object object)
Persist the given transient instance, first assigning a generated identifier.
void saveOrUpdate(Object object)
Either save(Object) or update(Object) the given instance, depending upon
resolution of the unsaved-value checks.
void update(Object object)
Update the persistent instance with the identifier of the given detached
instance.
void delete(Object object)
Remove a persistent instance from the datastore.
SQLQuery createSQLQuery(String queryString)
Create a new instance of SQLQuery for the given SQL query string.
Transaction beginTransaction()
Begin a unit of work and return the associated Transaction object.
PMCA502L: Thilagavathi M, AP(Sr.), SCORE
Hibernate Implements JPA Specification
Mapping Annotations-Domain Model
 Historically applications using Hibernate would have used its proprietary XML mapping
file format for this purpose. With Jakarta Persistence, most of this information is now
defined in a way that is portable across ORM/Jakarta Persistence providers using
annotations (and/or standardized XML format).
 A simple table and domain model JAKARTA
PERSISTENCE
 Table Domain Model
Mapping Annotations-Domain Model
 A Java class can be easily transformed into an entity. For transformation the basic requirements
are: -
 No-argument Constructor
 Annotation
@Entity - This is a marker annotation which indicates that this class is an entity. This annotation
must be placed on the class name.
@Id - This annotation is placed on a specific field that holds the persistent identifying properties.
This field is treated as a primary key in database.
 @Table maps the class to a table in the database.
 @GeneratedValue annotation, the name itself suggests that it will generate something. This
annotation is generally used in conjunction with @Id annotation to automatically generate unique
values for primary key columns within our database tables. When creating an entity class we have
to specify a primary key for that entity. For marking the field property as a primary key of the
entity we use @Id annotation. When we apply @GeneratedValue annotation to our primary key field
or property. It will instruct hibernate to automatically generate a unique value for that field during
the process of persisting the entity into the database. The @GeneratedValue annotation provides us
with different strategies for the generation of primary keys. We will the use following one.
 GenerationType.IDENTITY: This strategy will help us to generate the primary key value by the
database itself using the auto-increment column option. It relies on the database’s native support
for generating unique values.
 @Column – maps the instance variable with a column attribute in the table.

PMCA502L: Thilagavathi M, AP(Sr.), SCORE


Console based Hibernate Application
 Create a Maven Project (Refer Project titled ‘Hibernate Maven Project’)
 Add below dependencies in pom.xml
Console based Hibernate Application

 Required four packages for managing


 pojo classes- com.vit.hibernate.entity.Student
 dao classes - com.vit.hibernate.dao.StudentDao
 Util class - com.vit.hibernate.util.HibernateUtil
 Driver class-com.vit.hibernate.App
HQL
 DML, or Data Manipulation Language, refers to SQL statements such as INSERT, UPDATE,
and DELETE. Hibernate provides methods for bulk SQL-style DML statement execution,
in the form of Hibernate Query Language (HQL).
 Pseudo-syntax for UPDATE and DELETE statements using HQL
 UPDATE FROM EntityName e WHERE e.name = ?
 DELETE FROM EntityName e WHERE e.name = ?
 The FROM clause can only refer to a single entity, which can be aliased.
 Executing an HQL UPDATE, using the Query.executeUpdate()
Hibernate Transactions
 Generally transactions,
 might refer to the physical transaction with the database.
 might refer to the logical notion of a transaction as related to a persistence context.
 might refer to the application notion of a Unit-of-Work, as defined by the archetypal pattern.
 Hibernate supports both mechanisms for integrating with transactions and allowing
applications to manage physical transactions.
 JDBC itself and JTA.
 Hibernate uses JDBC connections and JTA resources directly, without adding any
additional locking behavior. Hibernate does not lock objects in memory. The behavior
defined by the isolation level of your database transactions does not change when you
use Hibernate.
 The Hibernate Session acts as a transaction-scoped cache providing repeatable reads
for lookup by identifier and queries that result in loading entities.

14
Hibernate – Web Application
Create a Maven Project (Refer Project titled ‘mavenproject’)
ControllerServlet

Index.jsp

15
Hibernate – Web Application
CourseDao
PoJO class : Course
Hibernate – Web Application
Util Class
Hibernate – Web Application Output

You might also like