创建 CDS 视图实体
ABAP RESTful 应用程序编程模型 › 开发 › 开发应用程序 › 使用独占草稿功能开发事务应用 › 使用 UUID 键值构建被管理业务对象 › 数据模型和业务对象结构 › 创建 CDS 视图实体
对于具有草稿的托管场景,为 CDS 视图实体创建数据定义以构建业务对象的结构。
上下文和过程
之前,已为差旅业务对象创建持久数据库表。被管理业务对象的结构现在必须由 CDS 数据建模定义。
为表示业务对象的三个交易实体的 CDS 视图实体创建数据定义:差旅、预订和预订补充。
备注
CDS 视图实体是基于 DDIC 的后继 CDS 视图。CDS 视图实体提供了许多优势,例如优化和简化语法检查,并在视图激活期间提高性能。
为差旅实体选择模板定义根视图实体。
为预订和登记补充实体选择模板 定义具有对父关联的视图实体 。
备注
ABAP 航班参考场景中的草稿场景使用后缀
_D。由于 CDS 视图实体是界面视图,因此根据 VDM(虚拟数据模型)命名规则以R_为前缀。有关详细信息,请参阅 开发对象的命名规则 。
对具有使用 UUID 键值布局的草稿的被管理业务对象使用 CDS 视图实体时,需要注意以下事项:
对于业务对象,必须定义表示业务对象抬头的根实体,并提供用于寻址业务对象的名称。
必须在 CDS 视图实体中按子实体的构成以及与父实体的关联定义被管理业务对象的构成结构。此外,与锁和权限主实体的关联(在我们的场景中,为根实体)必须可从业务对象中的所有实体获得。
如果 RAP 托管运行时框架使用相应的
@Semantics注释进行注释,则会自动填充管理字段。这些字段仅在业务对象的根实体上是必需的。特别是,每当更改实例时,都会更新使用@Semantics.systemDateTime.localInstanceLastChangedAt: true注释的字段。在我们的场景中,此字段用于 ETag 比较。
数据定义 /DMO/R_Travel_D:差旅根视图
差旅根视图的数据源是数据库表 /DMO/A_TRAVEL_D。数据库表中的所有字段均用于数据模型。
要从其他实体检索值帮助的主数据,请定义与 CDS 实体 /DMO/I_Agency,/DMO/I_Customer, I_Currency, /DMO/I_Overall_Status_VH 和 I_Currency 的关联。前者是 ABAP 航班参考场景的一部分,您可以下载该场景。CDS 视图 I_Currency 通常在开发系统中可用。
使用注释 @Semantics.amount.currencyCode 对价格字段进行注释,以定义用作货币的字段。
备注
与基于 DDIC 的 CDS 视图相反,在 CDS 视图实体中,如果已在数据库表中指定货币代码字段,则无需将货币代码字段指定为货币代码。
将语义注释用于管理字段。ETag 字段 local_instance_last_changed_at 接收注释 @Semantics.systemDateTime.localInstanceLastChangedAt: true。
只要存在构成结构的实体,就使用关键字 composition 定义与子实体 /DMO/R_Booking_D 的组成关系。
展开以下代码示例以查看 /DMO/R_Travel_D 的源代码。
示例代码
@EndUserText.label: 'Travel View Entity for Draft RefScen'
@AccessControl.authorizationCheck: #NOT_REQUIRED
define root view entity /DMO/R_Travel_D
as select from /DMO/A_TRAVEL_D
composition [0..*] of /DMO/R_Booking_D 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 [1..1] to /DMO/I_Overall_Status_VH as _OverallStatus on $projection.OverallStatus = _OverallStatus.OverallStatus
association [0..1] to I_Currency as _Currency on $projection.CurrencyCode = _Currency.Currency
{ ///DMO/A_TRAVEL_D
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 OverallStatus,
@Semantics.user.createdBy: true
local_created_by as LocalCreatedBy,
@Semantics.systemDateTime.createdAt: true
local_created_at as LocalCreatedAt,
@Semantics.user.localInstanceLastChangedBy: true
local_last_changed_by as LocalLastChangedBy,
//local ETag field --> OData ETag
@Semantics.systemDateTime.localInstanceLastChangedAt: true
local_last_changed_at as LocalLastChangedAt,
//total ETag field
@Semantics.systemDateTime.lastChangedAt: true
last_changed_at as LastChangedAt,
//Associations
_Booking,
_Agency,
_Customer,
_OverallStatus,
_Currency
}
数据定义 /DMO/R_Booking_D:预订视图
预订视图的数据源是数据库表 /DMO/A_BOOKING_D。数据库表中的所有字段均用于数据模型。
要从其他实体检索值帮助的主数据,请定义与 CDS 实体 /DMO/I_Customer、/DMO/I_Carrier、/DMO/I_Connection 和 /DMO/I_Booking_Status_VH 的关联。这些 CDS 视图是可下载的 ABAP 航班参考场景的一部分。
使用注释 @Semantics.amount.currencyCode 对价格字段进行注释,以定义用作货币的字段。
备注
与 CDS 视图相比,在 CDS 视图实体中,如果已在数据库表中指定货币代码字段,则无需将货币代码字段指定为货币代码。
将语义注释用于管理字段。本地 ETag 字段 local_instance_last_changed_at 接收注释 @Semantics.systemDateTime.localInstanceLastChangedAt: true。
只要存在构成结构的实体,就使用关键字 composition 定义与子实体 /DMO/R_BookingSupplement_D 的组成关系,并使用关键字 association to parent 定义与父实体 /DMO/R_Travel_D 的组成关系。
展开以下代码示例以查看 /DMO/R_Booking_D 的源代码。
示例代码
@EndUserText.label: 'Booking View Entity for Draft RefScen'
@AccessControl.authorizationCheck: #NOT_REQUIRED
define view entity /DMO/R_Booking_D as select from /DMO/A_BOOKING_D
association to parent /DMO/R_Travel_D as _Travel on $projection.TravelUUID = _Travel.TravelUUID
composition [0..*] of /DMO/R_BookingSupplement_D as _BookingSupplement
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.AirlineID = _Carrier.AirlineID
association [1..1] to /DMO/I_Connection as _Connection on $projection.AirlineID = _Connection.AirlineID
and $projection.ConnectionID = _Connection.ConnectionID
association [1..1] to /DMO/I_Booking_Status_VH as _BookingStatus on $projection.BookingStatus = _BookingStatus.BookingStatus
{ ///DMO/A_BOOKING_D
key booking_uuid as BookingUUID,
parent_uuid as TravelUUID,
booking_id as BookingID,
booking_date as BookingDate,
customer_id as CustomerID,
carrier_id as AirlineID,
connection_id as ConnectionID,
flight_date as FlightDate,
@Semantics.amount.currencyCode: 'CurrencyCode'
flight_price as FlightPrice,
currency_code as CurrencyCode,
booking_status as BookingStatus,
//local ETag field --> OData ETag
@Semantics.systemDateTime.localInstanceLastChangedAt: true
local_last_changed_at as LocalLastChangedAt,
//Associations
_Travel,
_BookingSupplement,
_Customer,
_Carrier,
_Connection,
_BookingStatus
}
数据定义 /DMO/R_BookingSupplement_D:预订补充视图
预订视图的数据源是数据库表 /DMO/A_BKSUPPL_D。数据库表中的所有字段均用于数据模型。
要从其他实体中检索值帮助和文本的主数据,请定义与 CDS 实体 /DMO/I_Supplement 和 /DMO/I_SupplementText 的关联。这些 CDS 视图是可下载的 ABAP 航班参考场景的一部分。
使用注释 @Semantics.amount.currencyCode 对价格字段进行注释,以定义用作货币的字段。
备注
与 CDS 视图相比,在 CDS 视图实体中,如果已在数据库表中指定货币代码字段,则无需将货币代码字段指定为货币代码。
将语义注释用于管理字段。本地 ETag 字段 local_instance_last_changed_at 接收注释 @Semantics.systemDateTime.localInstanceLastChangedAt: true。
只要存在构成结构的实体,就使用关键字 association to parent 定义与父实体 /DMO/R_Booking_D 的组成关系,并定义与根实体 /DMO/R_Travel_D 的组成关系。
展开以下代码示例以查看 /DMO/R_BookingSupplement_D 的源代码。
示例代码
@EndUserText.label: 'BookSuppl View Entity fro Draft RefScen'
@AccessControl.authorizationCheck: #NOT_REQUIRED
define view entity /DMO/R_BookingSupplement_D
as select from /DMO/A_BKSUPPL_D
association to parent /DMO/R_Booking_D as _Booking on $projection.BookingUUID = _Booking.BookingUUID
association [1..1] to /DMO/R_Travel_D as _Travel on $projection.TravelUUID = _Travel.TravelUUID
association [1..1] to /DMO/I_Supplement as _Product on $projection.SupplementID = _Product.SupplementID
association [1..*] to /DMO/I_SupplementText as _SupplementText on $projection.SupplementID = _SupplementText.SupplementID
{ ///DMO/A_BKSUPPL_D
key booksuppl_uuid as BookSupplUUID,
root_uuid as TravelUUID,
parent_uuid as BookingUUID,
booking_supplement_id as BookingSupplementID,
supplement_id as SupplementID,
@Semantics.amount.currencyCode: 'CurrencyCode'
price as BookSupplPrice,
currency_code as CurrencyCode,
//local ETag field --> OData ETag
@Semantics.systemDateTime.localInstanceLastChangedAt: true
local_last_changed_at as LocalLastChangedAt,
//Associations
_Travel,
_Booking,
_Product,
_SupplementText
}
来源:SAP Help Portal · 2025 FPS02 (Oct 2026) · 中文为 SAP 官方机器翻译