Additional Blogs by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

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.
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
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.
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.

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.

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.
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 zastverfh-physinv VALUE '1000000000'.
DATA lv_gjahr TYPE zastverfh-gjahr 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( sy-datum ).
CALL METHOD lo_astverf_obj->set_crttime( sy-uzeit ).
CALL METHOD lo_astverf_obj->set_crtuname( sy-uname ).
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( sy-datum ).
CALL METHOD lo_astverf_obj->set_crttime( sy-uzeit ).
CALL METHOD lo_astverf_obj->set_crtuname( sy-uname ).
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 ).

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( ).

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: / zastverfh-physinv,
        zastverfh
-gjahr,
        zastverfh
-crtdate,
        zastverfh
-crttime,
        zastverfh
-crtuname,
        zastverfh
-hstatus.

     
ENDLOOP.

   
ENDIF.

 
CATCH cx_os_object_not_found.

ENDTRY.

2 Comments