Shows how to use the Persistent object in the Application.
Previously, we have seen
Persistent Object Services - Basics. Today we will see how to use the Persistent Object services in the test application. You can find all these under
ABAP Objects.
To use the persistent objects, we need to:
Get the Agent Object by accessing the public attribute AGENT in the agent class.
For Read access, get the persistent object by using the GET_PERSISTENT method of the agent object. To create entry using the persistent objects, we need to use the CREATE_PERSISTENT method of the agent object.
Lets see it by example. For demo purpose, we will use the class CL_SPFLI_PERSISTENT.
| Code Snippet to show the use of the Persistent Object |
*&---------------------------------------------------------------------* *& Shows how to use the Persistent Service to work with Persistent *& objects to get data and create entries. *&---------------------------------------------------------------------* * REPORT ztest_persistent. * DATA: lo_spfli_a TYPE REF TO ca_spfli_persistent, " Actor Class lo_spfli_c TYPE REF TO cl_spfli_persistent, " Persistent Class lo_exc TYPE REF TO cx_root. " Exception * DATA: la_cityfrom TYPE spfli-cityfrom. * START-OF-SELECTION. * * Get the Agent lo_spfli_a = ca_spfli_persistent=>agent. * ***** Reading the Entry TRY. * Get the Persistent object for key lo_spfli_c = lo_spfli_a->get_persistent( i_carrid = 'AA' i_connid = '0017' ). * * Print the CITYFORM, if the persistent object is created IF lo_spfli_c IS BOUND. la_cityfrom = lo_spfli_c->get_cityfrom( ). WRITE: 'City From:', la_cityfrom. * * Write: No data message ELSE. WRITE: 'No data found'. ENDIF. * * Exception handling CATCH cx_root INTO lo_exc. MESSAGE lo_exc TYPE 'S'. ENDTRY. CLEAR: lo_spfli_a, lo_spfli_c. * ***** Creating the Entry * Get the Agent (same agent object due to Singleton) lo_spfli_a = ca_spfli_persistent=>agent. * TRY. * Get the Persistent object for key lo_spfli_c = lo_spfli_a->create_persistent( i_carrid = 'ZZ' i_connid = '0017' ). * * set the CITYFROM lo_spfli_c->set_cityfrom( 'NY' ). * * Exception handling CATCH cx_root INTO lo_exc. MESSAGE lo_exc TYPE 'S'. ENDTRY. * * Required to coommit the changes to database COMMIT WORK.
|
You can check the standard program DEMO_CREATE_PERSISTENT.
Some facts:- Singleton Design pattern is used for the Agent object in the agent class.
- We need to call the COMMIT WORK in order to update our changes into the database table
- If the entry doesn't exist in the database than resulting object of method GET_PERSISTENT would not be bound.