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

Session 04 Java Persistence, Hibernate, Spring Data

This document provides an overview of Java Persistence API (JPA), Hibernate, and Spring Data, focusing on their components, architecture, and functionalities. It covers key concepts such as EntityManager, ORM, JPQL, and caching mechanisms in Hibernate, as well as the advantages of using these technologies for database interaction in Java applications. Additionally, it highlights the features of Spring Data JPA that simplify repository management and data access.

Uploaded by

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

Session 04 Java Persistence, Hibernate, Spring Data

This document provides an overview of Java Persistence API (JPA), Hibernate, and Spring Data, focusing on their components, architecture, and functionalities. It covers key concepts such as EntityManager, ORM, JPQL, and caching mechanisms in Hibernate, as well as the advantages of using these technologies for database interaction in Java applications. Additionally, it highlights the features of Spring Data JPA that simplify repository management and data access.

Uploaded by

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

Session 04

ANTEA TRAINING
Java Persistence, Hibernate, Spring Data
Session 04
• Summary
• Java Persistence, Entity Manager

• Hibernate, entities mapping, the caches, HQL

• Spring Data : Repositories, Transactions.


Session 04

Java Persistence API


Session 04
Java Persistence API
• What is Java Persistence API?

• JavaPersistence API or JPA is a collection of classes and


methods to persistently store and manipulate database
data.
• JPAis an open source API, therefore various enterprises and
communities provide products by adding some features to the
JPA persistence API:
• Hibernate, Eclipselink, Spring Data JPA, etc.
Java Persistence API
• How Java Persistence API works?
Java Persistence API
• Java Persistence Architecture
Java Persistence API
• Java Persistence Architecture

EntityManagerFactory This is a factory class of EntityManager. It creates and


manages multiple EntityManager instances.

EntityManager It is an Interface, it manages the persistence operations


on objects. It works like factory for Query instance.

Entity Entities are the persistence objects, stores as records in


the database.

EntityTransaction It has one-to-one relationship with EntityManager. For


each EntityManager, operations are maintained by
EntityTransaction class.
Persistence This class contain static methods to obtain
EntityManagerFactory instance.

Query This interface is implemented by each JPA vendor to


obtain relational objects that meet the criteria.
Java Persistence API
• Java Persistence Class Relationships
Java Persistence API
• Java Persistence Class Relationships

• The relationship between EntityManagerFactory and


EntityManager is one-to-many. It is a factory class to
EntityManager instances.
• The relationship between EntityManager and
EntityTransaction is one-to-one. For each EntityManager
operation, there is an EntityTransaction instance.
• The relationship between EntityManager and Query is one-
to-many. Many number of queries can execute using one
EntityManager instance.
• The relationship between EntityManager and Entity is one-
to-many. One EntityManager instance can manage multiple
Entities.
Java Persistence API
• Java Persistence ORM Components

• Object Relational Mapping (ORM) briefly tells you about


what is ORM and how it works.
• ORM is a programming ability to covert data from object type
to relational type and vice versa.
• The main feature of ORM is mapping or binding an object to
its data in the database.
• While mapping we have to consider the data, type of data
and its relations with its self-entity or entity in any other
table.
Java Persistence API
• Java Persistence ORM Components
Java Persistence API
• Java Persistence ORM Components

• JPA Provider : The vendor product which contains JPA flavor


(javax.persistence). For example Spring Data, Hibernate, etc.
• Mapping file : The mapping file (ORM.xml) contains mapping
configuration between the data in a POJO class and data in a
relational database.
• JPALoader : The JPA loader works like cache memory, which
can load the relational grid data.
• ObjectGrid : The Object grid is a temporary location which
can store the copy of relational data, i.e. like a cache
memory.
Java Persistence API
• Java Persistence Example
Java Persistence API
• Java Persistence Example
Java Persistence API
• Java Persistence Example
• <entity-mappings> : tag defines the schema definition to allow
entity tags into xml file.
• <entity> : tag defines the entity class which you want to convert into
table in a database. Attribute class defines the POJO entity class
name.
• <table> : tag defines the table name. If you want to keep class name
as table name then this tag is not necessary.
• <attributes> : tag defines the attributes (fields in a table).
• <id> : tag defines the primary key of the table. The <generated-
value> tag defines how to assign the primary key value such as
Automatic, Manual, or taken from Sequence.
• <basic> : tag is used for defining remaining attributes for table.
• <column-name> : tag is used to define user defined table field name.
Java Persistence API
• Java Persistence Annotations

• @Entity This annotation specifies to declare the class as entity or


a table.
• @Table This annotation specifies to declare table name.
• @Basic This annotation specifies non constraint fields explicitly.
• @Id This annotation specifies the property, use for identity
(primary key of a table) of the class.
• @GeneratedValue This annotation specifies, how the identity
attribute can be initialized such as Automatic, manual, or value
taken from sequence table.
• @Transient This annotation specifies the property which in not
persistent i.e. the value is never stored into database.
Java Persistence API
• Java Persistence Annotations

• @ManyToMany This annotation is used to define a many-to-


many relationship between the join Tables.
• @ManyToOneThis annotation is used to define a many-to-one
relationship between the join Tables.
• @OneToManyThis annotation is used to define a one-to-many
relationship between the join Tables.
• @OneToOne This annotation is used to define a one-to-one
relationship between the join Tables.
• @NamedQuery This annotation is used for specifying a Query
using static name.
• @NamedQueries This annotation is used for specifying list of
named queries.
Java Persistence API
• Java Persistence Example
Java Persistence API
• Java Persistence Example
Java Persistence API
• Java Persistence Example
Java Persistence API
• Java Persistence Example
Java Persistence API
• Java Persistence Example
Java Persistence API
• Java Persistence Query language

• JPQL is Java Persistence Query Language defined in JPA


specification.
• Itis used to create queries against entities to store in a
relational database.
• JPQLis developed based on SQL syntax. But it won’t affect
the database directly.
• JPQLcan retrieve information or data using SELECT clause,
can do bulk updates using UPDATE clause and DELETE clause.
• EntityManager.createQuery() API will support for querying
language.
Java Persistence API
• Java Persistence Query language

• JPQL syntax is very similar to the syntax of SQL.

• SQL works directly against relational database tables, records


and fields, whereas JPQL works with Java classes and
instances.
Java Persistence API
• JPQL Scalar and Aggregate Functions
Java Persistence API
• JPQL Between, And, Like Keywords
Java Persistence API
• JPQL Ordering
Java Persistence API
• JPQL Named Queries
Java Persistence API
• JPQL Named Queries
Java Persistence API
• JPQL Entity Relationships

• Many-To-One relation between entities: Where one entity


(column or set of columns) is/are referenced with another
entity (column or set of columns) which contain unique
values.
• In relational databases these relations are applicable by using
foreign key/primary key between tables.
Java Persistence API
• JPQL Entity Relationships : Many-To-One
Java Persistence API
• JPQL Entity Relationships

• OneToMany relation between entities: each row of one


entity is referenced to many child records in other entity. The
important thing is that child records cannot have multiple
parents
• In relational databases these relations are applicable by using
foreign key/primary key between tables.
Java Persistence API
• JPQL Entity Relationships : OneToMany
Java Persistence API
• JPQL Entity Relationships

• In
One-To-One relationship, one item can belong to only one
other item.
• It
means each row of one entity is referred to one and only
one row of another entity.
Java Persistence API
• JPQL Entity Relationships One-To-One
Java Persistence API
• JPQL Entity Relationships

• Many-To-Many relationship is where one or more rows from


one entity are associated with more than one row in other
entity.
Java Persistence API
• JPQL Entity Relationships Many-To-Many
Java Persistence API
• JPQL Entity Relationships Many-To-Many
Session 04

Hibernate Framework
Session 04
Hibernate Framework
• Hibernate Framework Introduction

• 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.
Hibernate Framework
• Advantages of Hibernate Framework

1) Open Source and Lightweight


2) Fast Performance
3) Database Independent Query HQL
4) Automatic Table Creation
5) Simplifies Complex Join
6) Provides Query Statistics and Database Status
Hibernate Framework
• JPA vs. Hibernate

Java Persistence API (JPA) defines the management of Hibernate is an Object-Relational Mapping (ORM) tool
relational data in the Java applications. which is used to save the state of Java object into the
database.
It is just a specification. Various ORM tools implement It is one of the most frequently used JPA
it for data persistence. implementation.

It is defined in javax.persistence package. It is defined in org.hibernate package.

The EntityManagerFactory interface is used to It uses SessionFactory interface to create Session


interact with the entity manager factory for the instances.
persistence unit. Thus, it provides an entity manager.
It uses EntityManager interface to create, read, and It uses Session interface to create, read, and delete
delete operations for instances of mapped entity operations for instances of mapped entity classes. It
classes. This interface interacts with the persistence behaves as a runtime interface between a Java
context. application and Hibernate.
It uses Java Persistence Query Language (JPQL) as an It uses Hibernate Query Language (HQL) as an object-
object-oriented query language to perform database oriented query language to perform database
operations. operations.
Hibernate Framework
• Hibernate Architecture
• Java application layer
• Hibernate framework layer
• Backhand api layer
• Database layer
Hibernate Framework
• Hibernate Architecture
Hibernate Framework
• Hibernate Architecture

The SessionFactory is a factory of session and client of


ConnectionProvider. It holds second level cache (optional) of data.
SessionFactory The org.hibernate.SessionFactory interface provides factory
method to get the object of Session.
The session object provides an interface between the application
and data stored in the database. It is a short-lived object and wraps
Session the JDBC connection. It is factory of Transaction, Query and
Criteria.
The transaction object specifies the atomic unit of work. It is
optional. The org.hibernate.Transaction interface provides
Transaction methods for transaction management.

It is a factory of JDBC connections. It abstracts the application from


DriverManager or DataSource. It is optional.
ConnectionProvider

It is a factory of Transaction. It is optional.


TransactionFactory
Hibernate Framework
• Hibernate Example Application
Hibernate Framework
• Hibernate Example Application
Hibernate Framework
• Hibernate Example Application
Hibernate Framework
• Caching in Hibernate
• Hibernate caching improves the performance of the
application by pooling the object in the cache.
• Itis useful when we have to fetch the same data multiple
times.
• There are mainly two types of caching:
• First Level Cache, and
• Second Level Cache
Hibernate Framework
• Caching in Hibernate: First Level Cache
• Session object holds the first level cache data. It is enabled by
default.
• The first level cache data will not be available to entire
application. An application can use many session object.
Hibernate Framework
• Caching in Hibernate: Second Level Cache
• SessionFactory object holds the second level cache data. The
data stored in the second level cache will be available to
entire application.
• Differentvendors have provided the implementation of
Second Level Cache :
• EH Cache
• Swarm Cache
• OS Cache
• JBoss Cache
Hibernate Framework
• Caching in Hibernate: Second Level Cache
• Each implementation provides different cache usage
functionality. There are four ways to use second level cache.
• read-only: caching will work for read only operation.
• nonstrict-read-write: caching will work for read and write but
one at a time.
• read-write: caching will work for read and write, can be used
simultaneously.
• transactional: caching will work for transaction.
Hibernate Framework
• Caching in Hibernate: Second Level Cache Example
Hibernate Framework
• Caching in Hibernate: Second Level Cache Example
Hibernate Framework
• Caching in Hibernate: Second Level Cache Example
Hibernate Framework
• 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.
• HQL isa superset of the JPQL, the Java Persistence Query
Language.
• A JPQL query is a valid HQL query, but not all HQL queries are
valid JPQL queries.
Hibernate Framework
• Hibernate Query Language : HQL vs JPQL
Session 04

Spring Data JPA


Session 04
Spring Data JPA
• Spring Data Introduction
• SpringData JPA API provides JpaTemplate class to integrate
spring application with JPA.
• You don't need to write the before and after code for
persisting, updating, deleting or searching object.
• So, it save a lot of code.
Spring Data JPA
• Spring Data Features
• Sophisticated support to build repositories based on Spring
and JPA
• Support for Querydsl predicates and thus type-safe JPA
queries
• Transparent auditing of domain class
• Pagination support, dynamic query execution, ability to
integrate custom data access code
• Validation of @Query annotated queries at bootstrap time
• Support for XML based entity mapping
• JavaConfig
based repository configuration by introducing
@EnableJpaRepositories.
Spring Data JPA
• Spring Data Spring Boot Example
Spring Data JPA
• Spring Data Spring Boot Example
Spring Data JPA
• Spring Data Spring Boot Example
Spring Data JPA
• Spring Data Spring Boot Example
Spring Data JPA
• Spring Data Spring Boot Example
Responsible Name of the document manager
File name Session 04 Java Persistence, Hibernate, Spring Data.pptx
Revisions
R01 Data rev. Farouk Korteby Creation of the document

This document is © 2021 Antea s.r.l.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy