Title: Container Managed Relationships CMR
1Container Managed Relationships(CMR)
- Source
- Enterprise JavaBeans, 3rd Edition, Richard
Monson-Haefel
2CMR
- Enabled with 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
13Mapped to the Databaseby the Implementation
- jbosscmp-jdbc.xml
- lt/jbosscmp-jdbcgt
-
- lt/enterprise-beansgt
-
- lt/enterprise-beansgt
- ltrelationshipsgt
- ltejb-relationgt
-
- lt/ejb-relationgt
- lt/relationshipsgt
- lt/jbosscmp-jdbcgt
14Mapped to the Databaseby the Implementation
- ltejb-relationgt
- ltejb-relation-namegtBorrower-Personlt/ejb-relati
on-namegt - ltforeign-key-mapping/gt
- ltejb-relationship-rolegt
- ltejb-relationship-role-namegtBorrower-to-Id
entity - lt/ejb-relationship-role-namegt
- lt/ejb-relationship-rolegt
- ltejb-relationship-rolegt
- ltejb-relationship-role-namegtIdentity-to-Bo
rrower - lt/ejb-relationship-role-namegt
- ltkey-fieldsgt
- ltkey-fieldgt
- ltfield-namegtidlt/field-namegt
- ltcolumn-namegtperson_idlt/column-nam
egt - lt/key-fieldgt
- lt/key-fieldsgt
- lt/ejb-relationship-rolegt
names must match values specified in ejb-jar.xml
id is column in Person
person_id is column in Borrower
15One-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
16One-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
- jbosscmp-jdbc.xml
- ltkey-fieldgt
- ltfield-namegtidlt/field-namegt
- ltcolumn-namegtperson_idlt/column-namegt
- lt/key-fieldgt
17Not Possible to Share Object from 11 Relation
aBorrower
xPerson
bBorrower
yPerson
PersonLocal x a.getIdentity() b.setIdentity(x)
aBorrower
xPerson
bBorrower
yPerson
18One-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
19One-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
20One-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
- jbosscmp-jdbc.xml
- ltkey-fieldgt
- ltfield-namegtidlt/field-namegt
- ltcolumn-namegtperson_idlt/column-namegt
- lt/key-fieldgt
21Not Possible to Share Object from 11 Relation
aBorrower
xPerson
bBorrower
yPerson
PersonLocal x a.getIdentity() b.setIdentity(x)
aBorrower
xPerson
bBorrower
yPerson
22One-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
23One-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()
24One-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
25One-to-Many Bidirectional (cont.)
- weblogic-cmp-rdbms-jar.xml
- ltcolumn-mapgt
- ltforeign-key-columngtborrower_idlt/foreig
n-key-columngt - ltkey-columngtidlt/key-columngt
- lt/column-mapgt
- jbosscmp-jdbc.xml
- ltkey-fieldgt
- ltfield-namegtidlt/field-namegt
- ltcolumn-namegtborrower_idlt/column-namegt
- lt/key-fieldgt
26Not 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
27Not Possible to Share Object from 1N Relation
aBorrower
xCheckout
yCheckout
bBorrower
zCheckout
BorrowerLocal b z.getBorrower() y.setBorrower(b
)
aBorrower
xCheckout
yCheckout
bBorrower
zCheckout
28Iterating 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
29Many-to-Many Unidirectional
interface TopicLocal String getId() String
getTitle() Set getAuthors() void
addAuthor(PersonLocal authors)
interface PersonLocal String getId()
String getFirstName()
table Topic id varchar(12) not null, title
varchar(32) not null,
1
table PersonTopicLink topic_id varchar(12) not
null, person_id varchar(12) not null
table Person id varchar(12) not null,
firstName varchar(32)
1
Use Linking Table for NN or linking legacy
tables together
30Many-to-Many Unidirectional
- TopicEJB
- public abstract void setAuthors(Set authors)
- public abstract Set 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.Setlt/cmr-field-
typegt - lt/cmr-fieldgt
- ltmultiplicitygtManylt/multiplicitygt
31Many-to-Many Unidirectional (cont.)weblogic-cmp-r
dbms-jar.xml
- ltweblogic-rdbms-relationgt
- ltrelation-namegtTopic-Authorlt/relation-namegt
- lttable-namegtcmrLibrary_PersonTopicLinklt/table-n
amegt - ltweblogic-relationship-rolegt
- ltrelationship-role-namegtTopic-to-Authorlt/rel
ationship-role-namegt - ltrelationship-role-mapgt
- ltcolumn-mapgt
- ltforeign-key-columngtperson_idlt/foreign
-key-columngt - ltkey-columngtidlt/key-columngt
- lt/column-mapgt
- lt/relationship-role-mapgt
- lt/weblogic-relationship-rolegt
32Many-to-Many Unidirectional (cont.)
weblogic-cmp-rdbms-jar.xml
- ltweblogic-relationship-rolegt
- ltrelationship-role-namegtAuthor-to-Topiclt/rel
ationship-role-namegt - ltrelationship-role-mapgt
- ltcolumn-mapgt
- ltforeign-key-columngttopic_idlt/foreign-
key-columngt - ltkey-columngtidlt/key-columngt
- lt/column-mapgt
- lt/relationship-role-mapgt
- lt/weblogic-relationship-rolegt
- lt/weblogic-rdbms-relationgt
33Many-to-Many Unidirectional (cont.)jbosscmp.xml
- ltejb-relationgt
- ltejb-relation-namegtTopic-Authorlt/ejb-relation-
namegt - ltrelation-table-mappinggt
- lttable-namegtcmrLibrary_PersonTopicLinklt/ta
ble-namegt - lt/relation-table-mappinggt
- ltejb-relationship-rolegt
- ltejb-relationship-role-namegt
- Topic-to-Author
- lt/ejb-relationship-role-namegt
- ltkey-fieldsgt
- ltkey-fieldgt
- ltfield-namegtidlt/field-namegt
- ltcolumn-namegtperson_idlt/column-nam
egt - lt/key-fieldgt
- lt/key-fieldsgt
- lt/ejb-relationship-rolegt
34Many-to-Many Unidirectional (cont.)jbosscmp.xml
- ltejb-relationship-rolegt
- ltejb-relationship-role-namegt
- Author-to-Topic
- lt/ejb-relationship-role-namegt
- ltkey-fieldsgt
- ltkey-fieldgt
- ltfield-namegtidlt/field-namegt
- ltcolumn-namegttopic_idlt/column-name
gt - lt/key-fieldgt
- lt/key-fieldsgt
- lt/ejb-relationship-rolegt
- lt/ejb-relationgt
35Objects Sharable in NN Relation
aTopic
xPerson
yPerson
bTopic
zPerson
Collection authors a.getAuthors() b.setAuthors(
authors)
aTopic
xPerson
yPerson
bTopic
zPerson
36Objects Sharable in NN Relation
aTopic
xPerson
yPerson
bTopic
zPerson
PersonLocal x a.getAuthorByName(x) b.addAutho
r(x)
aTopic
xPerson
yPerson
bTopic
zPerson
37Cascade 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
38Defining 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