You define a basic CDS view entity for departments and the associations between departments and employees.
Prerequisites
For this exercise, you need the CDS view entity for employee data which you created in a previous exercise (suggested name was: Z##_R_Employee, where ## is your group number). If you have not finished that exercise, create a copy of data definition /LRN/R_EMPLOYEE_ANN.
Task 1: Define a CDS View Entity for Department Data
Define a CDS view entity for department data (suggested name: Z##_R_Department, where ## is your group number). Let the new CDS view entity read all fields from the database table that you created in the previous exercise (suggested name was: Z##DEPMENT).
Note
If you have not finished the previous exercise, you can let your CDS view entity read the database table /LRN/DEPMENT_REL.Steps
In your own package, create a new data definition (suggested name: Z##_R_DEPARTMENT, where ## is your group number). Specify your database table as Referenced Object and choose the Define View Entity template to generate the definition statement, some standard annotations and the element list.
In the Project Explorer view, right-click your database table definition Z##DEPMENT to open the context menu.
From the context menu, choose New Data Definition.
Confirm that the Package field contains the name of your package and that the Referenced Object field contains the name of your database table definition.
In the Name field, enter the name for the CDS view entity (Z##_R_Department, where ## is your group number).
Enter Department (Entity) in the Description field and choose Next.
Confirm the transport request and choose Next.
Caution
Make sure you don't choose Finish yet. If you do, you are not able to choose the template that you want to use.From the list of Templates, choose Define View Entity, then choose Finish.
Apply source code formatting.
From the eclipse menu, choose Source Code → Format. Alternatively, choose Shift + F1.
Activate the data definition.
Press Ctrl + F3 to activate the development object.
Task 2: Define an Association
In your basic CDS view entity for employee data (suggested name was: Z##_R_Employee), define and expose an association using your new CDS view entity for department data as target (suggested association name: _Department). Choose a cardinality that matches the cardinality used in the foreign key relationship for the DEPARTMENT_ID table field.
Steps
Open the basic data definition for employee data (suggested name was: Z##_R_EMPLOYEE).
Press Ctrl + Shift + A.
Enter Z##_R_.
From the list of matching items, choose Z##_R_EMPLOYEE (Data Definition) and choose Next.
Define an association to your CDS view entity for department data with association name _Department.
Adjust the code as follows:
12345
define view entity Z##_R_Employee
as select from z##employ
association to Z##_R_Department as _Department
Define the ON-condition, using the department ID field in both view entities.
Hint
Make use of the $projection. prefix to address the element of the source view entity and the_Department. prefix to address the element of the target view entity.Adjust the code as follows:
123456
define view entity Z##_R_Employee
as select from z##employ
association to Z##_R_Department as _Department
on $projection.DepartmentId = _Department.Id
Add a cardinality that matches the cardinality of the foreign key relationship for the DEPARTMENT_ID table field.
Note
The cardinality of the foreign key relationship is 0..*,1. The first part specifies the number employees per department. The second part expresses the number of departments per employee.Adjust the code as follows:
123456
define view entity Z##_R_Employee
as select from z##employ
association[1..1] to Z##_R_Department as _Department
on $projection.DepartmentId = _Department.Id
Expose the association.
At the end of the element list, add a comma and the association name.
Adjust the code as follows:
12345
last_changed_at as LastChangedAt,
_Department
}
Activate the CDS data definition.
Press Ctrl + F3 to activate the development object.
Task 3: Define More Associations
In your basic CDS view entity for department data, define and expose three associations: an association for all employees (suggested association name: _Employee), an association for the department head (suggested name: _Head), and an association for the department assistant (suggested name: _Assistant). Choose the cardinalities based on the corresponding foreign key relationships in the underlying database tables.
Steps
Return to the data definition for department data. Define association _Employee to address all employees that are assigned to this department.
Note
Take into account that a department can have any number of employees, including zero.Adjust the code as follows:
1234567
define view entity Z##_R_Department
as select from z##depment
association [0..*] to Z##_R_Employee as _Employee
on $projection.Id = _Employee.DepartmentId
Define an association to address the details of the department head. Set the cardinality based on the cardinality of the foreign key relationship between the underlying database tables.
Note
In our model, it is NOT necessary that a department has a department head.Adjust the code as follows:
123456789
define view entity Z##_R_Department
as select from z##depment
association [0..*] to Z##_R_Employee as _Employee
on $projection.Id = _Employee.DepartmentId
association [0..1] to Z##_R_Employee as _Head
on $projection.HeadId = _Head.EmployeeId
Define an association to address the details of the department assistant. Set the cardinality based on the cardinality of the foreign key relationship between the underlying database tables.
Note
In our model, a department cannot exist without a department assistant.Adjust the code as follows:
1234567891011
define view entity Z##_R_Department
as select from z##depment
association [0..*] to Z##_R_Employee as _Employee
on $projection.Id = _Employee.DepartmentId
association [0..1] to Z##_R_Employee as _Head
on $projection.HeadId = _Head.EmployeeId
association [1..1] to Z##_R_Employee as _Assistant
on $projection.AssistantId = _Assistant.EmployeeId
Expose the association.
At the end of the element list, add a comma and the three association names (comma-separated).
Adjust the code as follows:
1234567
last_changed_at as LastChangedAt,
_Employee,
_Head,
_Assistant
}
Activate the CDS data definition.
Press Ctrl + F3 to activate the development object.
Task 4: Analyze and Use the Associations
Analyze the technical implementation of your CDS view for department data and use the associations for navigation in the Data Preview tool.
Steps
Display the SQL statement with which the technical representation of the view entity is created in the database.
Right-click anywhere in the source code of the data definition and choose Show SQL CREATE Statement.
Open the Data Preview for your CDS view for department data.
Right-click anywhere in the source code of the data definition and choose Open With → Data Preview. Alternatively, place the cursor anywhere in the source code of the data definition and press F8.
Display all employees that work for the Sales and Distribution department.
Right-click anywhere in the row with value SALE in the Id column and choose Follow Association. Alternatively, place the cursor anywhere in the row and press Ctrl + G.
Choose _Employee → Z##_R_Employee [0..*].
Return to the department list and display the assistant of the Administration department.
On the navigation path that is displayed above the Data Preview tab (sometimes referred to as "Breadcrumb naviation"), choose Z##_R_DEPARTMENT to return to the list of departments.
Right-click anywhere in the row with value ADMIN in the Id column and choose Follow Association. Alternatively, place the cursor anywhere in the row and press Ctrl + G.
Choose _Assistant → Z##_R_Employee [1..1].
Without returning to the list of departments, navigate to the department, this person works for and from there to the department head.
Right-click anywhere in the single row that is displayed and choose Follow Association.
Choose _Department → Z##_R_Department [1..1].
Right-click anywhere in the single row that is displayed and choose Follow Association.
Choose _Head → Z##_R_Employee [0..1].