55
Java Persistence API (JPA) Petr Kˇ remen [email protected] Winter Term 2020 Petr Kˇ remen ([email protected]) Java Persistence API (JPA) Winter Term 2020 1 / 55

Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

  • Upload
    others

  • View
    16

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

Java Persistence API (JPA)

Petr Kremen

[email protected]

Winter Term 2020

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 1 / 55

Page 2: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

Contents

1 Data Persistence

2 From JDBC to JPA

3 JPA Basics

4 Object-Relational Mapping (ORM) Basics

5 ORM Relationship Mapping

6 ORM Inheritance Mapping

7 Summary

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 2 / 55

Page 3: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

Data Persistence

Data Persistence

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 3 / 55

Page 4: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

Data Persistence

What “data persistence” means ?

We manipulate data (represented as object state) that need to be stored

persistently to survive a single run of the application

queriably to be able to retrieve/access them

scalably to be able to handle large data volumes

transactionally to ensure their consistency

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 4 / 55

Page 5: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

Data Persistence

How to achieve persistence I

Serialization simple, yet hardly queriable, not transactional, ...

stream persisting an instance of class C is deprecatedonce definition of C is modified (e.g. fieldadded/removed).

Relational Databases (MySQL, PostgreSQL, Oracle, ...)

efficient storage for data with rigid schema

well-established and most popular technology

efficient search using SQL standard

secure and Transactional (ACID)

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 5 / 55

Page 6: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

Data Persistence

How to achieve persistence II

NoSQL DatabasesKey-value storages (MongoDB, Hadoop, ...)

suitable for data without rigid schema

Object Databases

designed in 90’s to capture complexity of object models(e.g. inheritance)Issues: scalability, standardized queries

RDF Triple Stores (SDB, TDB, Sesame, Virtuoso, ...)

graph stores for distributed semantic web data –RDF(S), OWL

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 6 / 55

Page 7: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

Data Persistence

Programmatic Access to Relational Databases (RDBMS)

JDBC (JSR 221)

Java standard to ensure independence on the particular RDBMS (atleast theoretically)

EJB 2.1 (JSR 153)

Provides Object Relational Mapping (ORM), but complicated

(single entity = several Java files + XMLs)

distributed transactions, load balancing

iBatis,Hibernate – ORM driving forces for JPA 2

JPA 2 (JSR 317)

Standardized ORM solution for both standalone and Jakarta EEapplications

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 7 / 55

Page 8: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

From JDBC to JPA

From JDBC to JPA

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 8 / 55

Page 9: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

From JDBC to JPA

JDBCJava standard to ensure independence on the particular RDBMS (at leasttheoretically)Connection connection = null;PreparedStatement statement = null;try {

Class.forName("org.postgresql.Driver");connection = DriverManager.getConnection(jdbcURL,dbUser,dbPassword);statement = connection.prepareStatement("SELECT * FROM PERSON WHERE HASNAME LIKE ?");statement.setString(1, "%Pepa%");ResultSet rs = statement.executeQuery();

} catch (ClassNotFoundException e) {e.printStackTrace();

} catch (SQLException e) {e.printStackTrace();if ( statement != null ) {

try {statement.close();

} catch (SQLException e1) {e1.printStackTrace();

}}if ( connection != null ) {

try {connection.close();

} catch (SQLException e1) {e1.printStackTrace();

}}

}

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 9 / 55

Page 10: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

From JDBC to JPA

JDBC – entities CRUD

Create

PreparedStatement statement =connection.prepareStatement("INSERT INTO PERSON (id,hasname)VALUES (?,?)");

statement.setLong(1,10);statement.setString(2,"Honza");statement.executeUpdate();

Retrieve

PreparedStatement statement =connection.prepareStatement("SELECT * FROM PERSON WHERE ID=?");

statement.setLong(1,2);ResultSet rs = statement.executeQuery();

Update

PreparedStatement statement =connection.prepareStatement("UPDATE PERSON SET HASNAME=’Jirka’ WHERE ID=?");

statement.setLong(1,2);statement.executeUpdate();

Delete

PreparedStatement statement =connection.prepareStatement("DELETE FROM PERSON WHERE ID=?");

statement.setLong(1,1);statement.executeUpdate();

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 10 / 55

Page 11: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

From JDBC to JPA

Question 1: Why prepared statements ?

PreparedStatement statement =connection.prepareStatement("INSERT INTO PERSON (id,hasname) VALUES (?,?)"

);statement.setLong(1,10);statement.setString(2,"Honza");statement.executeUpdate();

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 11 / 55

Page 12: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

From JDBC to JPA

How to avoid boilerplate code ?

Boilerplate code

Obtaining (pooled) connectionSQLException handlingcreating Java objects out of the query results:

ResultSet rs = ...while(rs.next()) {

Person p = new Person();p.setId(rs.getLong("ID"));p.setHasName(rs.getString("HASNAME"));

}

Although SQL is a standard – there are still differ-ences in implementations (MySQL autoincrement, PostgreSQL serial ...)

solution = Object Relational Mapping (ORM)

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 12 / 55

Page 13: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

JPA Basics

JPA Basics

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 13 / 55

Page 14: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

JPA Basics

ORM Architecture

idea: “map whole Java classes to database records”

a typical system architecture with ORM:

@Entitypublic Person {@Idprivate Long id;private String hasName;// setters+getters

}

CREATE TABLE PERSON (ID bigint PRIMARY KEY NOT NULL,HASNAME varchar(255)

);

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 14 / 55

Page 15: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

JPA Basics

CRUD using JPA 2.0

InitializationEntityManagerFactory f = Persistence.createEntityManagerFactory("pu");EntityManager em = f.createEntityManager();EntityTransaction t = em.getTransaction();t.begin();

CreatePerson person = new Person();person.setId(10);Person.setHasName("Honza");em.persist(person);

RetrievePerson person = em.find(Person.class, 2);

UpdatePerson person = em.find(Person.class, 2);person.setHasName("Jirka");

DeletePerson person = em.find(Person.class, 1);em.remove(person);

Finalizationt.commit();

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 15 / 55

Page 16: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

JPA Basics

JPA 2.1

Java Persistence API 2.1 (JSR-338)

Although part of Java EE 7 specifications, JPA 2.1 can be used bothin EE and SE applications.

Main topics covered:

Basic scenariosController logic – EntityManager interfaceORM strategiesJPQL + Criteria API

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 16 / 55

Page 17: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

JPA Basics

JPA 2.1 – Entity Example

Minimal example (configuration by exception):

@Entitypublic class Person {

@Id@GeneratedValueprivate Integer id;

private String name;

// setters + getters}

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 17 / 55

Page 18: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

JPA Basics

JPA Basics

Let’s have a set of”suitably annotated“ POJOs, called entities,

describing your domain model.

A set of entities is logically grouped into a persistence unit.

JPA providers :

generate persistence unit from existing database,generate database schema from existing persistence unit.

Question: What is the benefit of the keeping Your domain model inthe persistence unit entities (OO) instead of the database schema(SQL) ?

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 18 / 55

Page 19: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

JPA Basics

JPA – Model

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 19 / 55

Page 20: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

JPA Basics

JPA 2.0 – Persistence Context

In runtime, the application accesses the object counterpart(represented by entity instances ) of the database data. These(managed) entities comprise a persistence context (PC).

PC is synchronized with the database on demand (refresh, flush) or attransaction commit.PC is accessed by an EntityManager instance and can be shared byseveral EntityManager instances.

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 20 / 55

Page 21: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

JPA Basics

JPA – Operations

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 21 / 55

Page 22: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

JPA Basics

JPA – Entity States

source: Wikipedia,http://cs.wikipedia.org/wiki/Java_Persistence_API

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 22 / 55

Page 23: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

JPA Basics

JPA – Operation Details

persist stores a new entity intopersistence context(PC). The PC mustnot contain an entitywith the same id,

merge merges a detachedentity with its managedversion (inside PC),

find finds an entity in theDB and fetches it intoPC,

refresh “reverts” a managedentity state from DB,

remove deletes a managedentity from PC.

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 23 / 55

Page 24: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

JPA Basics

JPA – EntityManager

EntityManager (EM) instance is in fact a generic DAO, whileentities can be understand as DPO (managed) or DTO (detached).

Selected operations on EM (CRUD) :

Create : em.persist(Object o)Read : em.find(Object id), em.refresh(Object o)

Update : em.merge(Object o)Delete : em.remove(Object o)

native/JPQL queries : em.createNativeQuery, em.createQuery, etc.Resource-local transactions :

em.getTransaction.[begin(),commit(),rollback()]

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 24 / 55

Page 25: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

Object-Relational Mapping (ORM) Basics

Object-Relational Mapping (ORM)Basics

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 25 / 55

Page 26: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

Object-Relational Mapping (ORM) Basics

Object model

Which one is correct ?

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 26 / 55

Page 27: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

Object-Relational Mapping (ORM) Basics

Database model

Which one is correct ?

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 27 / 55

Page 28: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

Object-Relational Mapping (ORM) Basics

ORM Basics

Simple View

Java Classes = Entities = SQL tablesJava Fields/accessors = Entity properties = SQL columns

The ORM is realized by means of Java annotations/XML.

Physical Schema annotations

@Table, @Column, @JoinColumn, @JoinTable, etc.

Logical Schema annotations

@Entity, @OneToMany, @ManyToMany, etc.

Each property can be fetched lazily/eagerly.

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 28 / 55

Page 29: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

Object-Relational Mapping (ORM) Basics

Mapping basic types

Primitive Java types:

String → varchar/text,

Integer → int,

byte[] → blob,

etc.

@Column – physical schemaproperties of the particularcolumn (insertable, updatable,precise data type, defaults, etc.)

@Lob – large objects

Default EAGER fetching (except@Lobs)

@Column(name="id")private String getName();

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 29 / 55

Page 30: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

Object-Relational Mapping (ORM) Basics

Mapping enums/temporals

Enums

@Enumerated(value=EnumType.String)private EnumPersonType type;

Stored either in a text column, or in an int column

Temporals

@Temporal(TemporalType.Date)private java.util.Date datum;

Stored in respective column type according to theTemporalType.

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 30 / 55

Page 31: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

Object-Relational Mapping (ORM) Basics

ORM – Identifiers

Single-attribute: @Id

Multiple-attribute – an identifier class must exist

Id. class: @IdClass, entity ids: @IdId. class: @Embeddable, entity id: @EmbeddedId

@Id@GeneratedValue(strategy=GenerationType.SEQUENCE)private int id;

Question: How to write hashCode, equals for entities ?

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 31 / 55

Page 32: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

Object-Relational Mapping (ORM) Basics

ORM – Generating Identifiers

Strategies

SEQUENCE – using the database native SEQUENCE functionality(Oracle, PostgreSQL)

IDENTITY – some DBMSs implement autonumber column (MS SQL,MySQL)

TABLE – special table keeps the last generated values

AUTO – the provider picks its own strategy

For database-related strategies, the value of id is set only on

Transaction.commit()

em.flush()

em.refresh()

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 32 / 55

Page 33: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

Object-Relational Mapping (ORM) Basics

ORM – Generated Identifiers TABLE strategy

@TableGenerator(name="Address_Gen",table="ID_GEN",pkColumnName="GEN_NAME",valueColumnName="GEN_VAL",initialValue=10000,allocationSize=100)

@Id@GeneratedValue(generator="AddressGen")private int id;

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 33 / 55

Page 34: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

ORM Relationship Mapping

ORM Relationship Mapping

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 34 / 55

Page 35: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

ORM Relationship Mapping

ORM – Relationships

Employee – Project

Unidirectional

accessed from one side only

emp.getProjects()prj.getEmployees()

Bidirectional

accessed from both sidessides

empl.getProjects()prj.getEmployees()

owning side = side used forchanging the relationship

inverse side = read-only side

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 35 / 55

Page 36: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

ORM Relationship Mapping

Unidirectional many-to-one relationship I

@Entitypublic class Employee {

// ...@ManyToOneprivate Department department;// ...

}

owning side = EmployeeIn DB, the N:1 relationship is implemented using a foreign key inside theEmployee table. In this case, the foreign key has a default name.

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 36 / 55

Page 37: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

ORM Relationship Mapping

Unidirectional many-to-one relationship II

@Entitypublic class Employee {

@Idprivate int id;private String name;private long salary;@ManyToOne@JoinColumn(name="DEPT_ID")private Department department;

}

owning side = Employee.Here, the foreign key is defined using the @JoinColumn annotation.BTW what do you think about “long salary”?

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 37 / 55

Page 38: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

ORM Relationship Mapping

Bidirectional many-to-one relationship

@Entitypublic class Employee {@Idprivate int id;private String name;private long salary;@ManyToOne@JoinColumn(name="DEPT_ID")private Department department;

}

owning side = Employee

@Entitypublic class Department {@Idprivate int id;private String name;

@OneToMany(mappedBy="department")private Collection<Employee>employees;

}

inverse side = DepartmentHere, the foreign key is defined using the @JoinColumn annotation.

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 38 / 55

Page 39: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

ORM Relationship Mapping

Unidirectional one-to-one relationship

@Entitypublic class Employee {@Idprivate int id;private String name;private long salary;@OneToOne@JoinColumn(name="PSPACE_ID")private ParkingSpace parkingSpace;

}

owning side = Employee.

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 39 / 55

Page 40: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

ORM Relationship Mapping

Bidirectional one-to-one relationship

@Entitypublic class Employee {

@Idprivate int id;private String name;private long salary;@OneToOne@JoinColumn(name=”PSPACE ID”)private ParkingSpace parkingSpace;

}

owning side = Employee

@Entitypublic class ParkingSpace {

@Idprivate int id;private int lot;private String location;

@OneToOne(mappedBy=”parkingSpace”);private Employee employee;

}

inverse side = ParkingSpace

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 40 / 55

Page 41: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

ORM Relationship Mapping

Bidirectional many-to-many relationship

@Entitypublic class Employee {@Idprivate int id;private String name;private long salary;

@ManyToManyprivate Collection<Project>project;

}

owning side = Employee

@Entitypublic class Project {

@Id private int id;private String name;

@ManyToMany(mappedBy="projects");private Collection<Employee>employees;

}

inverse side = Project

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 41 / 55

Page 42: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

ORM Relationship Mapping

Conceptual Modeling Intermezzo

M:N relationship is a conceptual modeling primitive

Does it mean that

A patient has one treatment that is handled in more hospitals ?A patient has more treatments, each handled in a single hospital ?A patient has more treatments, each handled in more hospitals ?

partialities and cardinalities are not enough in this case.

Careful modeling often leads to decomposing M:N relationships on theconceptual level (not on the logical level, like JPA).

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 42 / 55

Page 43: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

ORM Relationship Mapping

Bidirectional many-to-many relationship

@Entitypublic class Employee {@Id private int id;private String Name;private long salary;@ManyToMany@JoinTable(name="EMP_PROJ",joinColumns=@JoinColumn(name="EMP_ID"),

inverseJoinColumns=@JoinColumn(name="PROJ_ID"))

private Collection<Project>projects;

}

owning side = Employee

@Entitypublic class Project {@Id private int id;private String name;

@ManyToMany(mappedBy="projects");private Collection<Employee>employees;

}

inverse side = Project

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 43 / 55

Page 44: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

ORM Relationship Mapping

Unidirectional many-to-many relationship

@Entitypublic class Employee {@Id private int id;private String Name;private long salary;@ManyToMany@JoinTable(name="EMP_PROJ",joinColumns=@JoinColumn(name="EMP_ID"),

inverseJoinColumns=@JoinColumn(name="PROJ_ID"))

private Collection<Project>projects;

}

owning side = Employee

@Entitypublic class Project {

@Id private int id;private String name;

}

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 44 / 55

Page 45: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

ORM Relationship Mapping

Unidirectional one-to-many relationship

@Entitypublic class Employee {@Id private int id;private String name;@OneToMany@JoinTable(name="EMP_PHONE",joinColumns=@JoinColumn(name="EMP_ID"),

inverseJoinColumns=@JoinColumn(name="PHONE_ID"))

private Collection<Phone> phones;}

owning side = Employee

@Entitypublic class Phone {@Id private int id;private String type;private String num;

}

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 45 / 55

Page 46: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

ORM Relationship Mapping

Unidirectional one-to-many relationship

@Entity public class Employee {@Id private int id;private String name;@OneToMany @JoinTable(name="EMP_PHONE",joinColumns=@JoinColumn(name="EMP_ID"),inverseJoinColumns=@JoinColumn(name="PHONE_ID"))

private Collection<Phone> phones;}

@Entitypublic class Phone {@Id private int id;private String type;private String num;

}

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 46 / 55

Page 47: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

ORM Relationship Mapping

Lazy Loading

@Entitypublic class Employee {@Id private int id;private String name;

private ParkingSpaceparkingSpace;

}

@Entitypublic class Employee {@Id private int id;private String name;

@OneToOne(fetch=FetchType.LAZY)private ParkingSpaceparkingSpace;

}

parkingSpace instance fetchedfrom the DB at the time of readingthe parkingSpace field.

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 47 / 55

Page 48: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

ORM Inheritance Mapping

ORM Inheritance Mapping

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 48 / 55

Page 49: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

ORM Inheritance Mapping

Inheritance

How to map inheritance into DB ?

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 49 / 55

Page 50: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

ORM Inheritance Mapping

Strategies for inheritance mappingsingle table

joined

table per class

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 50 / 55

Page 51: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

ORM Inheritance Mapping

Inheritance mapping (single-table)

@Entity@Table(name="DB_PERSON_C")@Inheritance /* same as@Inheritance(strategy=InheritanceType.SINGLE_TABLE)*/

@DiscriminationColumn(name="EMP_TYPE")public abstract class Person {...}

@Entity@DiscriminatorValue("Emp")Public class Employee extends Person {...}

@Entity@DiscriminatorValue("Stud")Public class Student extends Person {...}

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 51 / 55

Page 52: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

ORM Inheritance Mapping

Inheritance mapping (joined)

@Entity@Table(name="DB_PERSON_C")@Inheritance(strategy=InheritanceType.JOINED)@DiscriminationColumn(name="EMP_TYPE",

discriminatorType=discriminatorType.INTEGER)public abstract class Person {...}

@Entity@Table(name="DB_EMPLOYEE_C")@DiscriminatorValue("1")public class Employee extends Person {...}

@Entity@Table(name="DB_STUDENT_C")@DiscriminatorValue("2")public class Student extends Person {...}

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 52 / 55

Page 53: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

ORM Inheritance Mapping

Inheritance mapping (table-per-class)

@Entity@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)public abstract class Person { ... }

@Entity@Table(name="DB_EMPLOYEE_C")@AttributeOverride(name="name", column=@Column(name="FULLNAME"))public class Employee extends Person { ... }

@Entity@Table(name="DB_STUDENT_C")public class Student extends Person { ... }

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 53 / 55

Page 54: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

Summary

Summary

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 54 / 55

Page 55: Java Persistence API (JPA) - cvut.cz...Java Persistence API 2.1 (JSR-338) Although part of Java EE 7 speci cations, JPA 2.1 can be used both in EE and SE applications. Main topics

Summary

Summary

Don’t forget!

JPA 2 is an ORM API inJakarta EE (Java EE)

JPA 2 is a must-know forJakartaEE developers

good conceptual model is a keyto model maintainability, thencomes JPA ...

And the next week ?

Spring

THANK YOU

Petr Kremen ([email protected]) Java Persistence API (JPA) Winter Term 2020 55 / 55