引言在 RAP 四层架构中数据建模层由 CDS ViewCore Data Services View构成二者是“实现载体”与“抽象层次”的关系。具体来说数据建模层是逻辑概念指负责定义业务对象的数据结构、关系、语义以及数据库计算逻辑的那一层。CDS View是这一层唯一的技术实现制品artifact它使用 ABAP CDS 语法编写运行在 SAP HANA 数据库上。在 RAP 中说“数据建模层”就是指一组精心设计的 CDS View 的组合。没有 CDS View数据建模层就不存在。任何上层的行为定义Behavior Definition、服务绑定Service Binding都必须基于某个 CDS View 作为根视图Root View来构建。CDS VIEW更多了解https://blog.csdn.net/HeathlX/article/details/160806605?spm1001.2014.3001.5502下面讲解如何根据自定义数据库表创建CDS VIEW步骤1.为实体创建接口CDS VIEW右键数据库表ZRAP_ATRAV_KJ-New Data DefinitionCDS VIEW代码AccessControl.authorizationCheck: #CHECK EndUserText.label: Travel BO view define view entity ZI_RAP_ATRAV_KJ as select from zrap_atrav_kj as Travel association [0..*] to ZI_RAP_Booking_KJ as _Booking on $projection.TravelUUID _Booking.TravelUUID association [0..1] to /DMO/I_Agency as _Agency on $projection.AgencyID _Agency.AgencyID association [0..1] to /DMO/I_Customer as _Customer on $projection.CustomerID _Customer.CustomerID association [0..1] to I_Currency as _Currency on $projection.CurrencyCode _Currency.Currency { key travel_uuid as TravelUUID, travel_id as TravelID, agency_id as AgencyID, customer_id as CustomerID, begin_date as BeginDate, end_date as EndDate, Semantics.amount.currencyCode: CurrencyCode booking_fee as BookingFee, Semantics.amount.currencyCode: CurrencyCode total_price as TotalPrice, currency_code as CurrencyCode, description as Description, overall_status as TravelStatus, Semantics.user.createdBy: true created_by as CreatedBy, Semantics.systemDateTime.createdAt: true created_at as CreatedAt, Semantics.user.lastChangedBy: true last_changed_by as LastChangedBy, Semantics.systemDateTime.lastChangedAt: true last_changed_at as LastChangedAt, Semantics.systemDateTime.localInstanceLastChangedAt: true local_last_changed_at as LocalLastChangedAt, /* associations */ _Booking, _Agency, _Customer, _Currency }另外一张表同理CDS VIEW代码AccessControl.authorizationCheck: #CHECK EndUserText.label: Booking BO view define view entity ZI_RAP_Booking_KJ as select from zrap_abook_kj as Booking association [1..1] to ZI_RAP_ATRAV_KJ as _Travel on $projection.TravelUUID _Travel.TravelUUID association [1..1] to /DMO/I_Customer as _Customer on $projection.CustomerID _Customer.CustomerID association [1..1] to /DMO/I_Carrier as _Carrier on $projection.CarrierID _Carrier.AirlineID association [1..1] to /DMO/I_Connection as _Connection on $projection.CarrierID _Connection.AirlineID and $projection.ConnectionID _Connection.ConnectionID association [1..1] to /DMO/I_Flight as _Flight on $projection.CarrierID _Flight.AirlineID and $projection.ConnectionID _Flight.ConnectionID and $projection.FlightDate _Flight.FlightDate association [0..1] to I_Currency as _Currency on $projection.CurrencyCode _Currency.Currency { key booking_uuid as BookingUUID, travel_uuid as TravelUUID, booking_id as BookingID, booking_date as BookingDate, customer_id as CustomerID, carrier_id as CarrierID, connection_id as ConnectionID, flight_date as FlightDate, Semantics.amount.currencyCode: CurrencyCode flight_price as FlightPrice, currency_code as CurrencyCode, Semantics.user.createdBy: true created_by as CreatedBy, Semantics.user.lastChangedBy: true last_changed_by as LastChangedBy, Semantics.systemDateTime.localInstanceLastChangedAt: true local_last_changed_at as LocalLastChangedAt, /* associations */ _Travel, _Customer, _Carrier, _Connection, _Flight, _Currency }保存激活步骤 2. 定义组合模型最后需要增强 CDS 数据模型以指定业务对象结构即组合模型或组合树组合模型包含两个节点根节点 atrav 及其子节点 booking。定义根节点在CDS VIEW的define view entity语句中添加关键字rootdefinerootview entity ZI_RAP_Atrav_KJ将_Booking关联的定义调整为组合关系这是定义父节点行程与子节点预订之间关系所必需的。为此请将关联_Booking的定义替换为以下组合定义composition [0..*] of ZI_RAP_Booking_#### as _Booking保存更改但切勿立即激活更改打开 CDS 数据定义ZI_RAP_BOOKING_####并将关联_Travel的定义替换为下方提供的定义以指定从子节点booking到其父节点atrav的关系。association to parent ZI_RAP_ATRAV_#### as _Travel on $projection.TravelUUID _Travel.TravelUUID保存激活后进行数据预览完整代码(注意)根据自定义的CDS VIEW名称补充即可案例的视图名称为ZI_RAP_atrav_####ZI_RAP_Booking_####AccessControl.authorizationCheck: #CHECK EndUserText.label: Travel BO view define root view entity ZI_RAP_Travel_#### as select from zrap_atrav_#### as Travel composition [0..*] of ZI_RAP_Booking_#### as _Booking association [0..1] to /DMO/I_Agency as _Agency on $projection.AgencyID _Agency.AgencyID association [0..1] to /DMO/I_Customer as _Customer on $projection.CustomerID _Customer.CustomerID association [0..1] to I_Currency as _Currency on $projection.CurrencyCode _Currency.Currency { key travel_uuid as TravelUUID, travel_id as TravelID, agency_id as AgencyID, customer_id as CustomerID, begin_date as BeginDate, end_date as EndDate, Semantics.amount.currencyCode: CurrencyCode booking_fee as BookingFee, Semantics.amount.currencyCode: CurrencyCode total_price as TotalPrice, currency_code as CurrencyCode, description as Description, overall_status as TravelStatus, Semantics.user.createdBy: true created_by as CreatedBy, Semantics.systemDateTime.createdAt: true created_at as CreatedAt, Semantics.user.lastChangedBy: true last_changed_by as LastChangedBy, Semantics.systemDateTime.lastChangedAt: true last_changed_at as LastChangedAt, Semantics.systemDateTime.localInstanceLastChangedAt: true local_last_changed_at as LocalLastChangedAt, /* associations */ _Booking, _Agency, _Customer, _Currency }AccessControl.authorizationCheck: #CHECK EndUserText.label: Booking BO view define view entity ZI_RAP_Booking_#### as select from zrap_abook_#### as Booking association to parent ZI_RAP_Travel_#### as _Travel on $projection.TravelUUID _Travel.TravelUUID association [1..1] to /DMO/I_Customer as _Customer on $projection.CustomerID _Customer.CustomerID association [1..1] to /DMO/I_Carrier as _Carrier on $projection.CarrierID _Carrier.AirlineID association [1..1] to /DMO/I_Connection as _Connection on $projection.CarrierID _Connection.AirlineID and $projection.ConnectionID _Connection.ConnectionID association [1..1] to /DMO/I_Flight as _Flight on $projection.CarrierID _Flight.AirlineID and $projection.ConnectionID _Flight.ConnectionID and $projection.FlightDate _Flight.FlightDate association [0..1] to I_Currency as _Currency on $projection.CurrencyCode _Currency.Currency { key booking_uuid as BookingUUID, travel_uuid as TravelUUID, booking_id as BookingID, booking_date as BookingDate, customer_id as CustomerID, carrier_id as CarrierID, connection_id as ConnectionID, flight_date as FlightDate, Semantics.amount.currencyCode: CurrencyCode flight_price as FlightPrice, currency_code as CurrencyCode, Semantics.user.createdBy: true created_by as CreatedBy, Semantics.user.lastChangedBy: true last_changed_by as LastChangedBy, Semantics.systemDateTime.localInstanceLastChangedAt: true local_last_changed_at as LocalLastChangedAt, /* associations */ _Travel, _Customer, _Carrier, _Connection, _Flight, _Currency }