Presentation is loading. Please wait.

Presentation is loading. Please wait.

Enterprise Java v041109Container Managed Relationships1 Container Managed Relationships (CMR) Source: “Enterprise JavaBeans, 3rd Edition”, Richard Monson-Haefel.

Similar presentations


Presentation on theme: "Enterprise Java v041109Container Managed Relationships1 Container Managed Relationships (CMR) Source: “Enterprise JavaBeans, 3rd Edition”, Richard Monson-Haefel."— Presentation transcript:

1 Enterprise Java v041109Container Managed Relationships1 Container Managed Relationships (CMR) Source: “Enterprise JavaBeans, 3rd Edition”, Richard Monson-Haefel

2 Enterprise Java v041109Container Managed Relationships2 CMR 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

3 Enterprise Java v041109Container Managed Relationships3 Seven 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)

4 Enterprise Java v041109Container Managed Relationships4 Defined 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

5 Enterprise Java v041109Container Managed Relationships5 Used in the EJB Class public String getName() { return getIdentity().getFirstName() + " " + getIdentity().getLastName(); } public String getPhoneNumber() { return getIdentity().getPhoneNumber(); } public String getAddress() { return getIdentity().getAddress(); }

6 Enterprise Java v041109Container Managed Relationships6 Defined in ejb-jar.xml...... (1)Person (unidirectional) --> Borrower-Person......

7 Enterprise Java v041109Container Managed Relationships7 Defining Borrower->Person in ejb-jar.xml (1)Person (unidirectional) --> Borrower-Person Borrower-to-Identity One Borrower identity Matches void setIdentity(PersonLocal) PersonLocal getIdentity() from EJB Class

8 Enterprise Java v041109Container Managed Relationships8 Defining Person->Borrower in ejb-jar.xml Identity-to-Borrower One Person Unidirectional choice signified by no cmr-field in Person mapping back to Borrower

9 Enterprise Java v041109Container Managed Relationships9 ejb-jar.xml Every Relationship must have a name Every has exactly two sub-elements defined –ejb-relationship-role must define a cmr-field to be navigatable to the related object (otherwise unidirectional)

10 Enterprise Java v041109Container Managed Relationships10 Stored 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) );

11 Enterprise Java v041109Container Managed Relationships11 Mapped to the Database by the Implementation weblogic-cmp-rdbms-jar.xml... …... False

12 Enterprise Java v041109Container Managed Relationships12 Mapped to the Database by the Implementation Borrower-Person Borrower-to-Identity person_id id Identity-to-Borrower names must match values specified in ejb-jar.xml id is column in Person person_id is column in Borrower

13 Enterprise Java v041109Container Managed Relationships13 Mapped to the Database by the Implementation jbosscmp-jdbc.xml … … …

14 Enterprise Java v041109Container Managed Relationships14 Mapped to the Database by the Implementation Borrower-Person Borrower-to-Identity Identity-to-Borrower id person_id names must match values specified in ejb-jar.xml id is column in Person person_id is column in Borrower

15 Enterprise Java v041109Container Managed Relationships15 One-to-One Unidirectional interface BorrowerLocal String getId(); String getName(); String getPhoneNumber(); String getAddress(); Date getBeginDate(); Date getEndDate(); boolean isValid(); void terminate(); 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); 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

16 Enterprise Java v041109Container Managed Relationships16 One-to-One Unidirectional BorrowerEJB public abstract void setIdentity(PersonLocal person); public abstract PersonLocal getIdentity(); ejb-jar.xml identity weblogic-cmp-rdbms-jar.xml person_id id jbosscmp-jdbc.xml id person_id

17 Enterprise Java v041109Container Managed Relationships17 Not Possible to Share Object from 1:1 Relation a:Borrower x:Person b:Borrower y:Person PersonLocal x = a.getIdentity(); b.setIdentity(x); a:Borrower x:Person b:Borrower y:Person

18 Enterprise Java v041109Container Managed Relationships18 One-to-One Bidirectional interface BorrowerLocal String getId(); String getName(); String getPhoneNumber(); String getAddress(); Date getBeginDate(); Date getEndDate(); boolean isValid(); void terminate(); 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() 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

19 Enterprise Java v041109Container Managed Relationships19 One-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 //from borrower to person identity //from person to borrower borrower Note!!! - bidirectional and unidirectional database schema is identical. Only EJB class and portions of the deployment descriptor changed

20 Enterprise Java v041109Container Managed Relationships20 One-to-One Bidirectional (cont.) weblogic-cmp-rdbms-jar.xml //from borrower to person person_id id jbosscmp-jdbc.xml id person_id

21 Enterprise Java v041109Container Managed Relationships21 Not Possible to Share Object from 1:1 Relation a:Borrower x::Person b:Borrower y::Person PersonLocal x = a.getIdentity(); b.setIdentity(x); a:Borrower x::Person b:Borrower y::Person

22 Enterprise Java v041109Container Managed Relationships22 One-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

23 Enterprise Java v041109Container Managed Relationships23 One-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();

24 Enterprise Java v041109Container Managed Relationships24 One-to-Many Bidirectional (cont.) ejb-jar.xml One checkouts java.util.Set Many borrower

25 Enterprise Java v041109Container Managed Relationships25 One-to-Many Bidirectional (cont.) weblogic-cmp-rdbms-jar.xml borrower_id id jbosscmp-jdbc.xml id borrower_id

26 Enterprise Java v041109Container Managed Relationships26 Not Possible to Share Object from 1:N Relation a:Borrower x:Checkout b:Borrower z:Checkout Collection checkouts = a.getCheckouts(); b.setCheckouts(checkouts); y:Checkout a:Borrower x:Checkout b:Borrower z:Checkout y:Checkout

27 Enterprise Java v041109Container Managed Relationships27 Not Possible to Share Object from 1:N Relation a:Borrower x:Checkout b:Borrower z:Checkout BorrowerLocal b = z.getBorrower(); y.setBorrower(b); y:Checkout a:Borrower x:Checkout b:Borrower z:Checkout y:Checkout

28 Enterprise Java v041109Container Managed Relationships28 Iterating the Many Collection for(java.util.Iterator i=getCheckouts().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

29 Enterprise Java v041109Container Managed Relationships29 Many-to-Many Unidirectional interface TopicLocal String getId(); String getTitle(); Set getAuthors(); void addAuthor(PersonLocal authors); interface PersonLocal String getId(); String getFirstName(); * * table Person id varchar(12) not null, firstName varchar(32) table Topic id varchar(12) not null, title varchar(32) not null, table PersonTopicLink topic_id varchar(12) not null, person_id varchar(12) not null 1 1 * * Use Linking Table for N:N or linking legacy tables together

30 Enterprise Java v041109Container Managed Relationships30 Many-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 Many authors java.util.Set Many

31 Enterprise Java v041109Container Managed Relationships31 Many-to-Many Unidirectional (cont.) weblogic-cmp-rdbms-jar.xml Topic-Author cmrLibrary_PersonTopicLink Topic-to-Author person_id id

32 Enterprise Java v041109Container Managed Relationships32 Many-to-Many Unidirectional (cont.) weblogic-cmp-rdbms-jar.xml Author-to-Topic topic_id id

33 Enterprise Java v041109Container Managed Relationships33 Many-to-Many Unidirectional (cont.) jbosscmp.xml Topic-Author cmrLibrary_PersonTopicLink Topic-to-Author id person_id

34 Enterprise Java v041109Container Managed Relationships34 Many-to-Many Unidirectional (cont.) jbosscmp.xml Author-to-Topic id topic_id

35 Enterprise Java v041109Container Managed Relationships35 Objects Sharable in N:N Relation a:Topic x:Person b:Topic z:Person Collection authors = a.getAuthors(); b.setAuthors(authors); y:Person a:Topic x:Person b:Topic z:Person y:Person

36 Enterprise Java v041109Container Managed Relationships36 Objects Sharable in N:N Relation a:Topic x:Person b:Topic z:Person PersonLocal x = a.getAuthorByName(“x”); b.addAuthor(x); y:Person a:Topic x:Person b:Topic z:Person y:Person

37 Enterprise Java v041109Container Managed Relationships37 Cascade 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

38 Enterprise Java v041109Container Managed Relationships38 Defining Cascade Delete in ejb-jar.xml (1)Person (unidirectional) --> Borrower-Person Borrower-to-Identity One Borrower identity


Download ppt "Enterprise Java v041109Container Managed Relationships1 Container Managed Relationships (CMR) Source: “Enterprise JavaBeans, 3rd Edition”, Richard Monson-Haefel."

Similar presentations


Ads by Google