定义数据模型和业务对象结构
ABAP RESTful 应用程序编程模型 › 开发 › 开发应用程序 › 开发托管事务应用 › 开发随时可运行的业务对象 › 提供业务对象结构 › 定义数据模型和业务对象结构
差旅根视图 /DMO/I_Travel_M
对于根 CDS 视图 /DMO/I_Travel_M,数据库表 /dmo/travel_m 用作数据源。此 CDS 视图定义被管理业务对象的根实体,并表示要创建的差旅业务对象的构成层次结构的根。
要定义从根实体到子实体的构成关系,请使用关键字 COMPOSITION。在我们的示例中,我们将 /DMO/I_Booking_M 指定为组合 _Booking 中的子实体。因此,预订节点定义为业务对象根的直接子实体。基数 [0 .. *] 指定可以将任意数量的预订实例分配给每个差旅实例。
为了能够从其他实体访问主数据,请在 CDS 源代码中定义一组关联。这些关联是指属于演示应用程序场景一部分的 CDS 实体。其中一些视图主要用作检索文本信息的文本视图,并用作特定 UI 字段的值帮助提供者视图。
最后,标准管理数据的字段将添加到选择列表中,其中持久字段 last_changed_at 作为字段 ETag 扮演特殊角色。
为确保使用者端的数据处理一致,为所有管理和数量字段提供相应的 @Semantics 注释。这些注释是支持托管 ETag 处理所必需的,即每个操作中相关 ETag 字段的自动更新。
展开以下代码示例以查看 CDS 根视图 /DMO/I_Travel_M 的源代码。
示例代码
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Travel view - CDS data model'
define root view entity /DMO/I_Travel_M
as select from /dmo/travel_m as Travel -- the travel table is the data source for this view
composition [0..*] of /DMO/I_Booking_M as _Booking
association [0..1] to /DMO/I_Agency as _Agency on $projection.agency_id = _Agency.AgencyID
association [0..1] to /DMO/I_Customer as _Customer on $projection.customer_id = _Customer.CustomerID
association [0..1] to I_Currency as _Currency on $projection.currency_code = _Currency.Currency
association [1..1] to /DMO/I_Overall_Status_VH as _OverallStatus on $projection.overall_status = _OverallStatus.OverallStatus
{
key travel_id,
agency_id,
customer_id,
begin_date,
end_date,
@Semantics.amount.currencyCode: 'currency_code'
booking_fee,
@Semantics.amount.currencyCode: 'currency_code'
total_price,
currency_code,
overall_status,
description,
@Semantics.user.createdBy: true
created_by,
@Semantics.systemDateTime.createdAt: true
created_at,
@Semantics.user.localInstanceLastChangedBy: true
last_changed_by,
//local ETag field --> OData ETag
@Semantics.systemDateTime.localInstanceLastChangedAt: true
last_changed_at,
/* Associations */
_Booking,
_Agency,
_Customer,
_Currency,
_OverallStatus
}
预订视图 /DMO/I_Booking_M
清单 2(如下)为您提供登记实体的数据模型实施。在数据库表中声明的所有字段均用于 CDS 视图。管理字段 last_changed_at 用于对所有节点进行乐观并发控制。这意味着,每个业务对象实体都存储其自己的 eTag。
在根实体 /DMO/I_Travel_M 的数据定义中,我们指定了登记实体 /DMO/I_Booking_M 作为组合子实体。相反,此关系需要与其组成父实体的关联 - 来自子实体。此关系由关键字 ASSOCIATION TO PARENT 表示。
为了为 3 层实体层次结构提供数据模型,我们还定义了从登记到登记补充实体的构成关系。在我们的示例中,我们指定 /DMO/I_BookSuppl_M 作为组合 _BookSupplement 中的子实体。基数 [0 .. *] 表示可以将任意数量的预订补充实例分配给每个预订实例。
为了能够访问其他实体中的数据,请在 CDS 源代码中定义一组附加关联。
SELECT 列表包括预订实体的所有元素,这些元素与用户界面中的使用相关。
展开以下代码示例以查看 CDS 视图 /DMO/I_Booking_M 的源代码。
示例代码
@AccessControl.authorizationCheck: #NOT_REQUIRED
//@Metadata.ignorePropagatedAnnotations:true
@EndUserText.label: 'Booking view'
define view entity /DMO/I_Booking_M
as select from /dmo/booking_m as Booking
association to parent /DMO/I_Travel_M as _Travel on $projection.travel_id = _Travel.travel_id
composition [0..*] of /DMO/I_BookSuppl_M as _BookSupplement
association [1..1] to /DMO/I_Customer as _Customer on $projection.customer_id = _Customer.CustomerID
association [1..1] to /DMO/I_Carrier as _Carrier on $projection.carrier_id = _Carrier.AirlineID
association [1..1] to /DMO/I_Connection as _Connection on $projection.carrier_id = _Connection.AirlineID
and $projection.connection_id = _Connection.ConnectionID
association [1..1] to /DMO/I_Booking_Status_VH as _BookingStatus on $projection.booking_status = _BookingStatus.BookingStatus
{
key travel_id,
key booking_id,
booking_date,
customer_id,
carrier_id,
connection_id,
flight_date,
@Semantics.amount.currencyCode: 'currency_code'
flight_price,
currency_code,
booking_status,
//local ETag field --> OData ETag
@Semantics.systemDateTime.localInstanceLastChangedAt: true
last_changed_at,
/* Associations */
_Travel,
_BookSupplement,
_Customer,
_Carrier,
_Connection,
_BookingStatus
}
预订补充视图 /DMO/I_BookSuppl_M
3 层构成关系需要预订补充子实体 /DMO/I_BookSuppl_M 实体与其组成父实体的关联。
与预订实体一样,预订补充实体也使用字段 last_changed_at 存储 eTag 值。
要从其他实体访问主数据,请在 CDS 源代码中定义附加关联。
展开以下代码示例以查看 CDS 视图 /DMO/I_BookSuppl_M 的源代码。
示例代码
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Booking Supplement View - CDS data model'
define view entity /DMO/I_BookSuppl_M
as select from /dmo/booksuppl_m as BookingSupplement
association to parent /DMO/I_Booking_M as _Booking on $projection.travel_id = _Booking.travel_id
and $projection.booking_id = _Booking.booking_id
association [1..1] to /DMO/I_Travel_M as _Travel on $projection.travel_id = _Travel.travel_id
association [1..1] to /DMO/I_Supplement as _Product on $projection.supplement_id = _Product.SupplementID
association [1..*] to /DMO/I_SupplementText as _SupplementText on $projection.supplement_id = _SupplementText.SupplementID
{
key travel_id,
key booking_id,
key booking_supplement_id,
supplement_id,
@Semantics.amount.currencyCode: 'currency_code'
price,
currency_code,
//local ETag field --> OData ETag
@Semantics.systemDateTime.localInstanceLastChangedAt: true
last_changed_at,
/* Associations */
_Travel,
_Booking,
_Product,
_SupplementText
}
来源:SAP Help Portal · 2025 FPS02 (Oct 2026) · 中文为 SAP 官方机器翻译
← 创建 CDS 数据定义 · 定义随时可运行业务对象的基本行为 →