1:创建两张表:ztt_teacher01 和ztt_teacher02 用于 cds view中的join和association

2:创建两个cds view:ztt_teacher01_id_name和 ztt_teacher02_id_salary

3: 在ztt_teacher01_id_name 中使用连接对数据表进行查询

@AbapCatalog.sqlViewName: 'TEACHER01'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'teacher table have id,name field'
define view ztt_teacher01_id_name as select from ztt_teacher01 as t01 left outer join ztt_teacher02 as t02 on t01.id = t01.id {
  t01.id,
  name,
  salary
}

结果如下:

4: ztt_teacher02_id_salary

@AbapCatalog.sqlViewName: 'TEACHER02'   //名字,改名字用于外部交互。
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'teacher table has id_salary'
define view ztt_teacher02_id_salary as select from ztt_teacher02 {
  id,
  salary
}

5: ztt_teacher01_id_name 使用association

@AbapCatalog.sqlViewName: 'TEACHER01'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'teacher table have id,name field'
define view ztt_teacher01_id_name as select from ztt_teacher01 as t01
  association [..] to teacher02 as asso02 on $projection.id = asso02.id {
  t01.id,
  name,
  asso02.salary
}

结果如下:

因为association 涉及到两个cds view,因此不能使用测试环境对ztt_teacher01_id_name 进行mock 数据。但是可以对cds view 进行数据库查询。步骤如下:

1:将cds view ztt_teacher01_id_name 中的view 定义改为:

@AbapCatalog.sqlViewName: 'TEACHER01'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'teacher table have id,name field'
define view ztt_teacher01_id_name as select from ztt_teacher01 as t01
  association [..] to teacher02 as asso02 on $projection.id = asso02.id {
  t01.id,
  name,
  asso02.salary,
  asso02          //将另一个cds view 作为 ztt_teacher01_id_name 的一个属性。
}

2:定义查询语句,对teacher02 中的字段salary进行查询:

  method GET_TEACHER_SALARY_BY_ID.
    SELECT SINGLE \asso02-salary  FROM ztt_teacher01_id_name
      WHERE ztt_teacher01_id_name~id = @iv_id
      INTO @ev_salary.
  endmethod.

3: unit test 调用查询语句。

    METHOD Test_teacher_salary_assocation.

    TYPES: BEGIN OF teacher_association,
           id TYPE ztt_teacher01-id,
           name TYPE ztt_teacher01-name,
           salary TYPE ztt_teacher02-salary,
      end of teacher_association.

    data lt_TEST_DATA TYPE STANDARD TABLE OF teacher_association.
    .
    DATA lv_salary TYPE i.

*    lt_TEST_DATA = VALUE #(  " 对association不能插入假数据了,因为底层有两个 view。而创建的 test_environment只是对一个view进行虚拟。
*                           ( id = '1' name = 'lisi' salary = 10 )  " the first data adds to cds view
*                           ( id = '2' name = 'zhaoliu' salary = 20 )  " other data with same name will be abandoned.
*                           ).
*    lv_cds_test_environment->insert_test_data( i_data = lt_TEST_DATA ).

    CALL METHOD f_cut->GET_TEACHER_SALARY_BY_ID(  "调用方法,直接对cds view进行查询,该cds view 直接查询数据库中的数据。
      EXPORTING
        iv_id   = lv_id
      IMPORTING
        ev_salary = lv_salary ).
    cl_abap_unit_assert=>assert_equals( act = lv_salary
      exp =
      msg = 'the name is not expected!' ).
  ENDMETHOD.

成功执行。

cds view join和association的更多相关文章

  1. 如何用ABAP代码读取CDS view association的数据

    我有如下一个CDS view, 这个view的数据来自CRMD_ORDERADM_H, 定义了一个名称为_statushelp的association, 指向了另一个CDS view Z_C_Stat ...

  2. Inner Join, Left Outer Join和Association的区别

    测试用的CDS视图的源代码,第8行用Inner Join连接TJ02T, 后者存放了所有系统状态的ID和描述. Inner Join测试结果:对于那些在TJ02T里没有维护描述信息的状态,它们不会出现 ...

  3. CDS view注解解析 - @Environment.systemField

    下面的CDS view使用到了@Environment.systemField这个注解,定义了两个参数#SYSTEM_LANGUAGE和#USER. 这个view从CRM物料主数据的产品抬头表COMM ...

  4. Create Fiori List App Report with ABAP CDS view – PART 1

    From Create Fiori List App Report with ABAP CDS view – PART 1 In this blog, I am going to show How C ...

  5. 使用CDS view开发SAP Marketing contact的facet追溯工具

    这篇SAP社区博客里,我的一位同事介绍了SAP Marketing里contact facet数据模型的存储表: https://blogs.sap.com/2016/07/01/how-does-s ...

  6. 一个使用CDS VIEW 的 DEMO

    一个使用CDS VIEW 的demo REPORT demo_cds_currency_conversion. CLASS demo DEFINITION. PUBLIC SECTION. CLASS ...

  7. Jerry的CDS view自学系列

    My CDS view self study tutorial - part 1 how to test odata service generated by CDS view https://blo ...

  8. Jerry的通过CDS view + Smart Template 开发Fiori应用的blog合集

    S4/HANA里有一个新的UI框架叫做Smart template, 配合ABAP后台的CDS view技术,能够让developer以Metadata driven的方式来开发Fiori应用, 这种 ...

  9. cds view 创建和调用

    cds view 是一个core data service, 能够将数据库表虚拟化为一个虚拟表(double).因为各个使用sap的公司,使用的数据库数据是不同的,所以提供一个数据库的虚拟.  通过向 ...

随机推荐

  1. ImageMagick 转换图片格式

    [root@ drawable-hdpi-v4]# convert ic_launcher.jpeg ic_launcher.png [root@ drawable-hdpi-v4]# file ic ...

  2. java 查看SOAP请求报文

    log.info("ESB 请求URL = " + cachedEndpoint.toString());//打印SOAP请求报文 add by LinJC on 20170120 ...

  3. Postman 接口测试神器

    Postman 接口测试神器 Postman 是一个接口测试和 http 请求的神器,非常好用. 官方 github 地址: https://github.com/postmanlabs Postma ...

  4. hdoj:2081

    手机短号 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  5. Web重温系列(三):OracleDependency实现监听数据库变化

    有个小项目(后来由另一个小组以Java开发了),内容是监控一个Oracle数据库.如果其中一个表A有数据变动,则需要将相关内容重组后通过接口发送给B. 通常的解决办法是定时查询,时间间隔可以小一点,还 ...

  6. hibernate中的addEntity setResultTransformer的比较

    如果使用原生sql语句进行query查询时,hibernate是不会自动把结果包装成实体的.所以要手动调用addEntity(Class class)等一系列方法. 如session.createSQ ...

  7. Android的TextView设置padding无效

    I finally found the problem I have to set the background first before setting the padding. Setting t ...

  8. ganglia使用nagios告警

    1.复制所需脚本到nagios的脚本目录,并设置权限  cp -fr ./ganglia-3.7.1/contrib/check_ganglia.py   /usr/local/nagios/libe ...

  9. Zephyr学习(四)系统时钟

    每一个支持多进程(线程)的系统都会有一个滴答时钟(系统时钟),这个时钟就好比系统的“心脏”,线程的休眠(延时)和时间片轮转调度都需要用到它. Cortex-M系列的内核都有一个systick时钟,这个 ...

  10. LeetCode - 769. Max Chunks To Make Sorted

    Given an array arr that is a permutation of [0, 1, ..., arr.length - 1], we split the array into som ...