You want to decouple the data access and the table definition. To do so, you define a CDS view entity that read
Prerequisites
For this exercise, you need the database table for employee data that you created and filled in a previous exercise (suggested name was: Z##EMPLOY, where ## is your group number). If you have not finished that exercise, create a copy of the database table /LRN/EMPLOY_DEP .
Task 1: Define a CDS View Entity
Define a new CDS view entity that reads from your database table with employee data (suggested name: Z##_R_Employee, where ## is your group number). Use a template that adds all table fields to the element list of the view (except for the client field, the annual salary and the currency code) and that takes over the key definition from the database table.
Steps
In your own package, create a new data definition (suggested name: Z##_R_EMPLOYEE, 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##EMPLOY 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_Employee, where ## is your group number).
Enter Employee (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.
For the time being, comment elements AnnualSalary and CurrencyCode.
Select both code rows and press Ctrl + < to add two slash signs (/) at the beginning of each row.
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: Analyze the CDS View Entity
Analyze the technical implementation of the CDS view entity in the database and test the result.
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.
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.