Show TOC

Background documentationGenerating a Persistent Object Locate this document in the navigation structure

 

Syntax Syntax

  1. report DEMO_CREATE_PERSISTENT.
  2. selection-screen begin of screen 400 title TEXT-400.
  3. parameters DELETE as checkbox.
  4. selection-screen end of screen 400.
  5. selection-screen begin of screen 500 title TEXT-500.
  6. parameters COMMIT as checkbox.
  7. selection-screen end of screen 500.
  8. data WA_SPFLI type SPFLI.
  9. data: CONNECTION type ref to CL_SPFLI_PERSISTENT,
  10.         AGENT      type ref to CA_SPFLI_PERSISTENT.
  11. data: EXC type ref to CX_ROOT,
  12.       TEXT type STRING.
  13. WA_SPFLI-CARRID     = 'LH'.
  14. WA_SPFLI-CONNID     = '123'.
  15. WA_SPFLI-COUNTRYFR  = 'DE'.
  16. WA_SPFLI-CITYFROM   = 'FRANKFURT'.
  17. WA_SPFLI-AIRPFROM   =  'FRA'.
  18. WA_SPFLI-COUNTRYTO  = 'SG'.
  19. WA_SPFLI-CITYTO     = 'SINGAPORE'.
  20. WA_SPFLI-AIRPTO     = 'SIN'.
  21. WA_SPFLI-FLTIME     = '740'.
  22. WA_SPFLI-DEPTIME    = '234500'.
  23. WA_SPFLI-ARRTIME    = '180000'.
  24. WA_SPFLI-DISTANCE   = '10000'.
  25. WA_SPFLI-DISTID     = 'KM'.
  26. WA_SPFLI-FLTYPE     = ' '.
  27. WA_SPFLI-PERIOD     = '1'.
  28.     AGENT = CA_SPFLI_PERSISTENT=>AGENT.
  29. try.
  30.     CONNECTION = AGENT->GET_PERSISTENT(  I_CARRID = WA_SPFLI-CARRID
  31.                                         I_CONNID = WA_SPFLI-CONNID ).
  32.     message 'Connection already exists' type 'I'.
  33.     call selection-screen 400 starting at 10 10.
  34.     if DELETE = 'X'.
  35.       try.
  36.           AGENT->DELETE_PERSISTENT( I_CARRID = WA_SPFLI-CARRID
  37.                                    I_CONNID = WA_SPFLI-CONNID ).
  38.           commit work.
  39.         catch CX_ROOT into EXC.
  40.           TEXT = EXC->GET_TEXT( ).
  41.           message TEXT type 'I'.
  42.       endtry.
  43.     endif.
  44.   catch CX_ROOT into EXC.
  45.     TEXT = EXC->GET_TEXT( ).
  46.     message TEXT type 'I'.
  47.     try.
  48.         CONNECTION = AGENT->CREATE_PERSISTENT(
  49.                        I_CARRID = WA_SPFLI-CARRID
  50.                        I_CONNID = WA_SPFLI-CONNID
  51.                        I_COUNTRYFR = WA_SPFLI-COUNTRYFR
  52.                        I_CITYFROM = WA_SPFLI-CITYFROM
  53.                        I_AIRPFROM = WA_SPFLI-AIRPFROM
  54.                        I_COUNTRYTO = WA_SPFLI-COUNTRYTO
  55.                        I_CITYTO = WA_SPFLI-CITYTO
  56.                        I_AIRPTO = WA_SPFLI-AIRPTO
  57.                        I_FLTIME = WA_SPFLI-FLTIME
  58.                        I_DEPTIME = WA_SPFLI-DEPTIME
  59.                        I_ARRTIME = WA_SPFLI-ARRTIME
  60.                        I_DISTANCE = WA_SPFLI-DISTANCE
  61.                        I_DISTID = WA_SPFLI-DISTID
  62.                        I_FLTYPE = WA_SPFLI-FLTYPE
  63.                        I_PERIOD = WA_SPFLI-PERIOD            ).
  64.         call selection-screen 500 starting at 10 10.
  65.         if COMMIT = 'X'.
  66.           commit work.
  67.         endif.
  68.         message 'Connection created' type 'I'.
  69.       catch CX_ROOT into EXC.
  70.         TEXT = EXC->GET_TEXT( ).
  71.         message TEXT type 'I'.
  72.     endtry.
  73. endtry.
End of the code.

A reference to the class actor of persistent class CL_SPFLI_PERSISTENT is assigned to the reference variable AGENT. The static constructor of the class CA_SPFLI_PERSISTENT generates this reference once only. The GET_PERSISTENT method checks whether there is a persistent object with this key in the database already. If so, it can be deleted using DELETE_PERSISTENT. If not, the system raises and catches the exception CX_OS_OBJECT_NOT_FOUND. Then it tries to create the object in the relevant CATCH block using CREATE_PERSISTENT. Note that the object is created in the database only when the COMMIT WORK statement is executed. If there is no COMMIT WORK statement, the program retains only the managed object, which is deleted from the database after the program ends (see Transaction Mode).