Escolar Documentos
Profissional Documentos
Cultura Documentos
Week 2
Object-Relational Mapping
ACKNOWLEDGMEN
TS
The slides in this presentation are
developed from:
Chapter 5 Object-Relational Mapping (page
125 to 159)
Beginning Java EE 7
Antonio Goncalves
Apress
3
OBJECTIVES
Interpret the basics of object-relational
mapping (ORM)in terms of how attributes
and relationships are mapped from a
domain model to a database.
Develop different mappings using JPA
annotations or XML mapping descriptors.
Investigate different ways for complex
relationship mappings.
CONFIGURATION BY
EXCEPTION
Unless specified differently, the container
or provider should apply the default rules.
Having to supply a configuration is the
exception to the rule.
This allows to write the minimum amount
of code to get application running.
CONFIGURATION BY
EXCEPTION
CONFIGURATION BY
EXCEPTION
JDBC rules apply for mapping Java
primitives to relational data types.
String VARCHAR(255)
Long BIGINT
Boolean SMALLINT
TABLE
Rules for configuration-by-exception
mapping state that the entity and the
table name are the same.
You may want to map data to a different
table, or even map a single entity to
multiple tables.
The @javax.persistence.Table
annotation makes it possible to change
the default values related to the table.
The name of the table
12
TABLE
The Book entity being mapped to a T_BOOK
table
13
SECONDARY TABLE
An entity can be mapped to a single table
(primary table).
Data can be spread across multiple tables
(secondary tables).
o Using the annotation @SecondaryTable to
associate a secondary table to an entity
o Using the annotation @SecondaryTables for
several secondary tables
o Specifying for each attribute which table it
belongs to with the @Column annotation
14
SECONDARY TABLE
Attributes of the Address entity are
mapped into three different tables.
15
SECONDARY TABLE
Each table contains different attributes
but they all have the same primary key (to
join the tables together).
16
PRIMARY KEYS
In relational database a primary key
uniquely identifies each row in a table.
It comprises either a single column or set
of columns.
JPA requires entities to have an identifier
mapped to a primary key.
PRIMARY KEYS
@javax.persistence.Id annotates an
attribute as being a unique identifier.
The value of this identifier can be
generated
PRIMARY KEYS
The Book entity with an automatically
generated identifier
19
COMPOSITE PRIMARY
K EYS
It is a good practice to designate a single
dedicated column as the primary key.
There are cases where a composite
primary key is required.
For example
COMPOSITE PRIMARY
K EYS
The primary key class NewsId is composed
COMPOSITE PRIMARY
K
EYS
The entity News embeds the primary key
class with @EmbeddedId annotation.
ATTRIBUTES
A primary key (simple or composite) is a
must an entity
An entity has all sorts of different
attributes, making up its state, that have
to be mapped to the table.
This state can include almost every Java
type:
23
ATTRIBUTES
24
ATTRIBUTES
The @javax.persistence.Basic
annotation has two parameters
(optional and fetch)
ATTRIBUTES
The Track entity with Lazy loading on the
wav attribute
26
ATTRIBUTES
@javax.persistence.Column annotation
defines the properties of a column.
ATTRIBUTES
Customising mapping for the Book entity
28
ATTRIBUTES
@javax.persistence.Temporal
annotation has three possible values:
DATE, TIME, or TIMESTAMP
map java.util.Date and
java.util.Calendar
represent date, hour, or second
29
ATTRIBUTES
Annotated with @Entity, all attributes are
automatically mapped to a table.
While no need to map an attribute, use
the @javax.persistence.Transient
(@Transient) annotation or the java
transient keyword.
For example in the Customer entity,
ATTRIBUTES
The Customer entity with a Transient age
31
ATTRIBUTES
The enumeration types are now so
frequently used
The values of an enum
Are constants;
Have an implicit ordinal assignment that is
determined by the order in which they are
declared.
This ordinal cannot be modified at runtime but
can be used to store the value of the
enumerated type in the database.
32
ATTRIBUTES
In the following CreditCard entity and with
the default mapping
33
ATTRIBUTES
34
ATTRIBUTES
In the following CreditCard entity and with
the default mapping
ATTRIBUTES
Mapping an enumerated type with
String
36
ATTRIBUTES
@ElementCollection annotation is used
to indicate that an attribute contains a
collection of instances of basic types or
embeddables
java.util.Collection
java.util.Set
java.util.List
ATTRIBUTES
The Book entity with a collection of strings
as descriptive tags
38
ATTRIBUTES
Relationship between the BOOK and the
TAG tables
39
41
42
44
EMBEDDABLES
Embeddables are objects that dont have a
persistent identity on their own.
They can be embedded only within owning
entities.
The owning entity can have collections of
embeddables or a single embeddable
attribute.
They are stored as an intrinsic part of an
owning entity and share the identity of this
entity.
Each attribute of the embedded object is 45
EMBEDDABLES
The @Embeddable annotation specifies that
Address can be embedded in another
entity class (or another embeddable).
Embeddables dont have a persistent
identity on their own
46
EMBEDDABLES
The Customer entity uses the @Embedded
annotation to specify Address is a persistent
attribute that will be stored as an intrinsic
part and share its identity.
Structure of the CUSTOMER table
with all ADDRESS attributes
47
RELATIONSHIP MAPPING
Object-oriented programming abounds
with classes and associations between
classes.
These associations link objects and allow
one object to cause another to perform an
action on its behalf.
An association has a direction
Unidirectional:
o
One object points only to another object;
o
One object can navigate toward another.
Bidirectional:
o
Both objects refer to each other;
o
One object can navigate toward another
48
RELATIONSHIP MAPPING
In Java, you use the dot (.) syntax to
navigate through objects.
customer.getAddress().getCountry()
unidirectio
nal
Bidirectional
association
represented
with two
arrows
bidirection
al
49
RELATIONSHIP MAPPING
An association also has a multiplicity (or
cardinality).
Each end of an association can specify
how many referring objects are involved in
the association.
java.util.Collection, java.util.Set,
java.util.List, or even java.util.Map
50
RELATIONSHIP MAPPING
A relationship has an ownership (i.e., the
owner of the relationship)
51
RELATIONSHIP MAPPING
A relational database is a collection of
relations (also called tables anything you
model is a table).
In JPA when you have an association
between one class and another, in the
database you will get a table reference.
This reference can be modelled in two
different ways:
52
RELATIONSHIP MAPPING
A relationship using a join column or
foreign key
53
RELATIONSHIP MAPPING
You wouldnt use a join table to
represent a one-to-one relationship,
as this could have performance issues
ENTITY RELATIONSHIP
JPA makes it possible to map associations.
An entity can be linked to another in a
relational model.
JPA has a default way of storing a relation.
JPA allows to use annotation to customise
the mapping.
The cardinality between two entities may
be:
One-to-one
One-to-many
Many-to-one
Many-to-many.
55
ENTITY RELATIONSHIP
Each respective mapping is named after
the cardinality of the source and target:
56
ONE-TO-ONE
UNIDIRECTIONAL
ONE-TO-ONE
UNIDIRECTIONAL
A Customer with one Address
58
ONE-TO-ONE
UNIDIRECTIONAL
As you see in CUSTOMER and ADDRESS,
these two entities have minimum
annotation requirements: @Entity and @Id
and @GeneratedValue for keys.
The persistence provider will map these
two entities to two tables and a foreign
key for the relationship.
A one-to-one mapping is triggered by the
fact that Address is declared an entity
and included in the Customer entity as an
attribute.
59
ONE-TO-ONE
UNIDIRECTIONAL
The CUSTOMER Table with a foreign key
pointing to the ADDRESS table
60
ONE-TO-ONE
UNIDIRECTIONAL
To customise the mapping, you can use
two annotations.
@OneToOne
@JoinColumn
61
ONE-TO-ONE
UNIDIRECTIONAL
The Customer entity with customised
relationship mapping
62
ONE-TO-ONE
UNIDIRECTIONAL
In JPA, a foreign key column is called a join
column.
The @JoinColumn annotation allows to
customise the mapping of a foreign key.
The previous example:
ONE-TO-MANY
UNIDIRECTIONAL
With a one-to-many relationship, one
source object refers to an ensemble of
target objects.
For example, a purchase order is
composed of several order lines.
64
ONE-TO-MANY
UNIDIRECTIONAL
The cardinality is multiple, and the
navigation is done only from Order toward
OrderLine.
In Java, this multiplicity is described by
the Collection, List, and Set interfaces
of the java.util package.
65
ONE-TO-MANY
UNIDIRECTIONAL
An Order contains OrderLines.
66
ONE-TO-MANY
UNIDIRECTIONAL
In the previous example, a collection of an
entity type is being used as an attribute
on this entity triggers:
ONE-TO-MANY
UNIDIRECTIONAL
Join table between ORDER and ORDER_LINE
68
ONE-TO-MANY
UNIDIRECTIONAL
If you like to rename the default join table
and foreign key names or to customise
mapping, you can use JPA annotations to
redefine these default values.
ONE-TO-MANY
UNIDIRECTIONAL
70
ONE-TO-MANY
UNIDIRECTIONAL
Structure of the join table
ONE-TO-MANY
UNIDIRECTIONAL
The Order entity with a join column
72
ONE-TO-MANY
In the previous example:
UNIDIRECTIONAL
The @OneToMany annotation is overriding the
73
SUMMARY
The focus of this week is ORM.
With configuration by exception, not much
is required to map entities to tables.
JPA uses @Entity informs the persistence
provider that a class is an entity.
JPA uses @Id informs that an attribute is
its identifier.
JPA has a very rich set of annotations to
customise every little detail of ORM.
Elementary annotations can be used on
attributes or classes to customise the
mapping.
74