Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Persistent Classes

To use the Persistence Service for objects, the classes of these objects must be created as persistent
classes in the Class Builder. The term persistent class does not imply that a class is persistent. (As a
template for objects, every class is persistent). Rather, it means that the objects of that class and their
state are managed by the Persistence Service. For example, the objects of these classes are instantiated
in the ABAP program with a method of the Persistence Service, which ensures that the initialization is
correct (not with the usual CREATE OBJECT statement). When the Class Builder creates a persistent
class, it automatically generates an associated class, known as the class actor or class agent, whose
methods manage the objects of persistent classes. As well as their identity, persistent classes can contain
key attributes, which allow the Persistence Service to ensure that the content of each persistent object is
unique.

Figure 1: Transaction SE24 (Class


Builder)

Figure 2: Specifying the Class Attributes


Persistent Object Service - Example

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.

You might also like