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.
Persistence Service as a logical layer
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.
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.
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.
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.
The data in these persistent objects is still not persistent unless it encounters a COMMIT WORK statement.
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
1. Using Business Key : http://wiki.sdn.sap.com/wiki/pages/viewpage.action?pageId=279937251: