0% found this document useful (0 votes)
54 views30 pages

BCD Session 07

This document discusses how to implement various types of association relationships when developing business components using EJB technologies. It examines the properties that define associations, including cardinality, direction, ownership and cascade type. It provides examples and code samples for implementing one-to-one unidirectional and bidirectional associations, one-to-many/many-to-one bidirectional associations, many-to-many bidirectional and unidirectional associations. It also discusses fetch and cascade mode settings for association mappings.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views30 pages

BCD Session 07

This document discusses how to implement various types of association relationships when developing business components using EJB technologies. It examines the properties that define associations, including cardinality, direction, ownership and cascade type. It provides examples and code samples for implementing one-to-one unidirectional and bidirectional associations, one-to-many/many-to-one bidirectional associations, many-to-many bidirectional and unidirectional associations. It also discusses fetch and cascade mode settings for association mappings.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 30

Business Component Development Using EJB Technologies

Objectives

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 associations
Implement 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

Ver. 1.0

Slide 1 of 30

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.

Ver. 1.0

Slide 2 of 30

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.

<<entity>>
Auction

Ver. 1.0

<<entity>>
Bid

<<entity>>
Item

Slide 3 of 30

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
Ownership
Cascade type

Ver. 1.0

Slide 4 of 30

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:
Bidirectional
Unidirectional

Ver. 1.0

Slide 5 of 30

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 of


an operation to associated entities and is expressed using
one of the following terms:
All
Persist
Merge
Remove
Refresh
None

Ver. 1.0

Slide 6 of 30

Business Component Development Using EJB Technologies


Examples of Association Relationships

The following table shows the seven possible


cardinality-direction combinations.

Ver. 1.0

Cardinality

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

Slide 7 of 30

Business Component Development Using EJB Technologies


Examples of Association Relationships (Contd.)

The following figure shows an example of a one-to-one


bidirectional relationship.
Car_1

Engine_1

The following figure shows an example of a one-to-one


unidirectional relationship.

Auction_1

Ver. 1.0

Item_1

Slide 8 of 30

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.
Auction_1

Bid_1

The following figure shows an example of a one-to-many


unidirectional relationship.
Order_1

OrderItem_1

Ver. 1.0

Slide 9 of 30

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

Ver. 1.0

Slide 10 of 30

Business Component Development Using EJB Technologies


Examples of Association Relationships (Contd.)

The following figure shows an example of many-to-many


bidirectional relationship.
Company_1

Employee_1

Employee_2
Company_2
Employee_3

Company_3

Ver. 1.0

Employee_4

Slide 11 of 30

Business Component Development Using EJB Technologies


Examples of Association Relationships (Contd.)

The following figure shows an example of many-to-many


unidirectional relationship.
Order_1

InventoryItem_1

InventoryItem_2
Order_2
InventoryItem_3

Order_3

Ver. 1.0

InventoryItem_4

Slide 12 of 30

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.

Ver. 1.0

Slide 13 of 30

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
2
3
4
5
6
7

Ver. 1.0

@Entity
public class Customer {
@Id
private int id;
@OneToOne
private Record custRecord;
..//
}

Slide 14 of 30

Business Component Development Using EJB Technologies


Implementing One-to-One Unidirectional Association (Contd.)

The following figure shows the table with join column


matching the corresponding entities.

Ver. 1.0

Slide 15 of 30

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.

Ver. 1.0

Slide 16 of 30

Business Component Development Using EJB Technologies


Implementing One-to-One Bidirectional Association (Contd.)

The following code shows a bidirectional one-to-one


association owning entity example:
1
2
3
4
5
6
7
8

Ver. 1.0

@Entity
public class Customer {
@Id
private int id;
@OneToOne
private Record custRecord;
..//
}

Slide 17 of 30

Business Component Development Using EJB Technologies


Implementing One-to-One Bidirectional Association (Contd.)

The following table shows the join columns matching with


the corresponding entities.

Ver. 1.0

Slide 18 of 30

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.

Ver. 1.0

Slide 19 of 30

Business Component Development Using EJB Technologies


Implementing One-to-Many/Many-to-One Bidirectional Association (Contd.)

The following code shows a bidirectional one-to-many


relationship:
1
2
3
4
5
6
7
8

Ver. 1.0

@Entity
public class Customer {
@Id
private int id;
@OneToMany(mappedBy=customer)
private Collection <Order> orders;
..//
}

Slide 20 of 30

Business Component Development Using EJB Technologies


Implementing One-to-Many/Many-to-One Bidirectional Association (Contd.)

The following code shows a bidirectional many-to-many


relationship:
1
2
3
4
5
6
7
8

Ver. 1.0

@Entity
public class Order {
@Id
private int orderId;
@ManyToOne
private Customer customer;
..//
}

Slide 21 of 30

Business Component Development Using EJB Technologies


Implementing One-to-Many/Many-to-One Bidirectional Association (Contd.)

The following figure shows the table with join columns


matching the corresponding entities.

Ver. 1.0

Slide 22 of 30

Business Component Development Using EJB Technologies


Implementing Many-to-Many Bidirectional Association

The following figure shows many-to-many bidirectional


association between two entities.

Ver. 1.0

Slide 23 of 30

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
2
3
4
5
6
7
8

Ver. 1.0

@Entity
public class Worker {
@Id
private int id;
@ManyToMany
private Set <Project> projects;
..//
}

Slide 24 of 30

Business Component Development Using EJB Technologies


Implementing Many-to-Many Bidirectional Association (Contd.)

The following figure shows the table with join columns


matching the corresponding entities for one-to-many/
many-to-one bidirectional association.

Ver. 1.0

Slide 25 of 30

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

Ver. 1.0

Slide 26 of 30

Business Component Development Using EJB Technologies


Fetch Mode Attribute

The following table lists the default values for annotations.

Ver. 1.0

Annotation

Default

Basic

EAGER

OneToOne

EAGER

ManyToOne

EAGER

OneToMany

LAZY

ManyToMany

LAZY

Slide 27 of 30

Business Component Development Using EJB Technologies


Cascade Mode Attribute

Cascade mode settings are used when the EntityManager


API is invoked.
The following table describes the possible values for the
cascade attribute.

Ver. 1.0

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.

Slide 28 of 30

Business Component Development Using EJB Technologies


Summary

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

Ver. 1.0

Slide 29 of 30

Business Component Development Using EJB Technologies


Summary (Contd.)

The Cascade property is expressed using one of the following


terms:
All
Persist
Merge
Remove
Refresh
None

The Association mapping annotations attributes are:


Fetch mode
Cascade mode

The container interprets the fetch mode setting as:


EAGER
LAZY

Ver. 1.0

Slide 30 of 30

You might also like