How to create a persistent objects in SAP?

Creating a persistent object

ABAP objects provide a persistent object service that allows the developer to store objects in
the database. 

The values of the attributes of the object are stored in appropriate fields of the
database table specified.

Prior to storing objects in the database, a suitable database table with the name
ZEMP_TABLE is created to store the values of the objects' attributes. 

Two fields are defined, NUMBER1 and NAME (the field name NUMBER was not allowed, so NUMBER1 has been used as the field name). Refer to the following screenshot:



Once the database table is defined, a persistence class must be defined. 
In order to define persistent classes, follow these steps:

Call transaction SE24. Enter a suitable name of the persistent class to be created.

We will create a class by entering the name ZCL_MY_PERSIST. 
Enter the name in the Class field and click on the Create button.



Enter a suitable description in the field provided. Make sure that the Persistent Class
indicator is selected, and click on Save.


The programmer may only modify the methods HANDLE_EXCEPTION and INIT.


Click on the Persistence button. 

Then, enter the name of the table that was created for storage of data( in our case, we will enter the name ZEMP_TABLE). Refer to the following screenshot:



This will take you to the mapping editor. 

The lower part of the screen will show Table/Fields. 

Double-click each of the field that is to be included and stored as attributes of the persistent class. 

The selected field appears in the area earlier (for example, the NUMBER1 field as shown in the following screenshot). 

Click on the Set attribute values button to include the field.




This will transfer the selected field in the topmost area of the editor.

Similarly, the NAME field must be included.




All the mapped fields will appear at the top area of the mapper. 

The Number1 field will appear as a business key, as show in the following screenshot:




Upon activation of the persistence class, the system asks for activation of the actor class as well. 

Click on Yes, as shown in the following screenshot:



The class ZCL_MY_PERSIST is created and necessary methods needed for the persistence service are included. An actor class is also created with the class. 


The agent class has been generated by the name ZCA_MY_PERSIST. 

There is one base agent class generated as a result. In total, three classes are generated, the persistent class, the agent class, and the base class of the agent.



The class ZCL_MY_PERSIST contains methods for setting and getting the values of the attributes NAME and NUMBER1. 

Note that no SET method is generated for the key field, in our case NUMBER1.



The agent class provides number of useful methods related to the persistent property. Important methods, such as create_persistent, delete_persistant, and get_persistent are provided. 

The methods are implemented in the superclass zcb_my_persist of the agent class zca_my_persist.




During the generation of the persistent class zcl_my_persist, two additional classes are generated. 

These are the actor (agent) and the base agent classes having the names zca_my_persist and zcb_my_persist respectively.  

The base agent class is generated as abstract (that is, no instance can be constructed from it), and cannot be modified. 

It is created in a separate pool class from zcl_my_persist. 

The agent class zca_my_persist may be extended, as well as the loading and saving methods may be modified.


The instantiation mode of the persistence class may be set as abstract or protected. 

In our recipe, we have chosen the instantiation mode as protected (which means that only instances
may be created from within the class or its subclasses). 

However, making the instantiation mode of a persistent class as protected makes the generated base agent class a friend of the persistent class (in the world of ABAP objects, a friend or its subclasses may create instances of the class in question).


The coding for this recipe declares two references, emp and agent, to the persistent class zcl_my_persist and the agent class zca_my_persist, respectively. 

Next, the static factory method agent is called for the class zca_my_persist (agent class). 

The reference returned is stored in the variable agent. The agent class contains the method create_persistent required for storing the data into the database (this is analogous to the concept of insertion in database table).  

The most important part is the calling of the create_persistent method that is passed the number and name that is to be stored. 

The employee with the number 00000017 and name John Reed is created and reference is returned in emp.

One row with the number and a name is added to the table ZEMP_TABLE.



For reading the stored value related to the employee number 00000017, a number variable is declared and assigned the value 00000017. The static method agent of the zca_my_persist class is called in order to get a reference to the agent. 


The get_persistent method is then called and the number (in our case, 00000017)
is passed. 

This method returns the entire object emp pertaining to the employee number. 

You may then call the get_name method of the zcl_my_persist class for the emp object in order to retrieve the employee name.








No comments:

Post a Comment