SlideShare a Scribd company logo
Introduction to JPA and Hibernate
Held as part of the lecture series on
Web Engineering at Vienna University of Technology
May 2014
Business Informatics Group
Institute of Software Technology and Interactive Systems 

Vienna University of Technology
Favoritenstraße 9-11/188-3, 1040 Vienna, Austria

phone: +43 (1) 58801-18804 (secretary), fax: +43 (1) 58801-18896

office@big.tuwien.ac.at, www.big.tuwien.ac.at
Web Engineering
Introduction to JPA and Hibernate
Philipp Liegl
Outline of today’s talk
▪ JDBC
!
▪ JPA/Hibernate
▪ Relationships
▪ Persistence Context/Persistence Unit
▪ Entity Manager
▪ JPQL
▪ Hibernate Criteria API
!
Accompanying examples
https://github.com/pliegl/we2014/tree/master/jpa-sample
2
Motivation
3
N-Tier Architectures
▪ Layers of an information system
▪ Presentation layer
▪ Communication interface to external entities
▪ “View” in the model-view-controller
▪ Application logic layer (service layer)
▪ Implements operations requested by clients

through the presentation layer
▪ Represents the “business logic”
▪ Resource management layer 

(persistence layer)
▪ Deals with different data sources of an

information system
▪ Responsible for storing and retrieving data
presentation layer
application logic
layer
resource
management layer
client
TUWIENWEInformationSystem
Motivation
4
Accompanying model
Motivation
5
Traditional persistence with JDBC
presentation layer
service layer
persistence layer
JDBC
Database
DTO
DTO
Motivation
6
JDBC - Java Database Connectivity
▪ Used to access relational databases from Java programs
▪ First version released 1996
▪ Ability to
▪ Establish a connection to a database
▪ Execute an SQL statement and return results
▪ Create parameterized queries
▪ Manage database transactions
▪ Basic Steps
▪ Load driver or obtain an already defined data source
▪ Establish connection using a JDBC URL
▪ Create an SQL statement and execute SQL statement
▪ If present, process results present in result sets
▪ Close database resources
▪ Commit or rollback transaction, if necessary
http://www.oracle.com/technetwork/java/overview-141217.html
JDBC example
7
Insert an entry
Connection conn = null;
PreparedStatement stmt = null;
!
try {
conn = connection();
stmt = conn.prepareStatement( "INSERT INTO student VALUES(?, ?, ?)" );
stmt.setInt( 1, student.getId() );
stmt.setString( 2, student.getMatrNr() );
stmt.setString( 3, student.getName() );
stmt.executeUpdate();
stmt.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
}
JDBC example
8
Retrieve an entry
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = connection();
stmt = conn.prepareStatement( "SELECT id, matrnr, name FROM student 

WHERE id=?" );
stmt.setInt( 1, id );
rs = stmt.executeQuery();
rs.next();
!
Student student = new Student();
student.setId( rs.getInt( 1 ) );
student.setMatrNr( rs.getString( 2 ) );
student.setName( rs.getString( 3 ) );
rs.close();
stmt.close();
return student;
} catch (Exception e) {
e.printStackTrace();
} finally {
…
}
JDBC
9
Drawbacks
▪ Verbose JDBC boilerplate code for the various CRUD actions
▪ Manual mapping of JDBC result sets to the respective Java POJOs
▪ Imagine 40 different database tables with 20 attributes each…
▪ Manual synchronization of Java code in case of database schema
changes (e.g., a new field is added to a database table)
▪ Manual adaptation of the entire related JDBC Java code necessary
!
Object Relational Mapping
10
Reasons for using ORM
▪ In an application we want to focus on business concepts, not on the
relational database structure
▪ Abstract from the “by-hand” communication with the DB (e.g., via JDBC)
▪ Allow for an automatic synchronization between Java Objects and the
underlying database
▪ Portability
▪ ORM should be mostly DB independent (with the exception of some
types of features, such as identifier generation)
▪ Query abstractions using e.g. JPQL or HQL - the vendor specific
SQL is auto-generated
▪ Performance
▪ Object and query caching is automatically done by the ORM
Java Persistence API (JPA)
11
Introduction
▪ Specification for the management of persistence and object/relational mapping
with Java
▪ Persistence: Data objects shall outlive the JVM app
▪ Objective: provide an object/relational mapping facility for Java developers using
a Java domain model and a relational database
▪ Map Java POJOs to relational databases (which are one type of persistence)
▪ Standardized under the Java Community Process Program with contributions
from Hibernate, TopLink, JDO, and the EJB community
▪ Hibernate: Full JPA implementation with additional “native” features, e.g.,
▪ HQL (Hibernate Query Language) - similar to JPQL, but with some
extensions
▪ Criteria API
▪ Used version in this course: Hibernate 4.2.12.Final (supports JPA 2.0)
Caveats
12
ORM and JPA
▪ With JPA/Hibernate lots of “magic” is done under the hood, e.g., SQL-
DDL is automatically generated
▪ Know the database basics first (e.g., from a data engineering course), in
order to fully understand what JPA is doing under the hood
▪ After annotating the classes and running the application check the
resulting SQL-DDL (e.g., using the database explorer in IntelliJ or
Eclipse)
▪ When executing SQL-Queries using JPA/Hibernate use the “show SQL
queries” feature during development, in order to see what kind of
queries are actually executed
▪ Set <property name="hibernate.show_sql" value="true" />
in persistence.xml
Persistent Entities
13
Basics
▪ Are POJOs (Plain Old Java Objects)
▪ Lightweight persistent domain object
▪ Typically represent a table in a relational database
▪ Each entity instance corresponds to one row in that table
▪ Have a persistent identity
▪ May have both, persistent and transient (non-persistent) state
▪ Simple types (primitive data types, wrappers, enums)
▪ Composite types (e.g., Address)
▪ Non-persistent state (using identifier transient or @Transient
annotation)
Persistence with Hibernate
14
presentation layer
service layer
persistence layer
Database
DomainObjects
Cache
Hibernate
Simple Mapping
15
Enhance Java domain classes with JPA annotations
@Entity
public class ExamResult {
!
@Id
private Long id;
!
@Column(name = "prufungsDatum")
@Temporal(TemporalType.DATE)
private Date examDate;
!
private int mark;
!
@Transient
private String examLocation;
…
}
id pruefungsDatum mark
ExamResult
Important annotations
@Entity Specifies that the class is an entity
@Id Specifies the primary key of an entity
@Temporal Must be specified for fields of type
java.util.Date and java.util.Calendar
@TemporalType Type used to indicate a specific
mapping of java.util.Date or
java.util.Calendar. Allowed values:
- DATE
- TIME
- TIMESTAMP
@Transient Specifies that the field is not
persistent
Simple Mapping
16
Inheritance
Simple Mapping
17
Inheritance
@Entity
public class ExamResult extends BaseEntity {
!
@Column(name = "prufungsDatum")
@Temporal(TemporalType.DATE)
private Date examDate;
!
private int mark;
!
@Transient
private String examLocation;
!
//Getter and setters omitted
!
}
id pruefungsDatum mark
ExamResult
@MappedSuperclass
public class BaseEntity {
!
@Id
@GeneratedValue(

strategy = 

GenerationType.AUTO)
protected Long id;
!
public Long getId() {
return id;
}
}
Important annotations
@MappedSuperclass Designates a class whose mapping
information is applied to the entities that
inherit from it. A mapped superclass has no
separate table defined for it.
@GeneratedValue The GeneratedValue annotation may be
applied to a primary key property or field of an
entity or mapped superclass in conjunction
with the Id annotation.
Object-oriented vs. SQL
18
public class Student extends BaseEntity {
!
private String registrationNumber;
private String name;
private List<ExamResult> examResults;
…
}
public class ExamResult extends BaseEntity {
!
private Date examDate;
private String exam;
private int mark;
private Student student;
…
}
OO: Student owns the ExamResults

Usually: no ExamResult without a student
SQL:
• ExamResult contains a foreign key to the Student it belongs to
• The ExamResult owns (contains) the connection
• This is opposite to the OO perspective
Does not exist in the
DB, but is simulated
using an SQL query.
JPA takes care of
that.
Entity relationships
19
One-to-one, one-to-many, many-to-many, many-to-one relationships
among entities
• bi-directional or uni-directional
• Support for different Collection types, e.g., List, Set, Map, etc.
!
Need to specify the owning side in relationships
• Owning side table has the foreign key
• OneToOne relationship - the side where the foreign key is specified
• OneToMany, ManyToOne - the “many” side
Relationship mapping
20
Example using a unidirectional mapping
Four different options:
1. Using an embedded table, where Scholarship is the embedded table. (see example)
2. Scholarship and Student are separate tables. The primary key of Scholarship has a foreign
key constraint on the primary key of the “owning” Student (using @PrimaryKeyJoinColumn
annotation)
3. Scholarship and Student are separate tables. Student holds a foreign key which references
the primary key of Scholarship. The foreign key has a unique constraint.
4. Scholarship and Student are separate tables. Scholarship holds a foreign key which
references the primary key of Student. The foreign key has a unique constraint. (see
example)
Relationship mapping
21
Unidirectional OneToOne using an embedded table
@Entity
public class EmbeddedStudent extends BaseEntity {
!
@Column(name = "matrikelNummer", unique = true)
private String registrationNumber;
!
private String name;
!
@Embedded
private EmbeddedScholarship scholarship;
!
@Transient
private DateTime loginTime;
!
…
}
@Embeddable
public class EmbeddedScholarship {
!
private String description;
!
private Integer amount;
}
!
Important annotations
@Embedded Defines a persistent field
or property of an entity
whose value is an
instance of an
embeddable class. The
embeddable class must
be annotated as
Embeddable.
@Embeddable Defines a class whose
instances are stored as
an intrinsic part of an
owning entity and share
the identity of the entity.
Each of the persistent
properties or fields of the
embedded object is
mapped to the database
table for the entity.
Might be an issue with legacy databases, where the
DB schema already exists and must not be altered.
Relationship mapping
22
Unidirectional OneToOne using an embedded table - resulting SQL DDL
@Entity
public class EmbeddedStudent extends BaseEntity {
!
@Column(name = "matrikelNummer", unique = true)
private String registrationNumber;
!
private String name;
!
@Embedded
private EmbeddedScholarship scholarship;
!
@Transient
private DateTime loginTime;
!
…
}
@Embeddable
public class EmbeddedScholarship {
!
private String description;
!
private Integer amount;
}
!
CREATE TABLE EMBEDDEDSTUDENT
(
ID BIGINT PRIMARY KEY NOT NULL,
NAME VARCHAR(255),
MATRIKELNUMMER VARCHAR(255),
AMOUNT INTEGER,
DESCRIPTION VARCHAR(255)
);
CREATE UNIQUE INDEX anIndexName
ON EMBEDDEDSTUDENT ( MATRIKELNUMMER );
Relationship mapping
23
Bidirectional OneToOne using foreign key
@Entity
public class Student extends BaseEntity {
!
@Column(name = "matrikelNummer", 

unique = true)
private String registrationNumber;
!
private String name;
!
@OneToOne(fetch = FetchType.LAZY, 

cascade = CascadeType.ALL,

mappedBy="grantedTo")
private Scholarship scholarship;
!
@Transient
private DateTime loginTime;
…
}
Important annotations
@OneToOne Defines a single-valued association
to another entity that has one-to-
one multiplicity.
@FetchType LAZY = do not load referenced
entity, until it is accessed for the
first time
EAGER = load referenced entity
immediately
@CascadeType Defines the set of cascadable
operations that are propagated to
the associated entity. ALL is
equivalent to cascade={PERSIST,
MERGE, REMOVE, REFRESH,
DETACH}
mappedBy References the field that “owns”
the relationship in the referenced
entity. Required unless the
relationship is unidirectional.
The “non-owning” side
Relationship mapping
24
Bidirectional OneToOne using foreign key cont’d
@Entity
public class Scholarship extends BaseEntity {
!
private String description;
!
private Integer amount;
!
@JoinColumn(name=“student_id”, unique=true)
@OneToOne
private Student grantedTo;
!
…
}
The “owning” side
Important annotations
@JoinColumn Specifies a column for joining an
entity association or element
collection. 

In this case: the name of the
column, where the foreign key will
be stored.
Relationship mapping
25
Bidirectional OneToOne using foreign key - resulting SQL DDL
CREATE TABLE STUDENT
(
ID BIGINT PRIMARY KEY NOT NULL,
NAME VARCHAR(255),
MATRIKELNUMMER VARCHAR(255)
);
CREATE UNIQUE INDEX anIndexName
ON STUDENT ( MATRIKELNUMMER );
CREATE TABLE SCHOLARSHIP
(
ID BIGINT PRIMARY KEY NOT NULL,
AMOUNT INTEGER,
DESCRIPTION VARCHAR(255),
STUDENT_ID BIGINT,
FOREIGN KEY ( STUDENT_ID ) 

REFERENCES STUDENT ( ID )
);
CREATE UNIQUE INDEX uniqueIndexName 

ON SCHOLARSHIP ( STUDENT_ID );
Relationship mapping
26
Bidirectional OneToMany
@Entity
public class Student extends BaseEntity {
!
@Column(name = "matrikelNummer", unique = true)
private String registrationNumber;
!
private String name;
!
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, 

mappedBy = "grantedTo")
private Scholarship scholarship;
!
@OneToMany(cascade = CascadeType.ALL, mappedBy = "student")
private List<ExamResult> examResults;
!
@Transient
private DateTime loginTime;
…
}
Important annotations
@OneToMany Defines a many-valued association
with one-to-many multiplicity.
The “non-owning” side
Relationship mapping
27
Bidirectional OneToMany cont’d
@Entity
public class ExamResult extends BaseEntity {
!
@Column(name = "prufungsDatum")
@Temporal(TemporalType.DATE)
private Date examDate;
!
private String exam;
!
private int mark;
!
@ManyToOne
private Student student;
!
!
@Transient
private String examLocation;
…
}
Important annotations
@ManyToOne Defines a single-valued association
to another entity class that has
many-to-one multiplicity.
The “owning” side
Relationship mapping
28
Bidirectional OneToMany - resulting SQL DDL
CREATE TABLE STUDENT
(
ID BIGINT PRIMARY KEY NOT NULL,
NAME VARCHAR(255),
MATRIKELNUMMER VARCHAR(255)
);
CREATE UNIQUE INDEX anIndexNameB 

ON STUDENT ( MATRIKELNUMMER );
CREATE TABLE EXAMRESULT
(
ID BIGINT PRIMARY KEY NOT NULL,
EXAM VARCHAR(255),
PRUFUNGSDATUM DATE,
MARK INTEGER NOT NULL,
STUDENT_ID BIGINT,
FOREIGN KEY ( STUDENT_ID ) REFERENCES STUDENT ( ID )
);
Relationship mapping
29
ManyToMany
@Entity
public class Student extends BaseEntity {
!
@Column(name = "matrikelNummer", unique = true)
private String registrationNumber;
!
private String name;
!
@OneToOne(fetch = FetchType.LAZY, 

cascade = CascadeType.ALL, 

mappedBy = "grantedTo")
private Scholarship scholarship;
!
@OneToMany(cascade = CascadeType.ALL, 

mappedBy = "student")
private List<ExamResult> examResults;
!
@ManyToMany(mappedBy = "students")
private List<Course> courses;
!
@Transient
private DateTime loginTime;
!
…
}
Important annotations
@ManyToMany Defines a many-
valued association
with many-to-many
multiplicity.
Relationship mapping
30
ManyToMany cont’d
@Entity
public class Course extends BaseEntity {
!
private String courseNumber;
!
private String title;
!
@ManyToMany
private List<Student> students;
…
}
Relationship mapping
31
ManyToMany - resulting SQL DDL
CREATE TABLE STUDENT
(
ID BIGINT PRIMARY KEY NOT NULL,
NAME VARCHAR(255),
MATRIKELNUMMER VARCHAR(255)
);
CREATE UNIQUE INDEX anIndexNameB 

ON STUDENT ( MATRIKELNUMMER );
CREATE TABLE COURSE
(
ID BIGINT PRIMARY KEY NOT NULL,
COURSENUMBER VARCHAR(255),
TITLE VARCHAR(255)
);
CREATE TABLE COURSE_STUDENT
(
COURSES_ID BIGINT NOT NULL,
STUDENTS_ID BIGINT NOT NULL,
FOREIGN KEY ( COURSES_ID ) 

REFERENCES COURSE ( ID ),
FOREIGN KEY ( STUDENTS_ID ) 

REFERENCES STUDENT ( ID )
);
!
Cascade and Fetch
32
▪ Cascade Types
▪ All four relationship annotations may specify operations cascaded to
associated entities
▪ ALL, PERSIST, MERGE, REMOVE, REFRESH, DETACH
▪ Default is none
▪ Orphan Removal
▪ For @OneToOne and @OneToMany relationships
▪ Default is false
▪ Fetching Strategies
▪ Define how object hierarchies are loaded
▪ EAGER = load all related objects immediately
▪ LAZY = load the related objects only if they are accessed for the first time
▪ Be careful with EAGER, as large object graphs may be loaded unintentionally!
Persistence Concepts
33
▪ Persistence Unit (PU)
▪ Defines a set of entity classes managed by the EntityManager instance in
an application
▪ Maps the set of entity classes to a relational database
!
▪ Persistence Context (PC)
▪ Set of managed entity instances that exist in a particular data store
▪ Runtime context
!
▪ Entity Manager (EM)
▪ API for interaction with the persistence context
▪ Manipulates and controls the lifecycle of a persistence context
▪ Creates and removes persistent entity instances
▪ Finds entities using primary keys
▪ Runs queries on entities
Persistence Unit
▪ Persistence Unit
▪ Configuration to map entity classes in an application to a relational
database
▪ persistence.xml defines one or more persistence units
▪ Defined under /conf/META-INF/persistence.xml
▪ Classes with JPA annotations are

automatically detected upon start

of the application
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://
java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>DefaultDS</non-jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>
</persistence-unit>
</persistence>
34
Names must be referenced
in application.conf
API for managing entities
Persistence
Context
Entity Manager
35
Play
application
EntityManager
persist()
remove()
refresh()
merge()
find()
contains()
flush()
createQuery()
createNamedQuery()
Configuration in 	

persistence.xml
Persistence Unit
Managed
Entity
Managed
Entity
Entity lifecycle
36
Persistence Context
new()
persist()
merge()
remove()
updates
Transaction commit
Guaranteed scope 	

of object identity	

!
Only one managed entity 	

in PC represents a row
PC ends
No longer associated with	

persistence context
New Entity
Managed
Entity
Managed
Entity
Managed
Entity
Detached
Entity
Removed
Entity
Entities in managed/persistent state may be manipulated by the application and any changes will
be automatically detected and persisted when the persistence context is flushed. There is no
need to call a particular method to make your modifications persistent.
Entity manager samples
37
Typically one addresses the entity manager from a dedicated persistence service class
public void persist(BaseEntity entity) {
em().persist(entity);
}
public <T extends BaseEntity> T merge(T entity) {
return em().merge(entity);
}
public <T extends BaseEntity> T findEntity(Long id, 

Class<T> entityClazz) {
return em().find(entityClazz, id);
}
public void remove(BaseEntity entity) {
em().remove(entity);
}
Make an entity instance managed and persistent.
Throws EntityExistsException, if the entity
instance already exists.
Make an entity instance managed and
persistent. If it does not exist yet, persist it.
If it already exists, the entity instance is
updated.
Remove the instance.
Find the entity instance
using the primary key.
Finding Entities
38
▪ Find entity by primary key using EntityManager
<T> T find(Class<T> entityClass, Object primaryKey)

▪ Example:
Student student = entityManager.find(Student.class, id);
!
▪ For complex queries use
▪ Java Persistence Query Language (JPQL) or Hibernate Query Language (HQL)
▪ Both are object model focused query languages similar in nature to SQL. JPQL
is a heavily-inspired-by subset of HQL. A JPQL query is always a valid HQL
query, the reverse is not true however.
▪ Criteria API
▪ Native Queries
▪ EntityManager provides methods for creating Query objects
▪ createQuery
▪ createNativeQuery (using plain SQL - not recommended)
JPQL/HQL
39
▪ Similar to SQL
▪ Works with entities as defined in the application and *not* with SQL table
names and attribute names
▪ Portable (they abstract from vendor-specific SQL)
▪ Returns entities (no need to worry about result sets and their manual
conversion to POJOs)
▪ Select, update, delete
!
▪ Support for
▪ Joins
▪ Conditional Expressions
▪ Functional Expressions
▪ Subqueries
▪ Order by, group by, having
▪ …
Querying Entities with JPQL
40
▪ Dynamic Query
▪ Use parameter substitution and do not concatenate the JPQL string
with the parameter values
public List<Student> getStudentByName(String studentName) {
TypedQuery<Student> studentTypedQuery =
em().createQuery("SELECT s FROM Student s WHERE s.name LIKE :studentName", 

Student.class);
!
studentTypedQuery.setParameter("studentName", studentName);
!
return studentTypedQuery.getResultList();
}
Querying Entities with JPQL
41
▪ Static Query
▪ Named Query
▪ Recommended, as it may leverage use of query cache
@NamedQuery(name="findAllStudents",
query="SELECT s FROM Student s")
@Entity
public class Student extends BaseEntity {
…
}
public List<Student> getAllStudents() {
List<Student> students = em().createNamedQuery("findAllStudents", 

Student.class).getResultList();
return students;
}
Further reading: http://www.kumaranuj.com/2013/06/jpa-2-dynamic-queries-vs-named-queries.html
Criteria API
42
▪ Alternative to JPQL, same scope
▪ Dynamic Queries only
!
▪ Clauses are set using Java programming language objects
▪ the query can be created in a typesafe manner
!
▪ Obtain a CriteriaBuilder instance by using the
EntityManager.getCriteriaBuilder method
Querying Entities with Criteria API
43
public Student getStudent(String registrationNumber) {
!
CriteriaBuilder cb = em().getCriteriaBuilder();
CriteriaQuery<Student> criteriaQuery = cb.createQuery(Student.class);
Root<Student> s = criteriaQuery.from(Student.class);
ParameterExpression<String> parameter = cb.parameter(String.class);
criteriaQuery.select(s).where(cb.equal(s.get("registrationNumber"), parameter));
!
TypedQuery<Student> typedQuery = em().createQuery(criteriaQuery);
typedQuery.setParameter(parameter, registrationNumber);
return typedQuery.getSingleResult();
!
}
Querying entities with Hibernate Criteria
44
public List<ExamResult> getNegativeExamResults(Student student) {
Criteria c = ((Session) JPA.em().getDelegate()).createCriteria(Student.class);
c.createCriteria("examResults").add(Restrictions.eq("mark", 5));
return c.list();
}
Wrap up
45
Lessons learned today
▪ JPA/Hibernate provide a powerful ORM feature for Java-based
applications
!
▪ Lot’s of magic happens under the hood - know the data engineering
basics first!
!
▪ Before putting your persistence layer into production, thoroughly test it
using unit test
References
46
1. Sun Microsystems. JSR 220: Enterprise JavaBeansTM, Version 3.0 –
Java Persitence API, 2006
2. Carol McDonald. Java Persistence API: Best Practices, Sun Tech Days
2008-2009

http://de.slideshare.net/caroljmcdonald/td09jpabestpractices2
3. Carol McDonald. Enterprise JavaBean 3.0 & Java Persistence APIs:
Simplifying Persistence, Sun Tech Days 2006-2007

http://de.slideshare.net/caroljmcdonald/persistencecmcdonaldmainejug3
4. The Java EE 6 Tutorial, http://docs.oracle.com/javaee/6/tutorial/doc/
bnbpy.html
5. https://www.codecentric.de/files/2011/05/flush-und-clear-or-mapping-
anti-patterns.pdf (in German)
Backup
47
Further background information for the interested
Object relational mapping (ORM)
48
Why objects and databases do not play well together
▪ Object-Relational Impedance Mismatch (or paradigm mismatch)
▪ RDBMS represent data in tabular format
▪ Object-oriented languages such as Java present data in an interconnected graph of objects
▪ Loading and storing objects using a tabular relational database exposes different problems:
▪ Granularity

Oftentimes the object model will contain more classes, than the number of corresponding tables in the
database
▪ Subtypes

Inheritance is an integral part of object-oriented programming. RDBMS usually do not foresee an
inheritance mechanism.
▪ Identity

A RDMS defines a single notion of sameness: the primary key. Java, however, defines both, object
identity a==b and object equality a.equals(b)
▪ Associations

Associations are represented as unidirectional references in an Object Oriented Language such as
Java. An RDMS uses the concept of foreign keys. If one requires bidirectional relationships in Java, an
association must be defined twice.
▪ Data navigation. Association style navigation (Java), vs. SQL JOINs.
High-level overview of the Hibernate architecture
49
Application
Hibernate
Database
Persistent Objects
Detailed view of the Hibernate architecture
50
TransactionFactory
Database
JNDI JDBC JTA
ConnectionProvider
Session Transaction
SessionFactory
Application
Persistent
Objects
Transient
Objects
http://docs.jboss.org/hibernate/core/3.2/reference/en/html/architecture.html
Transaction
51
What exactly makes a database transaction?
▪ A transaction is a sequence of operations, performed as a single logical
unit of work. The single logical unit of work must have four properties in
order to qualify it as a transaction.
▪ Atomicity

A transaction must be an atomic unit of work, i.e., all of its data
modifications are performed, or no modification is performed at all.
!
▪ Consistency

After a transaction is completed, all data must be left in a consistent
state. The written data must confirm to the defined rules such as
constraints, triggers, cascades, etc.

All internal data structures, such as indexes, must be correct at the
end of the transaction.
Transaction cont’d
52
What exactly makes a database transaction?
▪ Isolation

Modifications of a given transaction must be isolated from modifications
made by other concurrent transactions. A transaction never recognizes
data in an intermediate state, which was potentially caused by another
concurrent transaction.
!
▪ Durability

After a transaction has been completed, its effects are permanently
stored in the system. Modifications persist even in the case of a system
failure.

More Related Content

What's hot

Java persistence api 2.1
Java persistence api 2.1Java persistence api 2.1
Java persistence api 2.1
Rakesh K. Cherukuri
 
JPA For Beginner's
JPA For Beginner'sJPA For Beginner's
JPA For Beginner's
NarayanaMurthy Ganashree
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
NexThoughts Technologies
 
PUC SE Day 2019 - SpringBoot
PUC SE Day 2019 - SpringBootPUC SE Day 2019 - SpringBoot
PUC SE Day 2019 - SpringBoot
Josué Neis
 
Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API
07.pallav
 
Spring Data JPA
Spring Data JPASpring Data JPA
Spring Data JPA
Knoldus Inc.
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - Core
Dzmitry Naskou
 
Hibernate
HibernateHibernate
Hibernate
Prashant Kalkar
 
Ajax ppt - 32 slides
Ajax ppt - 32 slidesAjax ppt - 32 slides
Ajax ppt - 32 slides
Smithss25
 
Spring Data JPA
Spring Data JPASpring Data JPA
Spring Data JPA
Cheng Ta Yeh
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
guest11106b
 
Spring ppt
Spring pptSpring ppt
Spring ppt
Mumbai Academisc
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
Serhat Can
 
java 8 new features
java 8 new features java 8 new features
java 8 new features
Rohit Verma
 
Spring MVC Framework
Spring MVC FrameworkSpring MVC Framework
Spring MVC Framework
Hùng Nguyễn Huy
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
Vikas Jagtap
 
Java Collections
Java  Collections Java  Collections
Spring Boot
Spring BootSpring Boot
Spring Boot
Jiayun Zhou
 
Express js
Express jsExpress js
Express js
Manav Prasad
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
Van Huong
 

What's hot (20)

Java persistence api 2.1
Java persistence api 2.1Java persistence api 2.1
Java persistence api 2.1
 
JPA For Beginner's
JPA For Beginner'sJPA For Beginner's
JPA For Beginner's
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
 
PUC SE Day 2019 - SpringBoot
PUC SE Day 2019 - SpringBootPUC SE Day 2019 - SpringBoot
PUC SE Day 2019 - SpringBoot
 
Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API
 
Spring Data JPA
Spring Data JPASpring Data JPA
Spring Data JPA
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - Core
 
Hibernate
HibernateHibernate
Hibernate
 
Ajax ppt - 32 slides
Ajax ppt - 32 slidesAjax ppt - 32 slides
Ajax ppt - 32 slides
 
Spring Data JPA
Spring Data JPASpring Data JPA
Spring Data JPA
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
 
Spring ppt
Spring pptSpring ppt
Spring ppt
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
java 8 new features
java 8 new features java 8 new features
java 8 new features
 
Spring MVC Framework
Spring MVC FrameworkSpring MVC Framework
Spring MVC Framework
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Express js
Express jsExpress js
Express js
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
 

Viewers also liked

JPA Best Practices
JPA Best PracticesJPA Best Practices
JPA Best Practices
Carol McDonald
 
ORM, JPA, & Hibernate Overview
ORM, JPA, & Hibernate OverviewORM, JPA, & Hibernate Overview
ORM, JPA, & Hibernate Overview
Brett Meyer
 
Database Access With JDBC
Database Access With JDBCDatabase Access With JDBC
Database Access With JDBC
Dharani Kumar Madduri
 
Java Exception Handling Best Practices - Improved Second Version
Java Exception Handling Best Practices - Improved Second VersionJava Exception Handling Best Practices - Improved Second Version
Java Exception Handling Best Practices - Improved Second Version
Lemi Orhan Ergin
 
Introduction to JPA Framework
Introduction to JPA FrameworkIntroduction to JPA Framework
Introduction to JPA Framework
Collaboration Technologies
 
Java Persistence API (JPA) - A Brief Overview
Java Persistence API (JPA) - A Brief OverviewJava Persistence API (JPA) - A Brief Overview
Java Persistence API (JPA) - A Brief Overview
Craig Dickson
 
Behavior Driven Development with Cucumber
Behavior Driven Development with CucumberBehavior Driven Development with Cucumber
Behavior Driven Development with Cucumber
Brandon Keepers
 
Hibernate using jpa
Hibernate using jpaHibernate using jpa
Hibernate using jpa
Mohammad Faizan
 
JPA 2.1 performance tuning tips
JPA 2.1 performance tuning tipsJPA 2.1 performance tuning tips
JPA 2.1 performance tuning tips
osa_ora
 
JPA - Java Persistence API
JPA - Java Persistence APIJPA - Java Persistence API
JPA - Java Persistence API
Thomas Wöhlke
 
Introduction to JPA (JPA version 2.0)
Introduction to JPA (JPA version 2.0)Introduction to JPA (JPA version 2.0)
Introduction to JPA (JPA version 2.0)
ejlp12
 
Hibernate ORM: Tips, Tricks, and Performance Techniques
Hibernate ORM: Tips, Tricks, and Performance TechniquesHibernate ORM: Tips, Tricks, and Performance Techniques
Hibernate ORM: Tips, Tricks, and Performance Techniques
Brett Meyer
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
John Blum
 
Using the latest Java Persistence API 2 Features - Tech Days 2010 India
Using the latest Java Persistence API 2 Features - Tech Days 2010 IndiaUsing the latest Java Persistence API 2 Features - Tech Days 2010 India
Using the latest Java Persistence API 2 Features - Tech Days 2010 India
Arun Gupta
 
Jpa
JpaJpa
Hibernate
HibernateHibernate
Hibernate
Ajay K
 
Hibernate教程
Hibernate教程Hibernate教程
Hibernate教程
Shilong Sang
 
[Java eeconf 2016] spring jta principles of work with transactions. Dmytro S...
[Java eeconf 2016] spring jta  principles of work with transactions. Dmytro S...[Java eeconf 2016] spring jta  principles of work with transactions. Dmytro S...
[Java eeconf 2016] spring jta principles of work with transactions. Dmytro S...
Dmytro Sokolov
 
Thinking Beyond ORM in JPA
Thinking Beyond ORM in JPAThinking Beyond ORM in JPA
Thinking Beyond ORM in JPA
Patrycja Wegrzynowicz
 
Lazy vs. Eager Loading Strategies in JPA 2.1
Lazy vs. Eager Loading Strategies in JPA 2.1Lazy vs. Eager Loading Strategies in JPA 2.1
Lazy vs. Eager Loading Strategies in JPA 2.1
Patrycja Wegrzynowicz
 

Viewers also liked (20)

JPA Best Practices
JPA Best PracticesJPA Best Practices
JPA Best Practices
 
ORM, JPA, & Hibernate Overview
ORM, JPA, & Hibernate OverviewORM, JPA, & Hibernate Overview
ORM, JPA, & Hibernate Overview
 
Database Access With JDBC
Database Access With JDBCDatabase Access With JDBC
Database Access With JDBC
 
Java Exception Handling Best Practices - Improved Second Version
Java Exception Handling Best Practices - Improved Second VersionJava Exception Handling Best Practices - Improved Second Version
Java Exception Handling Best Practices - Improved Second Version
 
Introduction to JPA Framework
Introduction to JPA FrameworkIntroduction to JPA Framework
Introduction to JPA Framework
 
Java Persistence API (JPA) - A Brief Overview
Java Persistence API (JPA) - A Brief OverviewJava Persistence API (JPA) - A Brief Overview
Java Persistence API (JPA) - A Brief Overview
 
Behavior Driven Development with Cucumber
Behavior Driven Development with CucumberBehavior Driven Development with Cucumber
Behavior Driven Development with Cucumber
 
Hibernate using jpa
Hibernate using jpaHibernate using jpa
Hibernate using jpa
 
JPA 2.1 performance tuning tips
JPA 2.1 performance tuning tipsJPA 2.1 performance tuning tips
JPA 2.1 performance tuning tips
 
JPA - Java Persistence API
JPA - Java Persistence APIJPA - Java Persistence API
JPA - Java Persistence API
 
Introduction to JPA (JPA version 2.0)
Introduction to JPA (JPA version 2.0)Introduction to JPA (JPA version 2.0)
Introduction to JPA (JPA version 2.0)
 
Hibernate ORM: Tips, Tricks, and Performance Techniques
Hibernate ORM: Tips, Tricks, and Performance TechniquesHibernate ORM: Tips, Tricks, and Performance Techniques
Hibernate ORM: Tips, Tricks, and Performance Techniques
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
 
Using the latest Java Persistence API 2 Features - Tech Days 2010 India
Using the latest Java Persistence API 2 Features - Tech Days 2010 IndiaUsing the latest Java Persistence API 2 Features - Tech Days 2010 India
Using the latest Java Persistence API 2 Features - Tech Days 2010 India
 
Jpa
JpaJpa
Jpa
 
Hibernate
HibernateHibernate
Hibernate
 
Hibernate教程
Hibernate教程Hibernate教程
Hibernate教程
 
[Java eeconf 2016] spring jta principles of work with transactions. Dmytro S...
[Java eeconf 2016] spring jta  principles of work with transactions. Dmytro S...[Java eeconf 2016] spring jta  principles of work with transactions. Dmytro S...
[Java eeconf 2016] spring jta principles of work with transactions. Dmytro S...
 
Thinking Beyond ORM in JPA
Thinking Beyond ORM in JPAThinking Beyond ORM in JPA
Thinking Beyond ORM in JPA
 
Lazy vs. Eager Loading Strategies in JPA 2.1
Lazy vs. Eager Loading Strategies in JPA 2.1Lazy vs. Eager Loading Strategies in JPA 2.1
Lazy vs. Eager Loading Strategies in JPA 2.1
 

Similar to Introduction to JPA and Hibernate including examples

Database Programming Techniques
Database Programming TechniquesDatabase Programming Techniques
Database Programming Techniques
Raji Ghawi
 
ORM JPA
ORM JPAORM JPA
inf5750---lecture-2.-c---hibernate-intro.pdf
inf5750---lecture-2.-c---hibernate-intro.pdfinf5750---lecture-2.-c---hibernate-intro.pdf
inf5750---lecture-2.-c---hibernate-intro.pdf
bhqckkgwglxjcuctdf
 
10 jdbc
10 jdbc10 jdbc
10 jdbc
snopteck
 
10 jdbc
10 jdbc10 jdbc
10 jdbc
snopteck
 
Spring data requery
Spring data requerySpring data requery
Spring data requery
Sunghyouk Bae
 
Spring framework part 2
Spring framework part 2Spring framework part 2
Spring framework part 2
Haroon Idrees
 
Oop java Concept
Oop java ConceptOop java Concept
Oop java Concept
Tanvir Ahmed
 
EJB 3.0 Java Persistence with Oracle TopLink
EJB 3.0 Java Persistence with Oracle TopLinkEJB 3.0 Java Persistence with Oracle TopLink
EJB 3.0 Java Persistence with Oracle TopLink
Bill Lyons
 
Java one 2010
Java one 2010Java one 2010
Java one 2010
scdn
 
Java Persistence API
Java Persistence APIJava Persistence API
Java Persistence API
Ilio Catallo
 
JavaOne 2017 - JNoSQL: The Definitive Solution for Java and NoSQL Database [C...
JavaOne 2017 - JNoSQL: The Definitive Solution for Java and NoSQL Database [C...JavaOne 2017 - JNoSQL: The Definitive Solution for Java and NoSQL Database [C...
JavaOne 2017 - JNoSQL: The Definitive Solution for Java and NoSQL Database [C...
Leonardo De Moura Rocha Lima
 
Slice: OpenJPA for Distributed Persistence
Slice: OpenJPA for Distributed PersistenceSlice: OpenJPA for Distributed Persistence
Slice: OpenJPA for Distributed Persistence
Pinaki Poddar
 
Alternatives of JPA/Hibernate
Alternatives of JPA/HibernateAlternatives of JPA/Hibernate
Alternatives of JPA/Hibernate
Sunghyouk Bae
 
Entity Persistence with JPA
Entity Persistence with JPAEntity Persistence with JPA
Entity Persistence with JPA
Subin Sugunan
 
NUS Hackers Club Mar 21 - Whats New in JavaSE 8?
NUS Hackers Club Mar 21 - Whats New in JavaSE 8?NUS Hackers Club Mar 21 - Whats New in JavaSE 8?
NUS Hackers Club Mar 21 - Whats New in JavaSE 8?
Chuk-Munn Lee
 
Polyglot persistence with Spring Data
Polyglot persistence with Spring DataPolyglot persistence with Spring Data
Polyglot persistence with Spring Data
Corneil du Plessis
 
13 java beans
13 java beans13 java beans
13 java beans
snopteck
 
Data access
Data accessData access
Data access
Joshua Yoon
 
Introduction to Datastore
Introduction to DatastoreIntroduction to Datastore
Introduction to Datastore
Software Park Thailand
 

Similar to Introduction to JPA and Hibernate including examples (20)

Database Programming Techniques
Database Programming TechniquesDatabase Programming Techniques
Database Programming Techniques
 
ORM JPA
ORM JPAORM JPA
ORM JPA
 
inf5750---lecture-2.-c---hibernate-intro.pdf
inf5750---lecture-2.-c---hibernate-intro.pdfinf5750---lecture-2.-c---hibernate-intro.pdf
inf5750---lecture-2.-c---hibernate-intro.pdf
 
10 jdbc
10 jdbc10 jdbc
10 jdbc
 
10 jdbc
10 jdbc10 jdbc
10 jdbc
 
Spring data requery
Spring data requerySpring data requery
Spring data requery
 
Spring framework part 2
Spring framework part 2Spring framework part 2
Spring framework part 2
 
Oop java Concept
Oop java ConceptOop java Concept
Oop java Concept
 
EJB 3.0 Java Persistence with Oracle TopLink
EJB 3.0 Java Persistence with Oracle TopLinkEJB 3.0 Java Persistence with Oracle TopLink
EJB 3.0 Java Persistence with Oracle TopLink
 
Java one 2010
Java one 2010Java one 2010
Java one 2010
 
Java Persistence API
Java Persistence APIJava Persistence API
Java Persistence API
 
JavaOne 2017 - JNoSQL: The Definitive Solution for Java and NoSQL Database [C...
JavaOne 2017 - JNoSQL: The Definitive Solution for Java and NoSQL Database [C...JavaOne 2017 - JNoSQL: The Definitive Solution for Java and NoSQL Database [C...
JavaOne 2017 - JNoSQL: The Definitive Solution for Java and NoSQL Database [C...
 
Slice: OpenJPA for Distributed Persistence
Slice: OpenJPA for Distributed PersistenceSlice: OpenJPA for Distributed Persistence
Slice: OpenJPA for Distributed Persistence
 
Alternatives of JPA/Hibernate
Alternatives of JPA/HibernateAlternatives of JPA/Hibernate
Alternatives of JPA/Hibernate
 
Entity Persistence with JPA
Entity Persistence with JPAEntity Persistence with JPA
Entity Persistence with JPA
 
NUS Hackers Club Mar 21 - Whats New in JavaSE 8?
NUS Hackers Club Mar 21 - Whats New in JavaSE 8?NUS Hackers Club Mar 21 - Whats New in JavaSE 8?
NUS Hackers Club Mar 21 - Whats New in JavaSE 8?
 
Polyglot persistence with Spring Data
Polyglot persistence with Spring DataPolyglot persistence with Spring Data
Polyglot persistence with Spring Data
 
13 java beans
13 java beans13 java beans
13 java beans
 
Data access
Data accessData access
Data access
 
Introduction to Datastore
Introduction to DatastoreIntroduction to Datastore
Introduction to Datastore
 

Recently uploaded

Russian Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service ...
Russian Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service ...Russian Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service ...
Russian Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service ...
shanihomely
 
How To Fill Timesheet in TaskSprint: Quick Guide 2024
How To Fill Timesheet in TaskSprint: Quick Guide 2024How To Fill Timesheet in TaskSprint: Quick Guide 2024
How To Fill Timesheet in TaskSprint: Quick Guide 2024
TaskSprint | Employee Efficiency Software
 
InflectraCON 360: Risk-Based Testing for Mission Critical Systems
InflectraCON 360: Risk-Based Testing for Mission Critical SystemsInflectraCON 360: Risk-Based Testing for Mission Critical Systems
InflectraCON 360: Risk-Based Testing for Mission Critical Systems
Inflectra
 
Amadeus Travel API, Amadeus Booking API, Amadeus GDS
Amadeus Travel API, Amadeus Booking API, Amadeus GDSAmadeus Travel API, Amadeus Booking API, Amadeus GDS
Amadeus Travel API, Amadeus Booking API, Amadeus GDS
aadhiyaeliza
 
當測試開始左移
當測試開始左移當測試開始左移
當測試開始左移
Jersey (CHE-PING) Su
 
welcome to presentation on Google Apps
welcome to   presentation on Google Appswelcome to   presentation on Google Apps
welcome to presentation on Google Apps
AsifKarimJim
 
🚂🚘 Premium Girls Call Ranchi 🛵🚡000XX00000 💃 Choose Best And Top Girl Service...
🚂🚘 Premium Girls Call Ranchi  🛵🚡000XX00000 💃 Choose Best And Top Girl Service...🚂🚘 Premium Girls Call Ranchi  🛵🚡000XX00000 💃 Choose Best And Top Girl Service...
🚂🚘 Premium Girls Call Ranchi 🛵🚡000XX00000 💃 Choose Best And Top Girl Service...
bahubalikumar09988
 
Celebrity Girls Call Mumbai 9930687706 Unlimited Short Providing Girls Servic...
Celebrity Girls Call Mumbai 9930687706 Unlimited Short Providing Girls Servic...Celebrity Girls Call Mumbai 9930687706 Unlimited Short Providing Girls Servic...
Celebrity Girls Call Mumbai 9930687706 Unlimited Short Providing Girls Servic...
kiara pandey
 
Odoo E-commerce website development guides
Odoo E-commerce website development guidesOdoo E-commerce website development guides
Odoo E-commerce website development guides
jhkdigitalmarketing
 
Fantastic Design Patterns and Where to use them No Notes.pdf
Fantastic Design Patterns and Where to use them No Notes.pdfFantastic Design Patterns and Where to use them No Notes.pdf
Fantastic Design Patterns and Where to use them No Notes.pdf
6m9p7qnjj8
 
Busty Girls Call Mumbai 9930245274 Unlimited Short Providing Girls Service Av...
Busty Girls Call Mumbai 9930245274 Unlimited Short Providing Girls Service Av...Busty Girls Call Mumbai 9930245274 Unlimited Short Providing Girls Service Av...
Busty Girls Call Mumbai 9930245274 Unlimited Short Providing Girls Service Av...
revolutionary575
 
Mumbai Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service A...
Mumbai Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service A...Mumbai Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service A...
Mumbai Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service A...
3610stuck
 
UMiami degree offer diploma Transcript
UMiami degree offer diploma TranscriptUMiami degree offer diploma Transcript
UMiami degree offer diploma Transcript
attueb
 
Celebrity Girls Call Mumbai 9920725232 Unlimited Short Providing Girls Servic...
Celebrity Girls Call Mumbai 9920725232 Unlimited Short Providing Girls Servic...Celebrity Girls Call Mumbai 9920725232 Unlimited Short Providing Girls Servic...
Celebrity Girls Call Mumbai 9920725232 Unlimited Short Providing Girls Servic...
45unexpected
 
Predicting Test Results without Execution (FSE 2024)
Predicting Test Results without Execution (FSE 2024)Predicting Test Results without Execution (FSE 2024)
Predicting Test Results without Execution (FSE 2024)
andrehoraa
 
Mobile App Development Company in Noida - Drona Infotech.
Mobile App Development Company in Noida - Drona Infotech.Mobile App Development Company in Noida - Drona Infotech.
Mobile App Development Company in Noida - Drona Infotech.
Mobile App Development Company in Noida - Drona Infotech
 
Girls Call Mysore 000XX00000 Provide Best And Top Girl Service And No1 in City
Girls Call Mysore 000XX00000 Provide Best And Top Girl Service And No1 in CityGirls Call Mysore 000XX00000 Provide Best And Top Girl Service And No1 in City
Girls Call Mysore 000XX00000 Provide Best And Top Girl Service And No1 in City
neshakor5152
 
BATber53 AWS Modernize your applications with purpose-built AWS databases
BATber53 AWS Modernize your applications with purpose-built AWS databasesBATber53 AWS Modernize your applications with purpose-built AWS databases
BATber53 AWS Modernize your applications with purpose-built AWS databases
BATbern
 
Maximizing Efficiency and Profitability: Optimizing Data Systems, Enhancing C...
Maximizing Efficiency and Profitability: Optimizing Data Systems, Enhancing C...Maximizing Efficiency and Profitability: Optimizing Data Systems, Enhancing C...
Maximizing Efficiency and Profitability: Optimizing Data Systems, Enhancing C...
OnePlan Solutions
 
GT degree offer diploma Transcript
GT degree offer diploma TranscriptGT degree offer diploma Transcript
GT degree offer diploma Transcript
attueb
 

Recently uploaded (20)

Russian Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service ...
Russian Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service ...Russian Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service ...
Russian Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service ...
 
How To Fill Timesheet in TaskSprint: Quick Guide 2024
How To Fill Timesheet in TaskSprint: Quick Guide 2024How To Fill Timesheet in TaskSprint: Quick Guide 2024
How To Fill Timesheet in TaskSprint: Quick Guide 2024
 
InflectraCON 360: Risk-Based Testing for Mission Critical Systems
InflectraCON 360: Risk-Based Testing for Mission Critical SystemsInflectraCON 360: Risk-Based Testing for Mission Critical Systems
InflectraCON 360: Risk-Based Testing for Mission Critical Systems
 
Amadeus Travel API, Amadeus Booking API, Amadeus GDS
Amadeus Travel API, Amadeus Booking API, Amadeus GDSAmadeus Travel API, Amadeus Booking API, Amadeus GDS
Amadeus Travel API, Amadeus Booking API, Amadeus GDS
 
當測試開始左移
當測試開始左移當測試開始左移
當測試開始左移
 
welcome to presentation on Google Apps
welcome to   presentation on Google Appswelcome to   presentation on Google Apps
welcome to presentation on Google Apps
 
🚂🚘 Premium Girls Call Ranchi 🛵🚡000XX00000 💃 Choose Best And Top Girl Service...
🚂🚘 Premium Girls Call Ranchi  🛵🚡000XX00000 💃 Choose Best And Top Girl Service...🚂🚘 Premium Girls Call Ranchi  🛵🚡000XX00000 💃 Choose Best And Top Girl Service...
🚂🚘 Premium Girls Call Ranchi 🛵🚡000XX00000 💃 Choose Best And Top Girl Service...
 
Celebrity Girls Call Mumbai 9930687706 Unlimited Short Providing Girls Servic...
Celebrity Girls Call Mumbai 9930687706 Unlimited Short Providing Girls Servic...Celebrity Girls Call Mumbai 9930687706 Unlimited Short Providing Girls Servic...
Celebrity Girls Call Mumbai 9930687706 Unlimited Short Providing Girls Servic...
 
Odoo E-commerce website development guides
Odoo E-commerce website development guidesOdoo E-commerce website development guides
Odoo E-commerce website development guides
 
Fantastic Design Patterns and Where to use them No Notes.pdf
Fantastic Design Patterns and Where to use them No Notes.pdfFantastic Design Patterns and Where to use them No Notes.pdf
Fantastic Design Patterns and Where to use them No Notes.pdf
 
Busty Girls Call Mumbai 9930245274 Unlimited Short Providing Girls Service Av...
Busty Girls Call Mumbai 9930245274 Unlimited Short Providing Girls Service Av...Busty Girls Call Mumbai 9930245274 Unlimited Short Providing Girls Service Av...
Busty Girls Call Mumbai 9930245274 Unlimited Short Providing Girls Service Av...
 
Mumbai Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service A...
Mumbai Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service A...Mumbai Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service A...
Mumbai Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service A...
 
UMiami degree offer diploma Transcript
UMiami degree offer diploma TranscriptUMiami degree offer diploma Transcript
UMiami degree offer diploma Transcript
 
Celebrity Girls Call Mumbai 9920725232 Unlimited Short Providing Girls Servic...
Celebrity Girls Call Mumbai 9920725232 Unlimited Short Providing Girls Servic...Celebrity Girls Call Mumbai 9920725232 Unlimited Short Providing Girls Servic...
Celebrity Girls Call Mumbai 9920725232 Unlimited Short Providing Girls Servic...
 
Predicting Test Results without Execution (FSE 2024)
Predicting Test Results without Execution (FSE 2024)Predicting Test Results without Execution (FSE 2024)
Predicting Test Results without Execution (FSE 2024)
 
Mobile App Development Company in Noida - Drona Infotech.
Mobile App Development Company in Noida - Drona Infotech.Mobile App Development Company in Noida - Drona Infotech.
Mobile App Development Company in Noida - Drona Infotech.
 
Girls Call Mysore 000XX00000 Provide Best And Top Girl Service And No1 in City
Girls Call Mysore 000XX00000 Provide Best And Top Girl Service And No1 in CityGirls Call Mysore 000XX00000 Provide Best And Top Girl Service And No1 in City
Girls Call Mysore 000XX00000 Provide Best And Top Girl Service And No1 in City
 
BATber53 AWS Modernize your applications with purpose-built AWS databases
BATber53 AWS Modernize your applications with purpose-built AWS databasesBATber53 AWS Modernize your applications with purpose-built AWS databases
BATber53 AWS Modernize your applications with purpose-built AWS databases
 
Maximizing Efficiency and Profitability: Optimizing Data Systems, Enhancing C...
Maximizing Efficiency and Profitability: Optimizing Data Systems, Enhancing C...Maximizing Efficiency and Profitability: Optimizing Data Systems, Enhancing C...
Maximizing Efficiency and Profitability: Optimizing Data Systems, Enhancing C...
 
GT degree offer diploma Transcript
GT degree offer diploma TranscriptGT degree offer diploma Transcript
GT degree offer diploma Transcript
 

Introduction to JPA and Hibernate including examples

  • 1. Introduction to JPA and Hibernate Held as part of the lecture series on Web Engineering at Vienna University of Technology May 2014
  • 2. Business Informatics Group Institute of Software Technology and Interactive Systems 
 Vienna University of Technology Favoritenstraße 9-11/188-3, 1040 Vienna, Austria
 phone: +43 (1) 58801-18804 (secretary), fax: +43 (1) 58801-18896
 office@big.tuwien.ac.at, www.big.tuwien.ac.at Web Engineering Introduction to JPA and Hibernate Philipp Liegl
  • 3. Outline of today’s talk ▪ JDBC ! ▪ JPA/Hibernate ▪ Relationships ▪ Persistence Context/Persistence Unit ▪ Entity Manager ▪ JPQL ▪ Hibernate Criteria API ! Accompanying examples https://github.com/pliegl/we2014/tree/master/jpa-sample 2
  • 4. Motivation 3 N-Tier Architectures ▪ Layers of an information system ▪ Presentation layer ▪ Communication interface to external entities ▪ “View” in the model-view-controller ▪ Application logic layer (service layer) ▪ Implements operations requested by clients
 through the presentation layer ▪ Represents the “business logic” ▪ Resource management layer 
 (persistence layer) ▪ Deals with different data sources of an
 information system ▪ Responsible for storing and retrieving data presentation layer application logic layer resource management layer client TUWIENWEInformationSystem
  • 6. Motivation 5 Traditional persistence with JDBC presentation layer service layer persistence layer JDBC Database DTO DTO
  • 7. Motivation 6 JDBC - Java Database Connectivity ▪ Used to access relational databases from Java programs ▪ First version released 1996 ▪ Ability to ▪ Establish a connection to a database ▪ Execute an SQL statement and return results ▪ Create parameterized queries ▪ Manage database transactions ▪ Basic Steps ▪ Load driver or obtain an already defined data source ▪ Establish connection using a JDBC URL ▪ Create an SQL statement and execute SQL statement ▪ If present, process results present in result sets ▪ Close database resources ▪ Commit or rollback transaction, if necessary http://www.oracle.com/technetwork/java/overview-141217.html
  • 8. JDBC example 7 Insert an entry Connection conn = null; PreparedStatement stmt = null; ! try { conn = connection(); stmt = conn.prepareStatement( "INSERT INTO student VALUES(?, ?, ?)" ); stmt.setInt( 1, student.getId() ); stmt.setString( 2, student.getMatrNr() ); stmt.setString( 3, student.getName() ); stmt.executeUpdate(); stmt.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } }
  • 9. JDBC example 8 Retrieve an entry Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try { conn = connection(); stmt = conn.prepareStatement( "SELECT id, matrnr, name FROM student 
 WHERE id=?" ); stmt.setInt( 1, id ); rs = stmt.executeQuery(); rs.next(); ! Student student = new Student(); student.setId( rs.getInt( 1 ) ); student.setMatrNr( rs.getString( 2 ) ); student.setName( rs.getString( 3 ) ); rs.close(); stmt.close(); return student; } catch (Exception e) { e.printStackTrace(); } finally { … }
  • 10. JDBC 9 Drawbacks ▪ Verbose JDBC boilerplate code for the various CRUD actions ▪ Manual mapping of JDBC result sets to the respective Java POJOs ▪ Imagine 40 different database tables with 20 attributes each… ▪ Manual synchronization of Java code in case of database schema changes (e.g., a new field is added to a database table) ▪ Manual adaptation of the entire related JDBC Java code necessary !
  • 11. Object Relational Mapping 10 Reasons for using ORM ▪ In an application we want to focus on business concepts, not on the relational database structure ▪ Abstract from the “by-hand” communication with the DB (e.g., via JDBC) ▪ Allow for an automatic synchronization between Java Objects and the underlying database ▪ Portability ▪ ORM should be mostly DB independent (with the exception of some types of features, such as identifier generation) ▪ Query abstractions using e.g. JPQL or HQL - the vendor specific SQL is auto-generated ▪ Performance ▪ Object and query caching is automatically done by the ORM
  • 12. Java Persistence API (JPA) 11 Introduction ▪ Specification for the management of persistence and object/relational mapping with Java ▪ Persistence: Data objects shall outlive the JVM app ▪ Objective: provide an object/relational mapping facility for Java developers using a Java domain model and a relational database ▪ Map Java POJOs to relational databases (which are one type of persistence) ▪ Standardized under the Java Community Process Program with contributions from Hibernate, TopLink, JDO, and the EJB community ▪ Hibernate: Full JPA implementation with additional “native” features, e.g., ▪ HQL (Hibernate Query Language) - similar to JPQL, but with some extensions ▪ Criteria API ▪ Used version in this course: Hibernate 4.2.12.Final (supports JPA 2.0)
  • 13. Caveats 12 ORM and JPA ▪ With JPA/Hibernate lots of “magic” is done under the hood, e.g., SQL- DDL is automatically generated ▪ Know the database basics first (e.g., from a data engineering course), in order to fully understand what JPA is doing under the hood ▪ After annotating the classes and running the application check the resulting SQL-DDL (e.g., using the database explorer in IntelliJ or Eclipse) ▪ When executing SQL-Queries using JPA/Hibernate use the “show SQL queries” feature during development, in order to see what kind of queries are actually executed ▪ Set <property name="hibernate.show_sql" value="true" /> in persistence.xml
  • 14. Persistent Entities 13 Basics ▪ Are POJOs (Plain Old Java Objects) ▪ Lightweight persistent domain object ▪ Typically represent a table in a relational database ▪ Each entity instance corresponds to one row in that table ▪ Have a persistent identity ▪ May have both, persistent and transient (non-persistent) state ▪ Simple types (primitive data types, wrappers, enums) ▪ Composite types (e.g., Address) ▪ Non-persistent state (using identifier transient or @Transient annotation)
  • 15. Persistence with Hibernate 14 presentation layer service layer persistence layer Database DomainObjects Cache Hibernate
  • 16. Simple Mapping 15 Enhance Java domain classes with JPA annotations @Entity public class ExamResult { ! @Id private Long id; ! @Column(name = "prufungsDatum") @Temporal(TemporalType.DATE) private Date examDate; ! private int mark; ! @Transient private String examLocation; … } id pruefungsDatum mark ExamResult Important annotations @Entity Specifies that the class is an entity @Id Specifies the primary key of an entity @Temporal Must be specified for fields of type java.util.Date and java.util.Calendar @TemporalType Type used to indicate a specific mapping of java.util.Date or java.util.Calendar. Allowed values: - DATE - TIME - TIMESTAMP @Transient Specifies that the field is not persistent
  • 18. Simple Mapping 17 Inheritance @Entity public class ExamResult extends BaseEntity { ! @Column(name = "prufungsDatum") @Temporal(TemporalType.DATE) private Date examDate; ! private int mark; ! @Transient private String examLocation; ! //Getter and setters omitted ! } id pruefungsDatum mark ExamResult @MappedSuperclass public class BaseEntity { ! @Id @GeneratedValue(
 strategy = 
 GenerationType.AUTO) protected Long id; ! public Long getId() { return id; } } Important annotations @MappedSuperclass Designates a class whose mapping information is applied to the entities that inherit from it. A mapped superclass has no separate table defined for it. @GeneratedValue The GeneratedValue annotation may be applied to a primary key property or field of an entity or mapped superclass in conjunction with the Id annotation.
  • 19. Object-oriented vs. SQL 18 public class Student extends BaseEntity { ! private String registrationNumber; private String name; private List<ExamResult> examResults; … } public class ExamResult extends BaseEntity { ! private Date examDate; private String exam; private int mark; private Student student; … } OO: Student owns the ExamResults
 Usually: no ExamResult without a student SQL: • ExamResult contains a foreign key to the Student it belongs to • The ExamResult owns (contains) the connection • This is opposite to the OO perspective Does not exist in the DB, but is simulated using an SQL query. JPA takes care of that.
  • 20. Entity relationships 19 One-to-one, one-to-many, many-to-many, many-to-one relationships among entities • bi-directional or uni-directional • Support for different Collection types, e.g., List, Set, Map, etc. ! Need to specify the owning side in relationships • Owning side table has the foreign key • OneToOne relationship - the side where the foreign key is specified • OneToMany, ManyToOne - the “many” side
  • 21. Relationship mapping 20 Example using a unidirectional mapping Four different options: 1. Using an embedded table, where Scholarship is the embedded table. (see example) 2. Scholarship and Student are separate tables. The primary key of Scholarship has a foreign key constraint on the primary key of the “owning” Student (using @PrimaryKeyJoinColumn annotation) 3. Scholarship and Student are separate tables. Student holds a foreign key which references the primary key of Scholarship. The foreign key has a unique constraint. 4. Scholarship and Student are separate tables. Scholarship holds a foreign key which references the primary key of Student. The foreign key has a unique constraint. (see example)
  • 22. Relationship mapping 21 Unidirectional OneToOne using an embedded table @Entity public class EmbeddedStudent extends BaseEntity { ! @Column(name = "matrikelNummer", unique = true) private String registrationNumber; ! private String name; ! @Embedded private EmbeddedScholarship scholarship; ! @Transient private DateTime loginTime; ! … } @Embeddable public class EmbeddedScholarship { ! private String description; ! private Integer amount; } ! Important annotations @Embedded Defines a persistent field or property of an entity whose value is an instance of an embeddable class. The embeddable class must be annotated as Embeddable. @Embeddable Defines a class whose instances are stored as an intrinsic part of an owning entity and share the identity of the entity. Each of the persistent properties or fields of the embedded object is mapped to the database table for the entity. Might be an issue with legacy databases, where the DB schema already exists and must not be altered.
  • 23. Relationship mapping 22 Unidirectional OneToOne using an embedded table - resulting SQL DDL @Entity public class EmbeddedStudent extends BaseEntity { ! @Column(name = "matrikelNummer", unique = true) private String registrationNumber; ! private String name; ! @Embedded private EmbeddedScholarship scholarship; ! @Transient private DateTime loginTime; ! … } @Embeddable public class EmbeddedScholarship { ! private String description; ! private Integer amount; } ! CREATE TABLE EMBEDDEDSTUDENT ( ID BIGINT PRIMARY KEY NOT NULL, NAME VARCHAR(255), MATRIKELNUMMER VARCHAR(255), AMOUNT INTEGER, DESCRIPTION VARCHAR(255) ); CREATE UNIQUE INDEX anIndexName ON EMBEDDEDSTUDENT ( MATRIKELNUMMER );
  • 24. Relationship mapping 23 Bidirectional OneToOne using foreign key @Entity public class Student extends BaseEntity { ! @Column(name = "matrikelNummer", 
 unique = true) private String registrationNumber; ! private String name; ! @OneToOne(fetch = FetchType.LAZY, 
 cascade = CascadeType.ALL,
 mappedBy="grantedTo") private Scholarship scholarship; ! @Transient private DateTime loginTime; … } Important annotations @OneToOne Defines a single-valued association to another entity that has one-to- one multiplicity. @FetchType LAZY = do not load referenced entity, until it is accessed for the first time EAGER = load referenced entity immediately @CascadeType Defines the set of cascadable operations that are propagated to the associated entity. ALL is equivalent to cascade={PERSIST, MERGE, REMOVE, REFRESH, DETACH} mappedBy References the field that “owns” the relationship in the referenced entity. Required unless the relationship is unidirectional. The “non-owning” side
  • 25. Relationship mapping 24 Bidirectional OneToOne using foreign key cont’d @Entity public class Scholarship extends BaseEntity { ! private String description; ! private Integer amount; ! @JoinColumn(name=“student_id”, unique=true) @OneToOne private Student grantedTo; ! … } The “owning” side Important annotations @JoinColumn Specifies a column for joining an entity association or element collection. 
 In this case: the name of the column, where the foreign key will be stored.
  • 26. Relationship mapping 25 Bidirectional OneToOne using foreign key - resulting SQL DDL CREATE TABLE STUDENT ( ID BIGINT PRIMARY KEY NOT NULL, NAME VARCHAR(255), MATRIKELNUMMER VARCHAR(255) ); CREATE UNIQUE INDEX anIndexName ON STUDENT ( MATRIKELNUMMER ); CREATE TABLE SCHOLARSHIP ( ID BIGINT PRIMARY KEY NOT NULL, AMOUNT INTEGER, DESCRIPTION VARCHAR(255), STUDENT_ID BIGINT, FOREIGN KEY ( STUDENT_ID ) 
 REFERENCES STUDENT ( ID ) ); CREATE UNIQUE INDEX uniqueIndexName 
 ON SCHOLARSHIP ( STUDENT_ID );
  • 27. Relationship mapping 26 Bidirectional OneToMany @Entity public class Student extends BaseEntity { ! @Column(name = "matrikelNummer", unique = true) private String registrationNumber; ! private String name; ! @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, 
 mappedBy = "grantedTo") private Scholarship scholarship; ! @OneToMany(cascade = CascadeType.ALL, mappedBy = "student") private List<ExamResult> examResults; ! @Transient private DateTime loginTime; … } Important annotations @OneToMany Defines a many-valued association with one-to-many multiplicity. The “non-owning” side
  • 28. Relationship mapping 27 Bidirectional OneToMany cont’d @Entity public class ExamResult extends BaseEntity { ! @Column(name = "prufungsDatum") @Temporal(TemporalType.DATE) private Date examDate; ! private String exam; ! private int mark; ! @ManyToOne private Student student; ! ! @Transient private String examLocation; … } Important annotations @ManyToOne Defines a single-valued association to another entity class that has many-to-one multiplicity. The “owning” side
  • 29. Relationship mapping 28 Bidirectional OneToMany - resulting SQL DDL CREATE TABLE STUDENT ( ID BIGINT PRIMARY KEY NOT NULL, NAME VARCHAR(255), MATRIKELNUMMER VARCHAR(255) ); CREATE UNIQUE INDEX anIndexNameB 
 ON STUDENT ( MATRIKELNUMMER ); CREATE TABLE EXAMRESULT ( ID BIGINT PRIMARY KEY NOT NULL, EXAM VARCHAR(255), PRUFUNGSDATUM DATE, MARK INTEGER NOT NULL, STUDENT_ID BIGINT, FOREIGN KEY ( STUDENT_ID ) REFERENCES STUDENT ( ID ) );
  • 30. Relationship mapping 29 ManyToMany @Entity public class Student extends BaseEntity { ! @Column(name = "matrikelNummer", unique = true) private String registrationNumber; ! private String name; ! @OneToOne(fetch = FetchType.LAZY, 
 cascade = CascadeType.ALL, 
 mappedBy = "grantedTo") private Scholarship scholarship; ! @OneToMany(cascade = CascadeType.ALL, 
 mappedBy = "student") private List<ExamResult> examResults; ! @ManyToMany(mappedBy = "students") private List<Course> courses; ! @Transient private DateTime loginTime; ! … } Important annotations @ManyToMany Defines a many- valued association with many-to-many multiplicity.
  • 31. Relationship mapping 30 ManyToMany cont’d @Entity public class Course extends BaseEntity { ! private String courseNumber; ! private String title; ! @ManyToMany private List<Student> students; … }
  • 32. Relationship mapping 31 ManyToMany - resulting SQL DDL CREATE TABLE STUDENT ( ID BIGINT PRIMARY KEY NOT NULL, NAME VARCHAR(255), MATRIKELNUMMER VARCHAR(255) ); CREATE UNIQUE INDEX anIndexNameB 
 ON STUDENT ( MATRIKELNUMMER ); CREATE TABLE COURSE ( ID BIGINT PRIMARY KEY NOT NULL, COURSENUMBER VARCHAR(255), TITLE VARCHAR(255) ); CREATE TABLE COURSE_STUDENT ( COURSES_ID BIGINT NOT NULL, STUDENTS_ID BIGINT NOT NULL, FOREIGN KEY ( COURSES_ID ) 
 REFERENCES COURSE ( ID ), FOREIGN KEY ( STUDENTS_ID ) 
 REFERENCES STUDENT ( ID ) ); !
  • 33. Cascade and Fetch 32 ▪ Cascade Types ▪ All four relationship annotations may specify operations cascaded to associated entities ▪ ALL, PERSIST, MERGE, REMOVE, REFRESH, DETACH ▪ Default is none ▪ Orphan Removal ▪ For @OneToOne and @OneToMany relationships ▪ Default is false ▪ Fetching Strategies ▪ Define how object hierarchies are loaded ▪ EAGER = load all related objects immediately ▪ LAZY = load the related objects only if they are accessed for the first time ▪ Be careful with EAGER, as large object graphs may be loaded unintentionally!
  • 34. Persistence Concepts 33 ▪ Persistence Unit (PU) ▪ Defines a set of entity classes managed by the EntityManager instance in an application ▪ Maps the set of entity classes to a relational database ! ▪ Persistence Context (PC) ▪ Set of managed entity instances that exist in a particular data store ▪ Runtime context ! ▪ Entity Manager (EM) ▪ API for interaction with the persistence context ▪ Manipulates and controls the lifecycle of a persistence context ▪ Creates and removes persistent entity instances ▪ Finds entities using primary keys ▪ Runs queries on entities
  • 35. Persistence Unit ▪ Persistence Unit ▪ Configuration to map entity classes in an application to a relational database ▪ persistence.xml defines one or more persistence units ▪ Defined under /conf/META-INF/persistence.xml ▪ Classes with JPA annotations are
 automatically detected upon start
 of the application <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http:// java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <non-jta-data-source>DefaultDS</non-jta-data-source> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/> <property name="hibernate.hbm2ddl.auto" value="create" /> </properties> </persistence-unit> </persistence> 34 Names must be referenced in application.conf
  • 36. API for managing entities Persistence Context Entity Manager 35 Play application EntityManager persist() remove() refresh() merge() find() contains() flush() createQuery() createNamedQuery() Configuration in persistence.xml Persistence Unit Managed Entity Managed Entity
  • 37. Entity lifecycle 36 Persistence Context new() persist() merge() remove() updates Transaction commit Guaranteed scope of object identity ! Only one managed entity in PC represents a row PC ends No longer associated with persistence context New Entity Managed Entity Managed Entity Managed Entity Detached Entity Removed Entity Entities in managed/persistent state may be manipulated by the application and any changes will be automatically detected and persisted when the persistence context is flushed. There is no need to call a particular method to make your modifications persistent.
  • 38. Entity manager samples 37 Typically one addresses the entity manager from a dedicated persistence service class public void persist(BaseEntity entity) { em().persist(entity); } public <T extends BaseEntity> T merge(T entity) { return em().merge(entity); } public <T extends BaseEntity> T findEntity(Long id, 
 Class<T> entityClazz) { return em().find(entityClazz, id); } public void remove(BaseEntity entity) { em().remove(entity); } Make an entity instance managed and persistent. Throws EntityExistsException, if the entity instance already exists. Make an entity instance managed and persistent. If it does not exist yet, persist it. If it already exists, the entity instance is updated. Remove the instance. Find the entity instance using the primary key.
  • 39. Finding Entities 38 ▪ Find entity by primary key using EntityManager <T> T find(Class<T> entityClass, Object primaryKey)
 ▪ Example: Student student = entityManager.find(Student.class, id); ! ▪ For complex queries use ▪ Java Persistence Query Language (JPQL) or Hibernate Query Language (HQL) ▪ Both are object model focused query languages similar in nature to SQL. JPQL is a heavily-inspired-by subset of HQL. A JPQL query is always a valid HQL query, the reverse is not true however. ▪ Criteria API ▪ Native Queries ▪ EntityManager provides methods for creating Query objects ▪ createQuery ▪ createNativeQuery (using plain SQL - not recommended)
  • 40. JPQL/HQL 39 ▪ Similar to SQL ▪ Works with entities as defined in the application and *not* with SQL table names and attribute names ▪ Portable (they abstract from vendor-specific SQL) ▪ Returns entities (no need to worry about result sets and their manual conversion to POJOs) ▪ Select, update, delete ! ▪ Support for ▪ Joins ▪ Conditional Expressions ▪ Functional Expressions ▪ Subqueries ▪ Order by, group by, having ▪ …
  • 41. Querying Entities with JPQL 40 ▪ Dynamic Query ▪ Use parameter substitution and do not concatenate the JPQL string with the parameter values public List<Student> getStudentByName(String studentName) { TypedQuery<Student> studentTypedQuery = em().createQuery("SELECT s FROM Student s WHERE s.name LIKE :studentName", 
 Student.class); ! studentTypedQuery.setParameter("studentName", studentName); ! return studentTypedQuery.getResultList(); }
  • 42. Querying Entities with JPQL 41 ▪ Static Query ▪ Named Query ▪ Recommended, as it may leverage use of query cache @NamedQuery(name="findAllStudents", query="SELECT s FROM Student s") @Entity public class Student extends BaseEntity { … } public List<Student> getAllStudents() { List<Student> students = em().createNamedQuery("findAllStudents", 
 Student.class).getResultList(); return students; } Further reading: http://www.kumaranuj.com/2013/06/jpa-2-dynamic-queries-vs-named-queries.html
  • 43. Criteria API 42 ▪ Alternative to JPQL, same scope ▪ Dynamic Queries only ! ▪ Clauses are set using Java programming language objects ▪ the query can be created in a typesafe manner ! ▪ Obtain a CriteriaBuilder instance by using the EntityManager.getCriteriaBuilder method
  • 44. Querying Entities with Criteria API 43 public Student getStudent(String registrationNumber) { ! CriteriaBuilder cb = em().getCriteriaBuilder(); CriteriaQuery<Student> criteriaQuery = cb.createQuery(Student.class); Root<Student> s = criteriaQuery.from(Student.class); ParameterExpression<String> parameter = cb.parameter(String.class); criteriaQuery.select(s).where(cb.equal(s.get("registrationNumber"), parameter)); ! TypedQuery<Student> typedQuery = em().createQuery(criteriaQuery); typedQuery.setParameter(parameter, registrationNumber); return typedQuery.getSingleResult(); ! }
  • 45. Querying entities with Hibernate Criteria 44 public List<ExamResult> getNegativeExamResults(Student student) { Criteria c = ((Session) JPA.em().getDelegate()).createCriteria(Student.class); c.createCriteria("examResults").add(Restrictions.eq("mark", 5)); return c.list(); }
  • 46. Wrap up 45 Lessons learned today ▪ JPA/Hibernate provide a powerful ORM feature for Java-based applications ! ▪ Lot’s of magic happens under the hood - know the data engineering basics first! ! ▪ Before putting your persistence layer into production, thoroughly test it using unit test
  • 47. References 46 1. Sun Microsystems. JSR 220: Enterprise JavaBeansTM, Version 3.0 – Java Persitence API, 2006 2. Carol McDonald. Java Persistence API: Best Practices, Sun Tech Days 2008-2009
 http://de.slideshare.net/caroljmcdonald/td09jpabestpractices2 3. Carol McDonald. Enterprise JavaBean 3.0 & Java Persistence APIs: Simplifying Persistence, Sun Tech Days 2006-2007
 http://de.slideshare.net/caroljmcdonald/persistencecmcdonaldmainejug3 4. The Java EE 6 Tutorial, http://docs.oracle.com/javaee/6/tutorial/doc/ bnbpy.html 5. https://www.codecentric.de/files/2011/05/flush-und-clear-or-mapping- anti-patterns.pdf (in German)
  • 49. Object relational mapping (ORM) 48 Why objects and databases do not play well together ▪ Object-Relational Impedance Mismatch (or paradigm mismatch) ▪ RDBMS represent data in tabular format ▪ Object-oriented languages such as Java present data in an interconnected graph of objects ▪ Loading and storing objects using a tabular relational database exposes different problems: ▪ Granularity
 Oftentimes the object model will contain more classes, than the number of corresponding tables in the database ▪ Subtypes
 Inheritance is an integral part of object-oriented programming. RDBMS usually do not foresee an inheritance mechanism. ▪ Identity
 A RDMS defines a single notion of sameness: the primary key. Java, however, defines both, object identity a==b and object equality a.equals(b) ▪ Associations
 Associations are represented as unidirectional references in an Object Oriented Language such as Java. An RDMS uses the concept of foreign keys. If one requires bidirectional relationships in Java, an association must be defined twice. ▪ Data navigation. Association style navigation (Java), vs. SQL JOINs.
  • 50. High-level overview of the Hibernate architecture 49 Application Hibernate Database Persistent Objects
  • 51. Detailed view of the Hibernate architecture 50 TransactionFactory Database JNDI JDBC JTA ConnectionProvider Session Transaction SessionFactory Application Persistent Objects Transient Objects http://docs.jboss.org/hibernate/core/3.2/reference/en/html/architecture.html
  • 52. Transaction 51 What exactly makes a database transaction? ▪ A transaction is a sequence of operations, performed as a single logical unit of work. The single logical unit of work must have four properties in order to qualify it as a transaction. ▪ Atomicity
 A transaction must be an atomic unit of work, i.e., all of its data modifications are performed, or no modification is performed at all. ! ▪ Consistency
 After a transaction is completed, all data must be left in a consistent state. The written data must confirm to the defined rules such as constraints, triggers, cascades, etc.
 All internal data structures, such as indexes, must be correct at the end of the transaction.
  • 53. Transaction cont’d 52 What exactly makes a database transaction? ▪ Isolation
 Modifications of a given transaction must be isolated from modifications made by other concurrent transactions. A transaction never recognizes data in an intermediate state, which was potentially caused by another concurrent transaction. ! ▪ Durability
 After a transaction has been completed, its effects are permanently stored in the system. Modifications persist even in the case of a system failure.