Skip to Content

The nature and longevity of the data in ABAP programs give them a unique name, if the data is alive as long as the context of the subroutine, or program then it is called transient data. Once the program is out of the context the values no longer exist. If the data needs to be preserved beyond the context / runtime of the program then it is called persistent data. This persistence is achieved in practice by storing the state as data in non-volatile storage such as a hard drive or flash memory and in practical scenarios what we call them as “databases”.

ABAP Objects are transient in nature and its scope is alive from the time the object is created using the “CREATE OBJECT” keyword until the system program (referred to as garbage collector) collects and destroys them. The Persistence Object Services can be considered as a logical layer between the ABAP program and the database. This POS (Persistent Object Services) allow you to save the attributes of objects with a unique identity, and then load them again when you need them.

2 - Persistent Objects - Architecture Diagram.jpg

Persistence Service as a logical layer

 

Persistence Classes:

In Class Builder, there is an option to create a “Persistent Class” and once created, the system automatically creates a Class Actor or Class Agent which has the following inheritance relationships.

3 - Persistent Objects - Create Class.png

The moment you create a Persistent Class, workbench also generates two additional classes: 

  • ZCB_PERSIST_SFLIGHT as the Base Class
  • ZCA_PERSIST_SFLIGHT as the Agent Class or Actor Class

The objects of persistent classes are managed by the Persistence Service. The objects are instantiated with a method of the class actor (ZCA_PERSIST_SFLIGHT), and not with the CREATE OBJECT statement. These objects are called Managed Objects.

1 - Persistent Objects - Class Diag.jpg

Transparent Table / Structure:

Create a Transparent Table / Structure with either the Busuness Key or GUID to uniquely identify the data in the persistent object. We will take the example of the SFLIGHT table and copy it into a Z-Table and rename it ZTB_SFLIGHT. Also add a new key field GUID.

This table will be used to store data from the persistent objects.

4 - Persistent Objects - Table Def..jpg

Mapping Data from the Persistent Class

Now that we have a Persistent Class and also an Actor Class to create the managed objects, we need to know what data needs to be persistent in nature, This is achieved by creating a “Persistence Mapping” between the Objects and the correcponding database tables.

This can be done using either of the three ways :

1. Using a Business Key

2. Using GUID

3. Using Both (i.e. Business Key and GUID)

We need to make use of the mapping assistant tool for all of the above mentioned ways.

5 - Persistent Objects - Mapping.jpg

5a - Persistent Objects -Mapping.jpg

The data in these persistent objects is still not persistent unless it encounters a COMMIT WORK statement.

Methods:

The “GET”ers and “SET”ers are automatically created in the class once the mapping is saved and activated. Since we create objects of the Persistent Class using the actor class, the methods GET_PERSISTENT, CREATE_PERSISTENT and DELETE_PERSISTENT must be used to perform the fetch, create and delete records from the database tables using  the Persistent Object Services

Sample Programs:

1. Using Business Key  : http://wiki.sdn.sap.com/wiki/pages/viewpage.action?pageId=279937251:

2. Using GUID             : http://wiki.sdn.sap.com/wiki/pages/viewpage.action?pageId=279937997

To report this post you need to login first.

5 Comments

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

  1. akshath lt

    Hi Subrahmanya Pindiproli,

    Very nice document and easy to understand about persistence class.

    Thanks for sharing your knowledge with us.

    All the best for future document.

    Regards,

    Akshath

    (0) 
  2. Kushi Mishra

    Hi Subrahmanya,

    Its really a very helpful article on Persistent classes.

    I have a query on the article. Kindly help me understanding that.

    In the table ZTB_SFLIGHT, we have the following fields as Primary keys

    GUID, CARRID, CONNID, FLDATE

    But when the Persistent class was created only GUID happened to the unique identifier of the persistent object whereas other primary keys (CARRID, CONNID, FLDATE) are not shown on Persistent Mapper as  Business Key (with Green Icon), When i tried creating a persistent class with Guid as well as other keys it didnt activate. I had to go to table ZTB_SLFIGHT and make the fields CARRID,CONNID and FLDATE as Non Key fields and then create the persistent class again with Only GUID as Primary Key field

    Kindly reply.

    Sincere Regards,

    Rakshan

    (0) 

Leave a Reply