news 2026/5/12 4:17:32

SAP-BTP :(4)RAP-创建CDS DATA模型映射和拓展

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
SAP-BTP :(4)RAP-创建CDS DATA模型映射和拓展

CDS数据模型基础上定义CDS数据模型映射。借助映射视图只暴露与特定服务相关的元素。可以对底层数据模型进行反规范化,还能定义精细调整项,例如虚拟元素、值帮助、搜索设置和UI语义。

右键->New Data Definition,填写Reference Object(根据上一篇文章创建的基础CDS VIEW视图)

从列表中选择Define Projection View模板,然后选择完成

创建完成先保存别激活

然后创建另外一个表CDS VIEW,和上面一样的操作

创建两个视图后修改CDS VIEW编辑器的代码:

@EndUserText.label: 'Travel BO projection view' @AccessControl.authorizationCheck: #CHECK @Search.searchable: true @Metadata.allowExtensions: true define root view entity ZC_RAP_Travel_#### as projection on ZI_RAP_Travel_#### as Travel { key TravelUUID, @Search.defaultSearchElement: true TravelID, @Consumption.valueHelpDefinition: [{ entity: { name: '/DMO/I_Agency', element: 'AgencyID'} }] @ObjectModel.text.element: ['AgencyName'] @Search.defaultSearchElement: true AgencyID, _Agency.Name as AgencyName, @Consumption.valueHelpDefinition: [{ entity: { name: '/DMO/I_Customer', element: 'CustomerID'} }] @ObjectModel.text.element: ['CustomerName'] @Search.defaultSearchElement: true CustomerID, _Customer.LastName as CustomerName, BeginDate, EndDate, @Semantics.amount.currencyCode: 'CurrencyCode' BookingFee, @Semantics.amount.currencyCode: 'CurrencyCode' TotalPrice, @Consumption.valueHelpDefinition: [{ entity: { name: 'I_Currency', element: 'Currency'} }] CurrencyCode, Description, TravelStatus, LastChangedAt, LocalLastChangedAt, /* Associations */ _Agency, _Booking : redirected to composition child ZC_RAP_Booking_####, _Currency, _Customer }

简短说明:

  • 为投影视图指定了别名Travel
  • 关键词rootDEFINE语句中被指定,用于将预计的业务对象设为根节点。
  • 视图注解@Metadata.allowedExtension是在DEFINE语句之前指定的,用于允许投影视图通过单独的元数据扩展进行增强
  • 视图注解@Search.Searchable位于DEFINE语句之前,用于让投影视图支持全文(即自由格式)搜索。
  • 已通过注释@Search.DefaultSearchElement为视图元素TravelIDAgencyIDCustomerID启用了自由式搜索。
  • 来自关联_Agency的视图元素AgencyName以及来自关联_CustomerCustomerName已添加到投影列表中。通过@ObjectModel.text.element注释,它们分别被指定为视图元素AgencyIDCustomerID的文本描述。
  • 已为视图元素AgencyIDCustomerIDCurrencyCode通过批注@Consumption.valueHelpDefinition指定了值帮助。必须指定用作值帮助提供程序的目标 CDS 实体的名称,以及与其链接到本地元素的元素名称。
  • 视图元素CurrencyCode通过@Semantics.amount.currencyCode注解被指定为货币字段BookingFeeTotalPrice的参考字段。
  • 视图元素CreatedByCreatedAtLastChangedBy已从投影列表中移除,因为它们仅具有管理功能,在我们的场景中没有用处。视图元素LastChangedAtLocalLastChangedAt保留在投影列表中,因为它们将用于第3周你的旅行清单报告应用的事务性启用——尤其是用于乐观锁的实现。
  • 已在投影列表中公开关联
  • 与预订业务对象子节点(_Booking)的关联已通过redirected to composition child语句重定向到相应的预订业务对象投影视图。
@EndUserText.label: 'Booking BO projection view' @AccessControl.authorizationCheck: #CHECK @Search.searchable: true @Metadata.allowExtensions: true define view entity ZC_RAP_Booking_#### as projection on ZI_RAP_Booking_#### as Booking { key BookingUUID, TravelUUID, @Search.defaultSearchElement: true BookingID, BookingDate, @Consumption.valueHelpDefinition: [{ entity : {name: '/DMO/I_Customer', element: 'CustomerID' } }] @ObjectModel.text.element: ['CustomerName'] @Search.defaultSearchElement: true CustomerID, _Customer.LastName as CustomerName, @Consumption.valueHelpDefinition: [{entity: {name: '/DMO/I_Carrier', element: 'AirlineID' }}] @ObjectModel.text.element: ['CarrierName'] CarrierID, _Carrier.Name as CarrierName, @Consumption.valueHelpDefinition: [ {entity: {name: '/DMO/I_Flight', element: 'ConnectionID'}, additionalBinding: [ { localElement: 'CarrierID', element: 'AirlineID' }, { localElement: 'FlightDate', element: 'FlightDate', usage: #RESULT}, { localElement: 'FlightPrice', element: 'Price', usage: #RESULT }, { localElement: 'CurrencyCode', element: 'CurrencyCode', usage: #RESULT } ] } ] ConnectionID, FlightDate, @Semantics.amount.currencyCode: 'CurrencyCode' FlightPrice, @Consumption.valueHelpDefinition: [{entity: {name: 'I_Currency', element: 'Currency' }}] CurrencyCode, LocalLastChangedAt, /* associations */ _Travel : redirected to parent ZC_RAP_Travel_####, _Customer, _Carrier, _Connection, _Flight }

简短说明:

  • 为投影视图指定别名Booking
  • 视图注解@Metadata.allowedExtension是在DEFINE语句之前指定的,用于允许投影视图通过单独的元数据扩展进行增强
  • 视图注解@Search.Searchable位于DEFINE语句之前,用于让投影视图支持全文(即自由格式)搜索。
  • 视图列BookingIDCustomerID已启用自由格式搜索。来自关联_Customer__Carrier的视图元素CustomerNameCarrierName已添加到投影列表中。它们分别通过注释@ObjectModel.text.element被指定为视图元素CustomerIDCarrierID的文本描述。
  • 已使用注解@Consumption.valueHelpDefinition为视图元素CustomerIDCarrierIDConnectionIDCurrencyCode指定了值帮助。这个元素的值帮助定义中,为从所选的值帮助记录中为本地视图元素CarrierIDFlightDateFlightPriceCurrency返回值,定义了一个额外的绑定条件。
  • 视图元素CurrencyCode被指定为货币字段FlightPrice的引用字段。
  • 视图元素CreatedByLastChangedBy已从投影列表中移除,因为它们仅具有管理功能,在我们的场景中没有用处。视图元素LocalLastChangedAt保留在投影列表中,因为它将用于第 3 周你的旅行清单报告应用的事务性启用——尤其是用于乐观锁的实现。
  • 所有关联均已在投影列表中公开。
  • 已使用redirected to parent语句,将与旅行业务对象父节点的关联重定向到相应的旅行业务对象投影视图。

然后激活

激活成功后,验证数据:执行数据预览


上面的CDS VIEW已经添加注释:@Metadata.allowExtensions:true,现在可以进行独立的 CDS 元数据扩展对其进行无需修改的增强(即在CDS VIEW的基础创建另外一个CDS VIEW进行一个功能的拓展)。

右键->New Metadata Extension

next

finish

设置列并给元素指定 UI 注释,代码如下:

@Metadata.layer: #CORE @UI: { headerInfo: { typeName: 'Travel', typeNamePlural: 'Travels', title: { type: #STANDARD, label: 'Travel', value: 'TravelID' } }, presentationVariant: [{ sortOrder: [{ by: 'TravelID', direction: #DESC }] }] } annotate view ZC_RAP_ATRAV_KJ with { @UI.facet: [ { id: 'Travel', purpose: #STANDARD, type: #IDENTIFICATION_REFERENCE, label: 'Travel', position: 10 }, { id: 'Booking', purpose: #STANDARD, type: #LINEITEM_REFERENCE, label: 'Booking', position: 20, targetElement: '_Booking'} ] @UI:{ identification: [{ position: 1, label: 'Travel UUID' }] } TravelUUID; @UI: { lineItem: [ { position: 10 } ], identification: [ { position: 10 } ], selectionField: [ { position: 10 } ] } TravelID; @UI: { lineItem: [ { position: 20 } ], identification: [ { position: 20 } ], selectionField: [ { position: 20 } ] } AgencyID; @UI: { lineItem: [ { position: 30 } ], identification: [ { position: 30 } ], selectionField: [ { position: 30 } ] } CustomerID; @UI: { lineItem: [ { position: 40 } ], identification: [ { position: 40 } ] } BeginDate; @UI: { lineItem: [ { position: 50 } ], identification: [ { position: 50 } ] } EndDate; @UI: { lineItem: [ { position: 60 } ], identification: [ { position: 60 } ] } BookingFee; @UI: { lineItem: [ { position: 70 } ], identification: [ { position: 70 } ] } TotalPrice; @UI: { lineItem: [ { position: 80 } ], identification: [ { position: 80 } ] } Description; @UI: { lineItem: [ { position: 90 } ], identification: [ { position: 90 } ] } TravelStatus; @UI.hidden: true LastChangedAt; @UI.hidden: true LocalLastChangedAt; }

代码简短说明:

  • #CORE被指定为元数据层,因为您是应用程序提供方。
    当为给定的CDS实体定义了多个元数据扩展时,该层会确定元数据的优先级。#CORE的优先级最低,#CUSTOMER的优先级最高。
  • 头部信息(例如类型名称和标题)以及我们使用@UI视图注释的列表报表应用的展示变体,均在ANNOTATE VIEW语句之前的顶部进行定义。查询到的行程数据将在列表中按TravelID元素以降序排列。
  • 对象页面的导航及其布局是通过花括号中的@UI.facet注解定义的。行程对象页面包含两个分面:行程实体的标识引用和预订实体的行项目引用,其中_Booking组合被指定为目标元素。
  • 各种@UI注释用于指定旅行列表和对象页面上每个元素的布局。

关于UI注释
如前所述,本课程的重点是应用程序的 RESTful 后端实现。因此,我们将仅使用少量 UI 注释来丰富预定义的 CDS 数据模型,以便后续生成我们的 SAP Fiori elements 应用程序。有关为元数据驱动的用户界面定义CDS注释以及ABAP CDS中支持的用户界面注释的更多信息,可在SAP帮助门户查阅。

例如

  • 注解@UI.lineItem用于指定列表中以列形式显示的每个元素的布局信息。
  • 注解@UI.identification用于指定对象页面标识部分中显示的每个元素的布局信息。
  • 注解@UI.selectionField用于让筛选栏中的某个元素支持选择。如果为指定元素定义了值帮助,该元素会自动暴露到界面上。
  • 对于定位顺序,属性position的值可以使用任意小数。
  • 通过 UI 注释@UI.hidden:true,我们可以阻止元素在 UI 或个性化设置对话框中显示。

同样的创建另外CDS VIEW的拓展视图

CDS VIEW代码如下:

@Metadata.layer: #CORE @UI: { headerInfo: { typeName: 'Booking', typeNamePlural: 'Bookings', title: { type: #STANDARD, value: 'BookingID' } } } annotate view ZC_RAP_Booking_KJ with { @UI.facet: [ { id: 'Booking', purpose: #STANDARD, type: #IDENTIFICATION_REFERENCE, label: 'Booking', position: 10 } ] @UI: { identification: [ { position: 10, label: 'Booking UUID' } ] } BookingUUID; @UI.hidden: true TravelUUID; @UI: { lineItem: [ { position: 20 } ], identification: [ { position: 20 } ] } BookingID; @UI: { lineItem: [ { position: 30 } ], identification: [ { position: 30 } ] } BookingDate; @UI: { lineItem: [ { position: 40 } ], identification: [ { position: 40 } ] } CustomerID; @UI: { lineItem: [ { position: 50 } ], identification: [ { position: 50 } ] } CarrierID; @UI: { lineItem: [ { position: 60 } ], identification: [ { position: 60 } ] } ConnectionID; @UI: { lineItem: [ { position: 70 } ], identification: [ { position: 70 } ] } FlightDate; @UI: { lineItem: [ { position: 80 } ], identification: [ { position: 80 } ] } FlightPrice; @UI.hidden: true LocalLastChangedAt; }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/12 4:15:33

Gorilla大模型:基于检索增强微调的API调用实战指南

1. 项目概述:当大语言模型学会“打电话”如果你在过去一年里折腾过AI应用开发,大概率遇到过这个头疼的问题:你有一个功能强大的大语言模型(LLM),比如GPT-4或者Claude,你想让它帮你查天气、发邮件…

作者头像 李华
网站建设 2026/5/12 4:12:31

新公司也能报高企?申报全攻略

不少初创企业都会有这样的疑问:公司2026年3月新注册,能不能在2027年申报高新技术企业?成功率如何?一、新成立企业,到底能不能报高企?依据高企申报政策,成立不满三年的企业,可按实际经…

作者头像 李华
网站建设 2026/5/12 4:06:35

ESP32开发板选型指南:为什么NodeMCU-32S是新手入门的最佳选择?

ESP32开发板选型指南:为什么NodeMCU-32S是新手入门的最佳选择? 当你第一次踏入物联网开发的世界,面对琳琅满目的ESP32开发板,选择困难症可能会瞬间发作。ESP32-DevKitC、TTGO T-Display、Wemos D1 Mini...这些名字听起来都很酷&am…

作者头像 李华
网站建设 2026/5/12 4:06:34

【实战】T100开发核心:从Genero FGL到帆软报表的进阶指南

1. Genero FGL基础语法精要 作为T100系统的底层开发语言,Genero FGL(前身TIPTOP4GL)的语法特性直接决定了开发效率。我刚开始接触时最头疼的就是它的变量定义方式,和常见编程语言差异较大。举个例子,定义一个字符串变量…

作者头像 李华