Title: Container Managed Relationships CMR
1Container Managed Relationships(CMR)
- Source
- Enterprise JavaBeans, 3rd Edition, Richard
Monson-Haefel
2CMR
- Enabled with the EJB 2.0
- EJB 1.1 had support for persistent fields but not
relationships - Extends the abstract schema of the Entity Bean
- Abstract accessors must be defined for CMR fields
- CMR uses only Local Interfaces
- Only EJBs deployed together in the same
deployment descriptor may have relationships with
one another - use same database and JVM
3Seven Relationship Types
- One-to-One (unidirectional, bidirectional)
- One-to-Many (unidirectional, bidirectional)
- Many-to-One (unidirectional)
- Many-to-One and One to Many bidirectional
identical - Many-to-Many (unidirectional, bidirectional)
4Defined and Initialized in the EJBClass
- public abstract class BorrowerEJB implements
EntityBean - public abstract void setId(String id)
- public abstract String getId()
- public abstract void setIdentity(PersonLocal
person) - public abstract PersonLocal getIdentity()
- //create transaction does not complete until both
the ejbCreate and ejbPostCreate are called - public String ejbCreate(PersonLocal identity)
throws BorrowerException - setId(identity.getId()) //primary key
values must be set in ejbCreate - return null
-
- public void ejbPostCreate(PersonLocal
identity) - setIdentity(identity) //relationships
must be set in ejbPostCreate -
id will be defined as a CMP Field
identity will be defined as a CMR Field
5Used in the EJB Class
- public String getName()
- return getIdentity().getFirstName() " "
getIdentity().getLastName() -
- public String getPhoneNumber()
- return getIdentity().getPhoneNumber()
-
- public String getAddress()
- return getIdentity().getAddress()
-
6Defined in ejb-jar.xml
- ltejb-jargt
- ltenterprise-beansgt
- ltentitygt
- ...
- lt/entitygt
- ...
- lt/enterprise-beansgt
- ltrelationshipsgt
- ltejb-relationgt lt!-- Borrower(0..1)-gt(1)Perso
n (unidirectional) --gt - ltejb-relation-namegtBorrower-Personlt/ejb-r
elation-namegt - ...
- lt/ejb-relationgt
- ...
- lt/relationshipsgt
- lt/ejb-jargt
7Defining Borrower-gtPersonin ejb-jar.xml
- ltejb-relationgt lt!-- Borrower(0..1)-gt(1)Person
(unidirectional) --gt - ltejb-relation-namegtBorrower-Personlt/ejb-relatio
n-namegt - ltejb-relationship-rolegt
- ltejb-relationship-role-namegtBorrower-to-Iden
titylt/ejb-relationship-role-namegt - ltmultiplicitygtOnelt/multiplicitygt
- ltrelationship-role-sourcegt
- ltejb-namegtBorrowerlt/ejb-namegt
- lt/relationship-role-sourcegt
- ltcmr-fieldgt
- ltcmr-field-namegtidentitylt/cmr-field-namegt
- lt/cmr-fieldgt
- lt/ejb-relationship-rolegt
Matches void setIdentity(PersonLocal) PersonLocal
getIdentity() from EJB Class
8Defining Person-gtBorrowerin ejb-jar.xml
- ltejb-relationship-rolegt
- ltejb-relationship-role-namegtIdentity-to-Borr
owerlt/ejb-relationship-role-namegt - ltmultiplicitygtOnelt/multiplicitygt
- ltrelationship-role-sourcegt
- ltejb-namegtPersonlt/ejb-namegt
- lt/relationship-role-sourcegt
- lt/ejb-relationship-rolegt
- lt/ejb-relationgt
Unidirectional choice signified by no cmr-field
in Person mapping back to Borrower
9ejb-jar.xml
- Every Relationship must have a name
- Every ltejb-relationgt has exactly two
ltejb-relationship-rolegt sub-elements defined - ejb-relationship-role must define a cmr-field to
be navigatable to the related object (otherwise
unidirectional)
10Stored in the Database
- create table Person (
- id varchar(12) not null unique,
- firstName varchar(32) not null,
- lastName varchar(32) not null,
- address varchar(128),
- phoneNumber char(10)
- )
- create table Borrower (
- id varchar(12) not null,
- beginDate date not null,
- endDate date,
- person_id char(12)
- )
11Mapped to the Databaseby the Implementation
- weblogic-cmp-rdbms-jar.xml
- ltweblogic-rdbms-jargt
- ltweblogic-rdbms-beangt
- ...
- lt/weblogic-rdbms-beangt
- ltweblogic-rdbms-relationgt
-
- lt/weblogic-rdbms-relationgt
- ...
- ltcreate-default-dbms-tablesgtFalselt/create-defau
lt-dbms-tablesgt - lt/weblogic-rdbms-jargt
12Mapped to the Databaseby the Implementation
- ltweblogic-rdbms-relationgt
- ltrelation-namegtBorrower-Personlt/relation-name
gt - ltweblogic-relationship-rolegt
- ltrelationship-role-namegtBorrower-to-Identi
ty - lt/relationship-role-namegt
- ltcolumn-mapgt
- ltforeign-key-columngtperson_idlt/foreign-
key-columngt - ltkey-columngtidlt/key-columngt
- lt/column-mapgt
- lt/weblogic-relationship-rolegt
- ltweblogic-relationship-rolegt
- ltrelationship-role-namegtIdentity-to-Borrow
er - lt/relationship-role-namegt
- lt/weblogic-relationship-rolegt
- lt/weblogic-rdbms-relationgt
names must match values specified in ejb-jar.xml
person_id is column in Borrower
id is column in Person
13One-to-One Unidirectional
interface PersonLocal String getId()
String getFirstName() void setFirstName(String
fname) String getLastName() void
setLastName(String lname) String
getAddress() void setAddress(String
address) String getPhoneNumber() void
setPhoneNumber(String number)
interface BorrowerLocal String getId()
String getName() String getPhoneNumber()
String getAddress() Date getBeginDate()
Date getEndDate() boolean isValid() void
terminate()
1
0..1
table Person id varchar(12) not null unique,
firstName varchar(32) not null, lastName
varchar(32) not null, address varchar(128),
phoneNumber char(10)
table Borrower id varchar(12) not null,
beginDate date not null, endDate date,
person_id varchar(12)
1
0..1
14One-to-One Unidirectional
- BorrowerEJB
- public abstract void setIdentity(PersonLocal
person) - public abstract PersonLocal getIdentity()
- ejb-jar.xml
- ltcmr-fieldgt
- ltcmr-field-namegtidentitylt/cmr-field-namegt
- lt/cmr-fieldgt
- weblogic-cmp-rdbms-jar.xml
- ltcolumn-mapgt
- ltforeign-key-columngtperson_idlt/foreign-
key-columngt - ltkey-columngtidlt/key-columngt
- lt/column-mapgt
15Not Possible to Share Object from 11 Relation
aBorrower
xPerson
bBorrower
yPerson
PersonLocal x a.getIdentity() b.setIdentity(x)
aBorrower
xPerson
bBorrower
yPerson
16One-to-One Bidirectional
interface PersonLocal String getId()
String getFirstName() void setFirstName(String
fname) String getLastName() void
setLastName(String lname) String
getAddress() void setAddress(String
address) String getPhoneNumber() void
setPhoneNumber(String number) Borrower
getBorrower()
interface BorrowerLocal String getId()
String getName() String getPhoneNumber()
String getAddress() Date getBeginDate()
Date getEndDate() boolean isValid() void
terminate()
1
0..1
table Person id varchar(12) not null unique,
firstName varchar(32) not null, lastName
varchar(32) not null, address varchar(128),
phoneNumber char(10)
table Borrower id varchar(12) not null,
beginDate date not null, endDate date,
person_id varchar(12)
1
0..1
17One-to-One Bidirectional
- BorrowerEJB
- public abstract void setIdentity(PersonLocal
person) - public abstract PersonLocal getIdentity()
- PersonEJB
- public abstract void setBorrower(BorrowerLocal
person) - public abstract BorrowerLocal getBorrower ()
- ejb-jar.xml
- ltcmr-fieldgt //from borrower to person
- ltcmr-field-namegtidentitylt/cmr-field-namegt
- lt/cmr-fieldgt
- ltcmr-fieldgt //from person to borrower
- ltcmr-field-namegtborrowerlt/cmr-field-namegt
- lt/cmr-fieldgt
- Note!!! - bidirectional and unidirectional
database schema is identical. Only EJB class and
portions of the deployment descriptor changed
18One-to-One Bidirectional (cont.)
- weblogic-cmp-rdbms-jar.xml
- ltcolumn-mapgt //from borrower to person
- ltforeign-key-columngtperson_idlt/foreign-
key-columngt - ltkey-columngtidlt/key-columngt
- lt/column-mapgt
19Not Possible to Share Object from 11 Relation
aBorrower
xPerson
bBorrower
yPerson
PersonLocal x a.getIdentity() b.setIdentity(x)
aBorrower
xPerson
bBorrower
yPerson
20One-to-Many Bidirectional
interface BorrowerLocal Set getCheckouts()
Date getBeginDate() Date getEndDate()
boolean isValid() void terminate()
interface CheckoutLocal Integer getId()
Date getOutDate() boolean isOverdue()
BorrowerLocal getBorrower()
1
table Checkout id int not null unique,
outDate longint not null, borrower_id
varchar(12)
table Borrower id varchar(12) not null,
beginDate date not null, endDate date,
person_id varchar(12)
1
21One-to-Many Bidirectional
- BorrowerEJB
- public abstract void setCheckouts(Set
checkouts) - public abstract Set getCheckouts()
- public void addCheckout(CheckoutLocal
checkout) throws CheckoutException - getCheckouts().add(checkout)
- java.util.Collection and Set are only allowable
CMR-many data types - Collection allowed to have duplicates
- Set not allowed to have duplicates
- CheckoutEJB
- public abstract void setBorrower(BorrowerLocal
borrower) - public abstract BorrowerLocal getBorrower()
22One-to-Many Bidirectional (cont.)
- ejb-jar.xml
- ltmultiplicitygtOnelt/multiplicitygt
- ltcmr-fieldgt
- ltcmr-field-namegtcheckoutslt/cmr-field-name
gt - ltcmr-field-typegtjava.util.Setlt/cmr-field-
typegt - lt/cmr-fieldgt
- ltmultiplicitygtManylt/multiplicitygt
- ltcmr-fieldgt
- ltcmr-field-namegtborrowerlt/cmr-field-namegt
- lt/cmr-fieldgt
- weblogic-cmp-rdbms-jar.xml
- ltcolumn-mapgt
- ltforeign-key-columngtborrower_idlt/foreig
n-key-columngt - ltkey-columngtidlt/key-columngt
- lt/column-mapgt
23Not Possible to Share Object from 1N Relation
aBorrower
xCheckout
yCheckout
bBorrower
zCheckout
Collection checkouts a.getCheckouts() b.setChec
kouts(checkouts)
aBorrower
xCheckout
yCheckout
bBorrower
zCheckout
24Not Possible to Share Object from 1N Relation
aBorrower
xCheckout
yCheckout
bBorrower
zCheckout
BorrowerLocal b z.getBorrower() y.setBorrower(b
)
aBorrower
xCheckout
yCheckout
bBorrower
zCheckout
25Iterating the Many Collection
- for(java.util.Iterator igetCheckouts().iterator()
i.hasNext() ) CheckoutLocal co
(CheckoutLocal)i.next()//use co -
- Must not add/remove elements from collection
while using iterator - only exception is use of i.remove()
- Empty collections are returned for no elements
26Many-to-Many Unidirectional
interface TopicLocal String getId() String
getTitle() Collection getAuthors() void
addAuthor(PersonLocal authors)
interface PersonLocal String getId()
String getFirstName()
table Topic id char(6) not null unique,
title varchar(32) not null,
1
table PersonTopicLink topic_id char(6) not
null, person_id char(6) not null
table Person id char(6) not null unique,
firstName varchar(32)
1
Use Linking Table for NN or linking legacy
tables together
27Many-to-Many Unidirectional
- TopicEJB
- public abstract void setAuthors(Collection
authors) - public abstract Collection getAuthors()
- public void addAuthor(PersonLocal author)
- getAuthors().add(author)
- ejb-jar.xml
- ltmultiplicitygtManylt/multiplicitygt
- ltcmr-fieldgt
- ltcmr-field-namegtauthorslt/cmr-field-namegt
- ltcmr-field-typegtjava.util.Collectionlt/cmr
-field-typegt - lt/cmr-fieldgt
- ltmultiplicitygtManylt/multiplicitygt
28Many-to-Many Unidirectional (cont.)
- weblogic-cmp-rdbms-jar.xml
- lttable-namegtPersonTopicLinklt/table-namegt
- ltcolumn-mapgt //from link to person table
- ltforeign-key-columngtperson_idlt/foreign-
key-columngt - ltkey-columngtidlt/key-columngt
- lt/column-mapgt
- ltcolumn-mapgt //from link to topic table
- ltforeign-key-columngttopic_idlt/foreign-k
ey-columngt - ltkey-columngtidlt/key-columngt
- lt/column-mapgt
29Objects Sharable in NN Relation
aTopic
xPerson
yPerson
bTopic
zPerson
Collection authors a.getAuthors() b.setAuthors(
authors)
aTopic
xPerson
yPerson
bTopic
zPerson
30Objects Sharable in NN Relation
aTopic
xPerson
yPerson
bTopic
zPerson
PersonLocal x a.getAuthorByName(x) b.addAutho
r(x)
aTopic
xPerson
yPerson
bTopic
zPerson
31Cascade Delete
- Causes the delete of one or more objects when a
related object is deleted - Used with one-to-one and one-to-many
relationships - entity bean that causes the cascade delete must
have a multiplicity of one - Implementation be deferred to database by
container
32Defining Cascade Deletein ejb-jar.xml
- ltrelationshipsgt
- ltejb-relationgt lt!-- Borrower(0..1)-gt(1)Perso
n (unidirectional) --gt - ltejb-relation-namegtBorrower-Personlt/ejb-r
elation-namegt - ltejb-relationship-rolegt
- ltejb-relationship-role-namegtBorrower-t
o-Identity - lt/ejb-relationship-role-namegt
- ltmultiplicitygtOnelt/multiplicitygt
- ltcascade-delete/gt lt!-- delete
Borrower role when delete Person --gt - ltrelationship-role-sourcegt
- ltejb-namegtBorrowerlt/ejb-namegt
- lt/relationship-role-sourcegt
- ltcmr-fieldgt
- ltcmr-field-namegtidentitylt/cmr-field
-namegt - lt/cmr-fieldgt
- lt/ejb-relationship-rolegt