Skip to Content

Using Persistent Class for CRUD(Create, Read, Update and Delete) Operations

Introduction

In contrast to the traditional SELECT and UPDATE query, SAP also provides the facility to use PERSISTENT Classes in order to perform CRUD(Create, Read, Update and Delete) operations.
 
The following blog will take you through the steps on how to perform CRUD operations on a TABLE using persistent objects.

Step by Step

Table which will be manipulated using the Persistent Class

I have selected an already created table in my system, you can create your own and use and existing one.
PersistenClassFig1.png
Figure 1

Creating the Persistent Class


Persistent classes are like normal classes and are created using the class builder (se24). The only difference while creating a persistent class is the tick mark Persistent Class, which identifies the class as a persistent class.

Goto SE24 write a name and then press the create button, select the Class type as Persistent Class and press SAVE as shown in Figure 2.

On Save system will generate two more classes along with the Persistent Class

Agent class ZCA_ASSET_VERF_DOC_HEADER and the

Base class ZCB_ASSET_VERF_DOC_HEADER
PersistenClassFig2.png
Figure 2

Since the Persistent class ZCL_ASSET_VERF_DOC_HEADER is a Protected class, system creates ZCA_(Agent class), which is used to get the
instance of the persistent class and ZCB_(Base class), which is an abstract class from which the agent class is derived.

This is done since the persistent class is kept by the system as a managed object and its class cannot be instantiated directly using the CREATE OBJECT command. So when ever CREAT_PERSISTENT, GET_PERSISTENT etc methods of the agent class are called, a new instance of the persistent class
will be created and returned.

Linking the table with the persistent class

While editing ZCL_ASSET_VERF_DOC_HEADER press the persistence button in the toolbar and in the popup screen enter the table to which you want
link the class to as shown in Figure 3.

PersistenClassFig3.png
Figure 3

Add the fields you want to make persistent by double clicking the fields in the bottom section and then pressing the arrow button as shown in Figure 4.
PersistenClassFig4.png

Figure 4


Tips: Click the Generator Settings button in the toolbar and select the Generate Methods for Query checkbox as shown in Figure 5, this will generate the GET_PERSISTENT_BY_QUERY method as well.
PersistenClassFig5.png

Figure 5


On saving you will see that the system will generate Setter and Getter methods for all selected fields as shown in Figure 6.
PersistenClassFig6.png
Figure 6

Activate the class and you can now use it to Create, Read, Update and Delete data from ZASTVERFH table.

Test program to Create, Read, Update and Delete from table using Persistent Class

Create:


DATA lo_astverf_obj TYPE REF TO zcl_asset_verf_doc_header.
DATA lv_physinv TYPE zastverfhphysinv VALUE ‘1000000000’.
DATA lv_gjahr TYPE zastverfhgjahr VALUE ‘2010’.

TRY .

lo_astverf_obj = zca_asset_verf_doc_header=>agent->create_persistent(

                            i_physinv = lv_physinv                                                                                                                               
                            i_gjahr = lv_gjahr ).

CALL METHOD lo_astverf_obj->set_crtdate( sydatum ).
CALL METHOD lo_astverf_obj->set_crttime( syuzeit ).
CALL METHOD lo_astverf_obj->set_crtuname( syuname ).
CALL METHOD lo_astverf_obj->set_hstatus( ‘I’ ).

COMMIT WORK.

CATCH cx_os_object_existing.

ENDTRY.

Update:

For updation everything remains the same only you need to replace the CREATE_PERSISTENT method with the GET_PERSISTENT  method and the exception will change from cx_os_object_existing to cx_os_object_not_found.

TRY.

lo_astverf_obj = zca_asset_verf_doc_header=>agent->get_persistent(

                           i_physinv = lv_physinv                                                   

                          i_gjahr = lv_gjahr ).

CALL METHOD lo_astverf_obj->set_crtdate( sydatum ).
CALL METHOD lo_astverf_obj->set_crttime( syuzeit ).
CALL METHOD lo_astverf_obj->set_crtuname( syuname ).
CALL METHOD lo_astverf_obj->set_hstatus( ‘I’ ).

COMMIT WORK.

CATCH cx_os_object_not_found.

ENDTRY.

Read:


Code for Read is same as update, the only change is we will use GETTER methods instead of SETTER methods.

TRY .
lo_astverf_obj
= zca_asset_verf_doc_header=>agent->get_persistent(

                           i_physinv = lv_physinv
                          i_gjahr
= lv_gjahr ).

zastverfhcrtdate = lo_astverf_obj->get_crtdate( ).
zastverfh
crttime = lo_astverf_obj->get_crttime( ).
zastverfh
crtuname = lo_astverf_obj->get_crtuname( ).
zastverfh
hstatus = lo_astverf_obj->get_hstatus( ).

CATCH cx_os_object_not_found.

ENDTRY.

Delete:

For deletion use method DELETE_PERSISTENT and exception CX_OS_OBJECT_NOT_EXISTING.

 

TRY .
zca_asset_verf_doc_header
=>agent->delete_persistent(

     i_physinv = lv_physinv
    i_gjahr
= lv_gjahr ).

COMMIT WORK.

CATCH cx_os_object_not_existing.

ENDTRY.

Read Multiple Records:

Here instead of using the GET_PERSISTENT method we use the GET_PERSISTENT_BY_QUERY method, which will return us the list of objects. Each
record in the database will correspond to an object in the internal table.

 

DATA lt_obj TYPE osreftab.
DATA ls_obj TYPE osref.

 
TRY .
    lt_obj
= zca_asset_verf_doc_header=>agent->if_os_ca_persistency~get_persistent_by_query(
                  i_par1
= ‘5%’
                  i_query
= cl_os_system=>get_query_manager( )->create_query(
                                    i_filter
= ‘PHYSINV LIKE PAR1’ ) ).


   
IF lines( lt_obj ) <> 0.

      LOOP AT lt_obj INTO ls_obj.

        lo_astverf_obj ?= ls_obj.


        zastverfh
physinv = lo_astverf_obj->get_physinv( ).
        zastverfh
gjahr = lo_astverf_obj->get_gjahr( ).
        zastverfh
crtdate = lo_astverf_obj->get_crtdate( ).
        zastverfh
crttime = lo_astverf_obj->get_crttime( ).
        zastverfh
crtuname = lo_astverf_obj->get_crtuname( ).
        zastverfh
hstatus = lo_astverf_obj->get_hstatus( ).

        WRITE: / zastverfhphysinv,
        zastverfh
gjahr,
        zastverfh
crtdate,
        zastverfh
crttime,
        zastverfh
crtuname,
        zastverfh
hstatus.

      ENDLOOP.

    ENDIF.

  CATCH cx_os_object_not_found.

ENDTRY.

To report this post you need to login first.

1 Comment

You must be Logged on to comment or reply to a post.

  1. Joseph BERTHE

    Hello,

    Thanks for your blog, it is very helpfull.

    However, in the Read Multiple Records section, I tried to use the parameter PAR1 but it seams it doesn’t work, in DEBUG, the query is something like this :

    >> PHYSINV LIKE PAR1

    PAR1 is not replace by i_par1.

    Regards,

    (0) 

Leave a Reply