Escolar Documentos
Profissional Documentos
Cultura Documentos
SAP definition
The Persistence Service lets the ABAP programmer work with relational database data in an object-
oriented way.
If you have any experiences in other programming languages, like Java or C#, then
maybe you have heard about ORM (Object-Relational Mapping) frameworks. These
ORM tools enable you to construct classes with attributes, and then the framework will
automatically generate the corresponding database tables in your database, and
represent the data like objects.
Unfortunately, SAP Persistence Service isn't capable to act completely like above, but
it's still a good initiative from SAP, and I am really excited to see the further
improvements.
SAP says
You may have to create new database tables, database views, or structures for the object model in
the ABAP Dictionary. At present the system does not perform this function automatically.
So, I wouldn't say that SAP Persistence Service it's an ORM tool, instead let's consider
it as a layer (data access) in our application architecture that enables us to work with
our relational database as ABAP Objects.
Today, I am going to introduce you the SAP Persistence Service using the database
table, called SCARR (Airline). We are going to create a Persistent Class for Airlines,
and I will show you its usage on very simple examples to ensure that you understand it
easily.
The ZCL_OS_SCARR is our Persistent Class that we are going to create later in this
tutorial. It has to implement the interface IF_OS_STATE, that ensures that you can
access all of your persistent objects in the same way. This class has as many attributes
and GET/SET methods as many field exists in its connected persistent structure (table,
view, structure) generated by the Class Builder.
For each Persistent Class two agent classes are generated by the Class Builder, an
abstract and a final.
The ZCA_OS_SCARR is one of our Agent classes, and it's responsible is to manage
our ZCL_OS_SCARR Persistent Objects (actually all the SQL statements take place in
this Agent class), it serves as a data repository.
In SE24, in the Class Builder, let's create a new class with the
name, ZCL_OS_SCARR(since it's going to be the persistent class of the database
table, called SCARR), and choose the class type, Persistent Class.
INTERFACES
After saving our new class, we can check what exactly Persistent Class means. It
implements the interface, IF_OS_STATE (like I showed you above).
FRIENDS
It is also friend of our abstract Agent class, ZCB_OS_SCARR.
After applying the same workflow for each field, we get the following attributes under our
class:
NEW ATTRIBUTES
After saving the persistence mapping, we can go back to our class definition, where we
can check the generated attributes, based on the mappings.
In the core of the TRY-CATCH block, we ask a reference of our agent class and save it
in the reference variable airline_agent, by querying the class attribute of
the ZCA_OS_SCARR, called agent (ZCA_OS_SCARR follows the singleton pattern).
Then, we simply ask the agent to get a persistent from the database with the business
key 'BA', and we save the receiving reference of the airline instance in the reference
variable, called airline.
From now on, we have an initialized instance of the ZCL_OS_SCARR (data came from
database), and we can call one of its GET/SET method, like GET_CARRNAME().
TRY.
airline_agent = zca_os_scarr=>agent.
airline = airline_agent->get_persistent( 'BA' ).
airline_name = airline->get_carrname( ).
WRITE: 'Name of the selected airline: ', airline_name.
CATCH cx_os_object_not_found.
" error handling comes here
ENDTRY.
It's very effective, and it's also reusable among different clients, like ABAP programs,
ABAP classes, Function Modules, and so on.
Then we ask the agent to create a persistent in the database with the following
parameters: 'WA', 'Wizz Air', 'HUF', 'http://wizzair.com'.
At last, there is nothing left then commit our work, and as a result a new record appears
in our table.
COMMIT WORK.
CATCH cx_os_object_existing.
" error handling comes here
ENDTRY.