Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 31

Java persistence API

Hibernate, JPA

Ionut Vlad
March 2020
Agenda

What, why?
JPA annotations + hands on?
Entity Manager + Repos?
Transactions/ACID
JPQL + queries

2
What?
 Java Persistence API(JPA)
– specification for relational data management in Java applications
 Hibernate
– provides an implementation of JPA
 Spring DATA JPA
– JPA Data access Abstraction layer
– Always needs a JPA provider(Hibernate, Eclipse Link)

Why?
 Information is power!!!
 Removes boilerplate code
 Powerful tool
 M from MVC

3
Project set-up
 Hibernate dependencies:

4
Project set-up - JPA
 persistence.xml:

5
Project set-up - Hibernate
 hibernate.cfg.xml:

6
Project set-up – Spring JPA
 application.properties

7
JPA Annotations – mapping classes
@Entity
- Mandatory
- Class needs to have a no-args constructor
- Only top-level non-final POJOs
- Class needs to have an @Id
- By default is the name of the annotated class

@Table
- Enhances @Entity
- Used when name of the table is different

@Id
- Defines PK
- Used with @Column if name of the column
name is different in the table

@GeneratedValue
- Auto – JPA provider will choose the strategy
- Some might not be supported by DB provider

8
JPA Annotations - mapping classes

@Column
- Links Entity attribute to table column
- By default most permissive
- Can be used only at attribute or getter level

@Temporal
- Used to map Date/Time/Timestamps columns

@Transient
- For non-persistent fields

@Enumerated
- Used to persist values of an Java enum
- EnumType.String persists “FEMALE”
- EnumType.Ordinal persists 0
- Default is Ordinal.

9
JPA Annotations mapping classes
@Embeddable
- Wrapper for columns

@Embedded
- Allows JPA to use embeddable POJOs

@EmbeddedId
- EmbeddedId class needs to implement
Serializable interface

10
JPA Annotations – mapping relationships
@OneToOne

11
JPA Annotations – mapping relationships
@OneToMany and @ManyToOne

12
JPA Annotations – mapping relationships
@ManyToMany – v1

13
JPA Annotations – mapping relationships
@ManyToMany – v2

14
JPA Annotations

@MappedSuperclass

15
JPA Annotations
- @Inheritance – single table

16
JPA Annotations
- @Inheritance – joined

17
JPA Annotations
- @Inheritance – table per concrete class

18
Java Data Types supported
 Primitive types + Wrappers + String
 java.math.BigInteger, java.math.BigDecimal
 java.util.Date, java.util.Calendar, java.util.Timestamp
 java.sql.Date( 2020-12-31), java.sql.Time(23:59:59), java.sql.Timestamp(2020-12-31 23:59:59)
 Collections from java.util: ArrayList, Vector, Stack, LinkedList, ArrayDeque, PriorityQueue, HashSet,
LinkedHashSet, TreeSet.
 Map types from java.util: HashMap, Hashtable, WeakHashMap, IdentityHashMap, LinkedHashMap,
TreeMap and Properties
 Enums
 @Lob – used for storing BLOB(binary large object)
or CLOB(Character Large Object)

19
Entities state management
 New(Transient)
– Newly created object, which is not associated with database row.
 Managed(Persisted)
– Associated with a database table
– Managed by persistence context( changes are detected and persisted during the flush).
 Detached
– When the current persistence context is closed.
– Can be reattached or merged if there’s no other object that maps the same row
 Deleted
– Entities that are scheduled to be deleted

20
JPA Entity Manager
 EntityManager – interface, manages the persistence operations.
 EntityManagerFactory – creates and manages multiple EntityManager instances
 EntityTransaction – one-to-one relation with an EntityManager
 Persistence – contains static methods to obtain EntityManagerFactory

Hibernate Session
 Session – interface, manages the persistence operations
 SessionFactory – creates and manages multiple Session instances
 Transaction – Atomic block of work, which can be committed or rolled-back. A Session can
have multiple transaction.

21
Transactions
 Atomicity - Atomicity ensures that a transaction will not
be successful unless all parts of it are successful.
Since an incomplete transaction will fail, the database
is less likely to get corrupted or incomplete data as a result.
 Consistency – comply with data validation rules. Rolling
Back on failure protects us against data corruption
 Isolation – handle each transaction without affecting the others.
 Durability – completed transactions are saved regardless of later failures.

22
Ways to interact with data
 javax.persistence.EntityManager interface:

23
Ways to interact with data
 org.hibernate.Session interface:

24
Ways to interact with data
 javax.persistence.Query interface:

25
Ways to interact with data
 Native queries:

 Named queries:

 JPQL queries

 Criteria API – T.B.C.

26
Save data

27
Delete/Update data

28
Query data – hands on

29
Useful links
 https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#reference
 https://www.tutorialspoint.com/jpa/jpa_jpql.htm
 https://thoughts-on-java.org/jpql/
 https://www.tutorialspoint.com/jpa/jpa_criteria_api.htm

30
31

You might also like