简体   繁体   English

如何为持久性类创建非持久性抽象超类

[英]How to create a not persistent abstract superclass for a persistent class

I wanted to create an abstract super class for my persistent classes, which provides some functions to query against any other persistent class which is member of that class. 我想为我的持久性类创建一个抽象超类,它提供了一些函数来查询属于该类成员的任何其他持久性类。

So I tried to add this abstract class as superclass, when I got following error: 因此,当出现以下错误时,我尝试将这个抽象类添加为超类:

The superclass of a persistent class must be persistent 持久类的超类必须是持久的

Message no. 讯息编号 OO629 OO629

Is there some kind of good workaround? 有某种好的解决方法吗? (I mean something diffrent from making an Interface and Copy-Paste the functions into each subclass) (我的意思是与制作接口和将函数复制粘贴到每个子类中有所不同)

Here some additional info: 这里是一些其他信息:

Here is what I want to do (Try IE if it does not work with chrome or friefox 这是我想做的(如果IE无法与chrome或friefox一起使用,请尝试IE。

Short Abstract: 简短摘要:

  • (I am working in user ) (我正在user工作)
  • Creating a general function to query against every key. 创建一个通用函数来查询每个键。
  • Creating a more specific function using the general function to query all the "links" from a join table( user_has_tag ), filling them into a attribute 使用通用函数创建更具体的函数,以查询user_has_tag表中的所有“链接”( user_has_tag ),并将其填充到属性中
  • Query the other foreignkey ( tag ) using the general function again. 再次使用常规功能查询另一个外键( tag )。

Here my abstract superclass (with the general function QUERY_BY_UUID : 这是我的抽象超类(具有一般功能QUERY_BY_UUID

CLASS zcl_ps_hrmobject DEFINITION
  PUBLIC
  ABSTRACT
  CREATE PUBLIC .

  PUBLIC SECTION.

    METHODS get_uuid
      RETURNING
        VALUE(ro_uuid) TYPE uuid .
  PROTECTED SECTION.

    METHODS query_by_uuid
      IMPORTING
        !ir_agent         TYPE REF TO object
      RETURNING
        VALUE(rt_entries) TYPE osreftab
      RAISING
        cx_os_object_not_found
        cx_os_query_error .
    METHODS get_query
      RETURNING
        VALUE(rr_query) TYPE REF TO if_os_query .
  PRIVATE SECTION.

    CONSTANTS lc_query_method_name TYPE string VALUE 'IF_OS_CA_PERSISTENCY~GET_PERSISTENT_BY_QUERY'. "#EC NOTEXT
    DATA mlr_query TYPE REF TO if_os_query .
ENDCLASS.



CLASS zcl_ps_hrmobject IMPLEMENTATION.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Protected Method ZCL_PS_HRMOBJECT->GET_QUERY
* +-------------------------------------------------------------------------------------------------+
* | [<-()] RR_QUERY                       TYPE REF TO IF_OS_QUERY
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD get_query.
    rr_query = me->mlr_query.
  ENDMETHOD.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_PS_HRMOBJECT->GET_UUID
* +-------------------------------------------------------------------------------------------------+
* | [<-()] RO_UUID                        TYPE        UUID
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD get_uuid.
* must be implemented in subclass

    RAISE EXCEPTION TYPE cx_os_no_implementation
*      EXPORTING
*        textid =
*        previous =
      .
  ENDMETHOD.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Protected Method ZCL_PS_HRMOBJECT->QUERY_BY_UUID
* +-------------------------------------------------------------------------------------------------+
* | [--->] IR_AGENT                       TYPE REF TO OBJECT
* | [<-()] RT_ENTRIES                     TYPE        OSREFTAB
* | [!CX!] CX_OS_OBJECT_NOT_FOUND
* | [!CX!] CX_OS_QUERY_ERROR
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD query_by_uuid.
    DATA: lr_query TYPE REF TO if_os_query,
          lr_uuid  TYPE uuid.

    lr_query = me->get_query( ).
    lr_uuid  = me->get_uuid( ).

*TRY.
    CALL METHOD ir_agent->(lc_query_method_name)
      EXPORTING
        i_query = lr_query
*       i_parameter_tab =
        i_par1  = lr_uuid
*       i_par2  =
*       i_par3  =
*       i_subclasses    = OSCON_FALSE
*       i_upto  = 0
*       i_options       = IF_OS_QUERY_OPTIONS=>DEFAULT_OPTIONS
      RECEIVING
        result  = rt_entries.
* CATCH cx_os_object_not_found .
* CATCH cx_os_query_error .
*ENDTRY.


  ENDMETHOD.
ENDCLASS.

否。您的描述听起来像您可能想仔细看一下生成的代理类和查询服务提供的方法。

I solved the problem with an interface and a static helperclass. 我通过接口和静态帮助器类解决了该问题。

The interface enforces three getter: 该接口强制执行三个getter:

get_uuid ( )
get_query ( )
get_query_method_name ( )

Now each Element implements this interface. 现在,每个元素都实现此接口。

In the helperclass I now have the Query_by_uuid function I wanted to heritate, which gets a Instance type of that interface ( ir_root_instance ). 在helperclass中,我现在具有要继承的Query_by_uuid函数,该函数获取该接口的Instance类型( ir_root_instance )。

Here the function 这里的功能

  METHOD QUERY_BY_UUID.
    DATA: lr_query             TYPE REF TO if_os_query,
          lr_uuid              TYPE uuid,
          lv_query_method_name TYPE string.

    lr_query             = ir_root_instance->get_query( ).
    lr_uuid              = ir_root_instance->get_uuid( ).
    lv_query_method_name = ir_root_instance->get_query_method_name( ).

*TRY.
    CALL METHOD ir_agent->(lv_query_method_name)
      EXPORTING
        i_query = lr_query
*       i_parameter_tab =
        i_par1  = lr_uuid
*       i_par2  =
*       i_par3  =
*       i_subclasses    = OSCON_FALSE
*       i_upto  = 0
*       i_options       = IF_OS_QUERY_OPTIONS=>DEFAULT_OPTIONS
      RECEIVING
        result  = rt_entry.
* CATCH cx_os_object_not_found .
* CATCH cx_os_query_error .
*ENDTRY.


  ENDMETHOD.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM