Download - BCD Session 07
-
8/10/2019 BCD Session 07
1/30
Slide 1 of 30Ver. 1.0
Business Component Development Using EJB Technologies
In this session, you will learn to:
Examine association relationships in the data and object
models
Use relationship properties to define associations
Implement one-to-one unidirectional associationsImplement one-to-one bidirectional associations
Implement many-to-one/one-to-many bidirectional associations
Implement many-to-many bidirectional associations
Implement many-to-many unidirectional associations
Examine fetch and cascade mode settings
Objectives
-
8/10/2019 BCD Session 07
2/30
Slide 2 of 30Ver. 1.0
Business Component Development Using EJB Technologies
Examining Association Relationships in Data and Object Models
The following figure shows some of the relationships within
the data model that represents the auction application in the
EIS tier.
-
8/10/2019 BCD Session 07
3/30
Slide 3 of 30Ver. 1.0
Business Component Development Using EJB Technologies
Examining Association Relationships in Data and Object Models (Contd.)
The following figure shows the object model that is used to
represent the auction application in the middle tier.
Auction
Bid
Item
1 1
1 *
-
8/10/2019 BCD Session 07
4/30
Slide 4 of 30Ver. 1.0
Business Component Development Using EJB Technologies
Using Relationship Properties to Define Associations
The properties that describe association relationships
between objects in the object model are:
Cardinality
Direction
OwnershipCascade type
-
8/10/2019 BCD Session 07
5/30
Slide 5 of 30Ver. 1.0
Business Component Development Using EJB Technologies
Using Relationship Properties to Define Associations (Contd.)
Cardinality specifies the quantity relationship between two
entities in a relationship and it is expressed using one of the
following annotations:
OneToOne
ManyToOne
OneToMany
ManyToMany
Direction implies navigation or visibility and is expressed
using one of the following terms:
BidirectionalUnidirectional
-
8/10/2019 BCD Session 07
6/30
Slide 6 of 30Ver. 1.0
Business Component Development Using EJB Technologies
Using Relationship Properties to Define Associations (Contd.)
Ownership specifies the owning side of the relationship. The
different types of ownerships are:
Owning side
Inverse side
Cascade property specifies the propagation of the effect ofan operation to associated entities and is expressed using
one of the following terms:
All
Persist
MergeRemove
Refresh
None
-
8/10/2019 BCD Session 07
7/30Slide 7 of 30Ver. 1.0
Business Component Development Using EJB Technologies
Examples of Association Relationships
The following table shows the seven possible
cardinality-direction combinations.
Cardinali ty Direction
One-to-one Bidirectional
One-to-one Unidirectional
One-to-many Bidirectional
One-to-many Unidirectional
Many-to-one Unidirectional
Many-to-many Bidirectional
Many-to-many Unidirectional
-
8/10/2019 BCD Session 07
8/30Slide 8 of 30Ver. 1.0
Business Component Development Using EJB Technologies
Examples of Association Relationships (Contd.)
The following figure shows an example of a one-to-one
bidirectional relationship.
The following figure shows an example of a one-to-one
unidirectional relationship.
Car_1 Engine_1
Auction_1 Item_1
-
8/10/2019 BCD Session 07
9/30Slide 9 of 30Ver. 1.0
Business Component Development Using EJB Technologies
Examples of Association Relationships (Contd.)
The following figure shows an example of a many-to-one/
one-to-many bidirectional relationship.
The following figure shows an example of a one-to-many
unidirectional relationship.
Auction_1
Bid_1
Order_1
OrderItem_1
-
8/10/2019 BCD Session 07
10/30Slide 10 of 30Ver. 1.0
Business Component Development Using EJB Technologies
Examples of Association Relationships (Contd.)
The following figure shows an example of a many-to-one
unidirectional relationships.
AccountType_1
Account_1
-
8/10/2019 BCD Session 07
11/30Slide 11 of 30Ver. 1.0
Business Component Development Using EJB Technologies
Examples of Association Relationships (Contd.)
The following figure shows an example of many-to-many
bidirectional relationship.
Employee_1
Employee_2
Employee_3
Employee_4Company_3
Company_2
Company_1
-
8/10/2019 BCD Session 07
12/30Slide 12 of 30Ver. 1.0
Business Component Development Using EJB Technologies
The following figure shows an example of many-to-many
unidirectional relationship.
Examples of Association Relationships (Contd.)
InventoryItem_1
Order_3
Order_2
Order_1
InventoryItem_2
InventoryItem_3
InventoryItem_4
-
8/10/2019 BCD Session 07
13/30Slide 13 of 30Ver. 1.0
Business Component Development Using EJB Technologies
Implementing One-to-One Unidirectional Association
The following figure shows a one-to-one unidirectional
association between two entities.
-
8/10/2019 BCD Session 07
14/30Slide 14 of 30Ver. 1.0
Business Component Development Using EJB Technologies
Implementing One-to-One Unidirectional Association (Contd.)
The following code shows an example of the owning side of
a unidirectional one-to-one relationship:1 @Entity
2 public class Customer {
3 @Id
4 private int id;
5 @OneToOne
6 private Record custRecord;
7 ..// }
-
8/10/2019 BCD Session 07
15/30Slide 15 of 30Ver. 1.0
Business Component Development Using EJB Technologies
The following figure shows the table with join column
matching the corresponding entities.
Implementing One-to-One Unidirectional Association (Contd.)
-
8/10/2019 BCD Session 07
16/30Slide 16 of 30Ver. 1.0
Business Component Development Using EJB Technologies
Implementing One-to-One Bidirectional Association
The following figure shows a one-to-one bidirectional
association between two entities.
-
8/10/2019 BCD Session 07
17/30Slide 17 of 30Ver. 1.0
Business Component Development Using EJB Technologies
The following code shows a bidirectional one-to-one
association owning entity example:1 @Entity
2 public class Customer {
3 @Id
4 private int id;
5 @OneToOne
6 private Record custRecord;
7 ..//
8 }
Implementing One-to-One Bidirectional Association (Contd.)
-
8/10/2019 BCD Session 07
18/30Slide 18 of 30Ver. 1.0
Business Component Development Using EJB Technologies
The following table shows the join columns matching with
the corresponding entities.
Implementing One-to-One Bidirectional Association (Contd.)
-
8/10/2019 BCD Session 07
19/30Slide 19 of 30Ver. 1.0
Business Component Development Using EJB Technologies
Implementing One-to-Many/Many-to-One Bidirectional Association
The following figure shows a one-to-many/many to one
bidirectional association between two entities.
-
8/10/2019 BCD Session 07
20/30Slide 20 of 30Ver. 1.0
Business Component Development Using EJB Technologies
The following code shows a bidirectional one-to-many
relationship:1 @Entity
2 public class Customer {
3 @Id
4 private int id;
5 @OneToMany(mappedBy=customer)
6 private Collection orders;
7 ..//
8 }
Implementing One-to-Many/Many-to-One Bidirectional Association (Contd.)
-
8/10/2019 BCD Session 07
21/30Slide 21 of 30Ver. 1.0
Business Component Development Using EJB Technologies
The following code shows a bidirectional many-to-many
relationship:1 @Entity
2 public class Order {
3 @Id
4 private int orderId;
5 @ManyToOne
6 private Customer customer;
7 ..//
8 }
Implementing One-to-Many/Many-to-One Bidirectional Association (Contd.)
-
8/10/2019 BCD Session 07
22/30Slide 22 of 30Ver. 1.0
Business Component Development Using EJB Technologies
The following figure shows the table with join columns
matching the corresponding entities.
Implementing One-to-Many/Many-to-One Bidirectional Association (Contd.)
-
8/10/2019 BCD Session 07
23/30
Slide 23 of 30Ver. 1.0
Business Component Development Using EJB Technologies
Implementing Many-to-Many Bidirectional Association
The following figure shows many-to-many bidirectional
association between two entities.
-
8/10/2019 BCD Session 07
24/30
Slide 24 of 30Ver. 1.0
Business Component Development Using EJB Technologies
Implementing Many-to-Many Bidirectional Association (Contd.)
The following code shows an example of the owning side of
a bidirectional many-to-many relationship:1 @Entity
2 public class Worker {
3 @Id
4 private int id;
5 @ManyToMany
6 private Set projects;
7 ..//
8 }
-
8/10/2019 BCD Session 07
25/30
Slide 25 of 30Ver. 1.0
Business Component Development Using EJB Technologies
The following figure shows the table with join columns
matching the corresponding entities for one-to-many/
many-to-one bidirectional association.
Implementing Many-to-Many Bidirectional Association (Contd.)
-
8/10/2019 BCD Session 07
26/30
Slide 26 of 30Ver. 1.0
Business Component Development Using EJB Technologies
Examining Fetch and Cascade Mode Settings
The Association mapping annotations attributes are:
Fetch mode
Cascade mode
All annotations that specify association mappings have a
fetch mode attribute.The container interprets the fetch mode setting as:
EAGER
LAZY
-
8/10/2019 BCD Session 07
27/30
Slide 27 of 30Ver. 1.0
Business Component Development Using EJB Technologies
Fetch Mode Attribute
The following table lists the default values for annotations.
Annotat ion Default
Basic EAGER
OneToOne EAGER
ManyToOne EAGER
OneToMany LAZY
ManyToMany LAZY
-
8/10/2019 BCD Session 07
28/30
Slide 28 of 30Ver. 1.0
Business Component Development Using EJB Technologies
Cascade mode settings are used when the EntityManagerAPI is invoked.
The following table describes the possible values for the
cascade attribute.
Cascade Mode Attribute
Value Comment
PERSIST Causes the persist operation to cascade to the target entity of the association
when the entity managers persist operation is invoked on the source entity.
MERGE Causes the merge operation to cascade to the target entity of the association
when the entity managers merge operation is invoked on the source entity.
REMOVE Causes the remove operation to cascade to the target entity of the association
when the entity managers remove operation is invoked on the source entity.
REFRESH Causes the refresh operation to cascade to the target entity of the association
when the entity managers refresh operation is invoked on the source entity.
ALL Causes all the entity state change operations (persist, merge, remove, and
refresh) to cascade to the target entity of the association.
-
8/10/2019 BCD Session 07
29/30
Slide 29 of 30Ver. 1.0
Business Component Development Using EJB Technologies
In this session, you learned that:
The properties that describe association relationships between
objects in the object model are:
Cardinality
Direction
Ownership
Cascade type
The different types of ownerships are:
Owning side
Inverse side
Summary
-
8/10/2019 BCD Session 07
30/30
Business Component Development Using EJB Technologies
The Cascade propertyis expressed using one of the following
terms:
All
Persist
Merge
RemoveRefresh
None
The Association mapping annotations attributes are:
Fetch mode
Cascade mode
The container interprets the fetch mode setting as:
EAGER
LAZY
Summary (Contd.)