Presentation is loading. Please wait.

Presentation is loading. Please wait.

15 Copyright © 2005, Oracle. All rights reserved. Container-Managed Relationships (CMRs)

Similar presentations


Presentation on theme: "15 Copyright © 2005, Oracle. All rights reserved. Container-Managed Relationships (CMRs)"— Presentation transcript:

1 15 Copyright © 2005, Oracle. All rights reserved. Container-Managed Relationships (CMRs)

2 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

3 15-3 Copyright © 2005, Oracle. All rights reserved. Relationships

4 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

5 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

6 15-6 Copyright © 2005, Oracle. All rights reserved. Cardinality and Direction of Relationships

7 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

8 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

9 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_pk ASSIGNMENTS EMPLOYEES Jonathon176 Nameemployee_id JOBS_HISTORY...SA_REP...job_id

10 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 Rows Java application JDBC TopLink

11 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 J2EE Application Server

12 15-12 Copyright © 2005, Oracle. All rights reserved. TopLink: Integrated with Oracle JDeveloper 10g

13 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

14 15-14 Copyright © 2005, Oracle. All rights reserved. Defining Abstract Accessor Methods Name: – get and set –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-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);...

16 15-16 Copyright © 2005, Oracle. All rights reserved. Accessor Methods in 1:M Relationships public abstract class DepartmentsBean implements EntityBean {... 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);...

17 15-17 Copyright © 2005, Oracle. All rights reserved. Checking Relationship Mappings in JDeveloper

18 15-18 Copyright © 2005, Oracle. All rights reserved. Accessor Methods in M:N Relationships public abstract class EmployeesBean implements EntityBean {... 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);... }

19 15-19 Copyright © 2005, Oracle. All rights reserved. Implementing a Relationship in the Deployment Descriptor

20 15-20 Copyright © 2005, Oracle. All rights reserved. Implementing a Relationship in the Deployment Descriptor

21 15-21 Copyright © 2005, Oracle. All rights reserved. Implementing 1:1 Relationships... EMP-PHONE Emp-has-Phone One Emp Phone PhoneLocal... Emp to Phone

22 15-22 Copyright © 2005, Oracle. All rights reserved. Implementing 1:1 Relationships... Phone-has-Emp One Phone Emp EmpLocal... Phone to Emp

23 15-23 Copyright © 2005, Oracle. All rights reserved. Implementing 1:M Relationships Departments-Locations Locations-has- departments_locationId One Locations departments_locationId java.util.Collection... Departments to Locations

24 15-24 Copyright © 2005, Oracle. All rights reserved. Implementing 1:M Relationships... Departments-owned-by- locations_locationId Many Departments locations_locationId... Locations to Departments

25 15-25 Copyright © 2005, Oracle. All rights reserved. Implementing M:N Relationships... JobHistory – Employees JobHistory-has-employees_employeeId Many JobHistory employees_employeeId java.util.Collection...

26 15-26 Copyright © 2005, Oracle. All rights reserved. Implementing M:N Relationships Employees-has-jobHistory_employeeId Many Employees jobHistory_employeeId java.util.Collection

27 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

28 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, which represents a bean in the relationship For each 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

29 15-29 Copyright © 2005, Oracle. All rights reserved. Default Mapping of Relationship Fields

30 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 element in the orion-ejb-jar.xml file to use the database table and columns that you specify. 4.Rearchive and redeploy your application.

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

32 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

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

34 15-34 Copyright © 2005, Oracle. All rights reserved.

35 15-35 Copyright © 2005, Oracle. All rights reserved.

36 15-36 Copyright © 2005, Oracle. All rights reserved.

37 15-37 Copyright © 2005, Oracle. All rights reserved.

38 15-38 Copyright © 2005, Oracle. All rights reserved.


Download ppt "15 Copyright © 2005, Oracle. All rights reserved. Container-Managed Relationships (CMRs)"

Similar presentations


Ads by Google