Upload
christian-marshall
View
222
Download
1
Tags:
Embed Size (px)
Citation preview
15Copyright © 2005, Oracle. All rights reserved.
Container-Managed Relationships (CMRs)
15-2 Copyright © 2005, Oracle. All rights reserved.
Objectives
After completing this lesson, you should be able to do the following:
• Define relationships between entity beans
• Determine the cardinality and direction of a relationship
• Identify various elements of the deployment descriptor that define the relationship
• Develop CMR entity beans
15-3 Copyright © 2005, Oracle. All rights reserved.
Relationships
15-4 Copyright © 2005, Oracle. All rights reserved.
Implementing Relationships
• Data manipulation in the beans may involve a more complex data model, with a relationship between entity beans.
• The important aspects of a relationship are:– Cardinality: Number of data instances that can
participate in a relationship
– Direction: Direction in which a relationship is navigated
15-5 Copyright © 2005, Oracle. All rights reserved.
Cardinality and Direction of Relationships
• Cardinality:– One-to-one– One-to-many or many-to-one– Many-to-many
• Directionality:– Unidirectional– Bidirectional
15-7 Copyright © 2005, Oracle. All rights reserved.
One-to-One Relationships
• The phone_num column is the foreign key in the EMP entity that is referencing the PHONE entity.
• Each employee has only one phone number and each phone number belongs to only one employee.
111 1111Smith100
phone_numnameemp_id
MCI650111 1111
servicearea_codephone_num
EMP
PHONE
15-8 Copyright © 2005, Oracle. All rights reserved.
One-to-Many Relationships
Each department exists in only one location identified by location_id, whereas each location can contain many departments (1:M).
…Seattle1700
…citylocation_id
DEPARTMENTS
LOCATIONS
1700Administration10
location_iddepartment_namedepartment_id
1700Purchasing30
15-9 Copyright © 2005, Oracle. All rights reserved.
Many-to-Many Relationships
Each employee may be assigned many jobs and each job may be assigned to many employees.
SA_REP176A1
job_idemployee_idassign_pkASSIGNMENTS
EMPLOYEES
Jonathon176
Nameemployee_id
JOBS_HISTORY
...SA_REP
...job_id
15-10 Copyright © 2005, Oracle. All rights reserved.
Oracle TopLink
Oracle TopLink run-time framework:
• Facilitates data integration with enterprise applications
• Facilitates rapid development, deployment, and execution of all persistence-related aspects of any Java application
SQL
RowsJava
application
JDBCTopLink
15-11 Copyright © 2005, Oracle. All rights reserved.
TopLink: Integration of J2EE Applications with Data Sources at Run Time
DB2
SQL server
EIS
Oracle
XML
Data mapping and integration
EJBs
Servlets/JSPs
J2EEApplication Server
15-12 Copyright © 2005, Oracle. All rights reserved.
TopLink: Integrated with Oracle JDeveloper 10g
15-13 Copyright © 2005, Oracle. All rights reserved.
Implementing Relationships
• Relationship restrictions:– Define only for CMP 2.0 entity beans– Declare related CMP beans in one deployment
descriptor– Use only local interfaces to define the relationship
• Define abstract get() and set() accessor methods in the bean class for each relationship field
• In the deployment descriptor, define:– Name of each relationship– Cardinality and direction for each relationship– One section for each side of a bidirectional
relationship– Cascade delete operations on one side of the
relationship
15-14 Copyright © 2005, Oracle. All rights reserved.
Defining Abstract Accessor Methods
• Name: – get<cmrfieldname> and set<cmrfieldname> – Example: getDepartment(),
setEmployees(EmpLocal emp)
• Return type: – Local interface of the target bean for single object
retrieval– Collection or set of local interface objects for
multiple object retrieval
• Location where it is defined:– Only in the source bean for unidirectional
relationship– In both beans for bidirectional relationship
15-15 Copyright © 2005, Oracle. All rights reserved.
Accessor Methods in 1:1 Relationships
For bidirectional relationships, define abstract get() and set() accessor methods for CMR fields in both the abstract bean classes.
public abstract class Emp implements EntityBean {...public abstract PhoneLocal getPhone_PhoneNo();public abstract void setPhone_PhoneNo(PhoneLocal p);...
public abstract class Phone implements EntityBean {...public abstract EmpLocal getEmp_EmployeeID();public abstract void setEmp_EmployeeID(EmpLocal e);...
15-16 Copyright © 2005, Oracle. All rights reserved.
Accessor Methods in 1:M Relationships
public abstract class DepartmentsBean implementsEntityBean{...public abstract LocationsLocal getLocations_locationId();public abstract void setLocations_locationId (LocationsLocal locations_locationId);...
public abstract class LocationsBean implements EntityBean {...public abstract Collection getDepartments_locationId();public abstract void setDepartments_locationId(Collection departments_locationId);...
15-17 Copyright © 2005, Oracle. All rights reserved.
Checking Relationship Mappings in JDeveloper
15-18 Copyright © 2005, Oracle. All rights reserved.
Accessor Methods in M:N Relationships
public abstract class EmployeesBean implementsEntityBean{ ...public abstract Collection getJobHistory_employeeId();public abstract void setJobHistory_employeeId(Collection jobHistory_employeeId);... }
public abstract class JobHistoryBean implements EntityBean { ...public abstract Collection getEmployees_employeeId();public abstract void setEmployees_employeeId(Collection employees_employeeId);... }
15-19 Copyright © 2005, Oracle. All rights reserved.
Implementing a Relationship inthe Deployment Descriptor
15-21 Copyright © 2005, Oracle. All rights reserved.
Implementing 1:1 Relationships
... <relationships> <ejb-relation> <ejb-relation-name>EMP-PHONE</ejb-relation-name>
<ejb-relationship-role> <ejb-relationship-role-name>
Emp-has-Phone </ejb-relationship-role-name> <multiplicity>One</multiplicity> <relationship-role-source>
<ejb-name>Emp</ejb-name> </ relationship-role-source>
<cmr-field><cmr-field-name>Phone</cmr-field-name><cmr-field-type>PhoneLocal</cmr-field-type>
</cmr-field> </ejb-relationship-role>
...
Emp to Phone
15-22 Copyright © 2005, Oracle. All rights reserved.
Implementing 1:1 Relationships
... <ejb-relationship-role>
<ejb-relationship-role-name>Phone-has-Emp
</ejb-relationship-role-name> <multiplicity>One</multiplicity>
<cascade-delete /> <relationship-role-source>
<ejb-name>Phone</ejb-name> </ relationship-role-source>
<cmr-field><cmr-field-name>Emp</cmr-field-name><cmr-field-type>EmpLocal</cmr-field-type>
</cmr-field> </ejb-relationship-role></ejb-relation>
</relationships> ...
Phone to Emp
15-23 Copyright © 2005, Oracle. All rights reserved.
Implementing 1:M Relationships
<relationships> <ejb-relation> <ejb-relation-name>Departments-Locations
</ejb-relation-name> <ejb-relationship-role>
<ejb-relationship-role-name>Locations-has- departments_locationId </ejb-relationship-role-name>
<multiplicity>One</multiplicity> <relationship-role-source> <ejb-name>Locations</ejb-name> </relationship-role-source> <cmr-field> <cmr-field-name>departments_locationId
</cmr-field-name> <cmr-field-type>java.util.Collection
</cmr-field-type> </cmr-field> </ejb-relationship-role>...
Departments to Locations
15-24 Copyright © 2005, Oracle. All rights reserved.
Implementing 1:M Relationships
... <ejb-relationship-role> <ejb-relationship-role-name>Departments-owned-by-
locations_locationId </ejb-relationship-role-name> <multiplicity>Many</multiplicity> <relationship-role-source> <ejb-name>Departments</ejb-name> </relationship-role-source> <cmr-field> <cmr-field-name>locations_locationId
</cmr-field-name> </cmr-field> </ejb-relationship-role>...
Locations to Departments
15-25 Copyright © 2005, Oracle. All rights reserved.
Implementing M:N Relationships
... <ejb-relation> <ejb-relation-name>JobHistory – Employees
</ejb-relation-name> <ejb-relationship-role> <ejb-relationship-role-name>
JobHistory-has-employees_employeeId </ejb-relationship-role-name>
<multiplicity>Many</multiplicity> <relationship-role-source> <ejb-name>JobHistory</ejb-name> </relationship-role-source> <cmr-field> <cmr-field-name>employees_employeeId
</cmr-field-name> <cmr-field-type>java.util.Collection
</cmr-field-type> </cmr-field> </ejb-relationship-role> ...
15-26 Copyright © 2005, Oracle. All rights reserved.
Implementing M:N Relationships
<ejb-relationship-role> <ejb-relationship-role-name>
Employees-has-jobHistory_employeeId </ejb-relationship-role-name>
<multiplicity>Many</multiplicity> <relationship-role-source> <ejb-name>Employees</ejb-name> </relationship-role-source> <cmr-field> <cmr-field-name>jobHistory_employeeId
</cmr-field-name> <cmr-field-type>java.util.Collection
</cmr-field-type> </cmr-field> </ejb-relationship-role>
15-27 Copyright © 2005, Oracle. All rights reserved.
Mapping Relationship Fields to Database
• Accept default mapping provided by container (specify relationship fields in ejb-jar.xml)
• Explicitly map the fields to existing table columns in orion-ejb-jar.xml
15-28 Copyright © 2005, Oracle. All rights reserved.
Default Mapping of Relationship Fields
• Database: Specified in data-sources.xml• Each table in relationship: Same as for CMP entity
• Columns in each table: Based on <cmr-field>, which represents a bean in the relationship
• For each <cmr-field> of an entity, the container creates a foreign key to the primary key of the related entity:– In the source bean table for one-to-one mapping– In a container-generated association table for one-
to-many and many-to-many relationships
• User-defined or automatically generated primary key
15-30 Copyright © 2005, Oracle. All rights reserved.
Explicit Mapping of Relationship Fields
1. Deploy your application with only the ejb-jar.xml elements configured.
2. Copy the container-created orion-ejb-jar.xml file to your development environment.
3. Modify the <entity-deployment> element in the orion-ejb-jar.xml file to use the database table and columns that you specify.
4. Rearchive and redeploy your application.
15-31 Copyright © 2005, Oracle. All rights reserved.
Using JDeveloper to Create CMR Beans
Using JDeveloper, you can create a container-managed relationship between entity beans in the following ways:
• Create CMP beans from tables and generate default relationships between entities.
• Use the EJB Module Editor to specify the relationship or use TopLink Mapping Editor.
• Create an EJB Diagram for CMP beans and generate default relationships.
15-32 Copyright © 2005, Oracle. All rights reserved.
Summary
In this lesson, you should have learned how to:
• Determine the cardinality and direction of a relationship
• Define and implement relationships between CMR entity beans
15-33 Copyright © 2005, Oracle. All rights reserved.
Practice 15: Overview
This practice covers creating a one-to-many relationship between the Employees and Departments entity beans.