Object Services: Persistence and Transaction Services
|
This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production. |
Most business data lives in database tables, but ABAP programs that use classes would like to work with objects: "this claim", with its status and its total, and methods that act on it. Object Services are a layer, written in ABAP Objects itself, that lets a program treat a table row as an object. You create a special kind of class, a persistent class, tell the Class Builder which table columns belong to which attributes, and the system generates the code that loads the row into an object and writes it back. Two more services sit beside it: one that bundles changes into transactions, and one that finds objects with a query. You meet Object Services mostly in older code that you have to read or maintain, because they belong to the classic way of programming. This page is written against Standard ABAP release 8.16. Object Services exist in classic Standard ABAP, on premise and in private editions. They are not part of ABAP Cloud: the Cloudification Repository classifies their central classes and interfaces as classic APIs, not as released ones, and new development uses RAP business objects instead. The listings on this page need persistent classes generated in the Class Builder of an SAP system, so each one is labelled as requiring an SAP system and was not run here.
Before you read
-
Classes and Objects: what a class, an instance, a reference variable and a static attribute are.
-
Interfaces and Inheritance: the services below are used through interface reference variables.
-
Database Tables and Structures: the table
zexp_claimthat the running example maps to a class. -
ABAP SQL: Writing Data: database LUWs, SAP LUWs and
COMMIT WORK, because the transaction service is built on them. -
Exceptions: Object Services report errors with class-based exceptions.
Key terms
-
Object Services: a software layer in ABAP Objects that provides persistence, transactions and queries for objects.
-
Persistent class: a class created in the Class Builder whose objects are managed by the persistence service and can be stored in the database.
-
Class agent: the generated class, also called class actor, that creates, loads and deletes the objects of one persistent class.
-
Mapping Assistant: the part of the Class Builder that links table columns to the attributes of a persistent class.
-
Business key: the key attributes of a persistent class that come from the primary key of its table and are visible to the program.
-
Instance GUID: a technical identifier of type
OS_GUIDthat the persistence service uses to tell the objects of a class apart. -
SAP LUW: a logical unit of work that spans several database LUWs and is ended by
COMMIT WORK.
Why Object Services exist
An ABAP object lives only in the internal session of the program that created it. When the program ends, or the garbage collector finds no more references, the object is gone. A table row is the opposite: it stays until somebody deletes it. A program that wants to "have" the claim as an object has to bridge that gap itself: SELECT the row into a work area, copy the fields into attributes, and write them back with UPDATE after every change. The Keyword Documentation describes this as data that exists twice, once transiently in the program and once persistently in the database, and in the meantime the two copies differ.
Object Services take that bridge over. The Keyword Documentation calls them an object-oriented framework for handling relational databases: instead of writing ABAP SQL statements, the program works with persistent objects whose attributes are mapped to database content, and the classes and interfaces of Object Services encapsulate the actual database access. Technically the objects are still transient. The documentation says there are no persistent objects in ABAP Objects as such; the persistence service makes it appear as if there were, by initialising an object from the row when it is requested and saving the object’s state when the program asks for it.
Object Services consist of three services.
| Service | Job |
|---|---|
Persistence service |
Creates, loads, changes and deletes the objects of persistent classes, and keeps one object per key in the program. |
Transaction service |
Bundles the changes to persistent objects into object-oriented transactions and decides when they are written. |
Query service |
Finds persistent objects with a logical condition and loads the matches. |
Where Object Services stand today
Object Services are a legacy technique. Three statements from the official sources place them:
-
The Keyword Documentation says that support of the Mapping Assistant has stopped and that no new mappings should be created. Persistent classes that exist keep working; new ones are not meant to be started.
-
The Cloudification Repository, SAP’s list of released and classic APIs, gives
CL_OS_SYSTEM,CX_OS_ERROR,IF_OS_STATE,IF_OS_FACTORY,IF_OS_QUERY,IF_OS_TRANSACTIONandIF_OS_TRANSACTION_MANAGERthe state classic API. That is not released, so ABAP for Cloud Development cannot use them.CL_OS_CA_COMMON, the common superclass of all class agents, has the same state (listing read on 2026-11-13). -
The Keyword Documentation warns that its Object Services examples may use artifacts that are not released as APIs for ABAP Cloud.
The current model is RAP, the RESTful Application Programming Model. A RAP business object describes its data in CDS view entities, its behaviour in a behavior definition, and the framework reads, changes and saves the data, takes the locks and manages the transaction. In both ABAP Cloud and Standard ABAP, new transactional code is written that way. The page RAP Overview and the pages after it cover RAP. If you open an old system you may still find persistent classes in packages from the 7.0 to 7.5 era, and then the rest of this page tells you how to read them. The book Complete ABAP by Kiran Bandari (SAP PRESS, 2016) treats Object Services in section 11.2 as an ordinary persistence technique for AS ABAP 7.4 and 7.5. That was fair in 2016. Today the persistence mapping, SET and GET methods and query strings it shows are legacy, and the official documentation wins where they differ.
Persistent classes and the Mapping Assistant
A persistent class is an ordinary global class with one extra attribute: in the Class Builder you mark it as persistent. The objects of such a class are called managed objects: you do not create them with NEW or CREATE OBJECT, you ask the class agent for them. Because only the agent creates them, the class is declared with CREATE PROTECTED (or abstract), and the agent is its friend.
The link between table columns and attributes is the persistence mapping, made in the Mapping Assistant, a tool inside the Class Builder. You choose the database tables, database views or structures, assign each column to an attribute, and the Class Builder generates:
-
a GET method and, for value attributes, a SET method for each attribute
-
the class agent
CA_<class>and its superclassCB_<class> -
the code in the agent that reads and writes the database
You must map every column of a table; if you want to manage only some columns with Object Services, you create a DDIC database view over them first.
Names follow the prefix of the class: in the running example, the persistent class zcl_exp_claim_persistent maps the table zexp_claim, and the generated agent classes are zca_exp_claim_persistent and zcb_exp_claim_persistent. A name in the customer namespace starts with Z, so the SAP prefixes CL_, CA_ and CB_ become ZCL_, ZCA_ and ZCB_.
The three kinds of key
How an object is identified depends on the mapping type. There are three.
| Mapping type | How objects are identified | Typical use |
|---|---|---|
Business key |
The key fields of the table become key attributes. A key attribute has a GET method but no SET method, so the key cannot change. The agent has |
Putting objects over existing tables, such as |
Instance GUID |
The table has exactly one key field, of type |
Objects that other objects own and that no program looks up by itself. |
Instance GUID and business key |
The table has an |
Objects that need both a technical identity and a semantic key. |
The mapping can also use several tables for one class (multitable mapping: all keys must have the same type), or a Dictionary structure instead of a table (structure mapping, where the programmer writes the access methods of the agent; it is needed when an object has a table-like attribute, which a flat table cannot hold). Persistent classes can have subclasses that inherit the mapping, either vertical (the added attributes go into additional tables with the same key) or horizontal (an abstract superclass whose subclasses each define the mapping). Private attributes of non-final persistent classes are not fully protected, because the agents of a subclass and its superclass are not in an inheritance relationship, so the documentation advises against keeping protected data in them.
Object references can also be stored. A reference to an object that has an instance GUID is stored in two OS_GUID fields, the class GUID that the Class Builder generates and the instance GUID; together they form an object ID (OID). The persistence service resolves such a reference and loads the target, but only when needed.
The parts of the persistence service
The class agent is the centre. It is a singleton: the first access to its static attribute AGENT makes the static constructor create exactly one instance for the program. Besides the class methods, it implements three general interfaces, IF_OS_FACTORY, IF_OS_CA_PERSISTENCY and IF_OS_CA_INSTANCE, so a program can handle the agents of different persistent classes through an interface reference variable. The persistent class itself implements IF_OS_STATE, the management interface. The methods you use most:
| Method | What it does |
|---|---|
|
Creates a new persistent object. Parameters follow the mapping: the business key is required, every other attribute is an optional importing parameter. With GUID mapping, a new instance GUID is generated. The system does not check whether the row already exists; the error shows only when saving. Check with |
|
Loads an object by business key. If an object with that key already exists in the program, the existing reference is returned and nothing is loaded again. It exists only for business-key mapping. |
|
Marks the object as deleted; the row is deleted at |
|
Create and return an object that the service manages but that has no connection to the database. |
|
Loads several objects selected by a query (see the query service below). |
|
Forces the object to be reloaded on its next access. |
|
Removes the object from the management of the persistence service. |
The parameter names of the generated methods follow the attribute names (in the listings below, i_claim_id for the key attribute claim_id); the Class Builder shows the exact signature once the class is generated.
Management states
Every managed object is in one of these states, which decides what the agent may do with it. The constants are in the type pool OSCON, and IF_OS_CA_INSTANCE~GET_STATUS reports the state.
| Constant | Value | Meaning |
|---|---|---|
|
0 |
A representative object; the data is not yet loaded. |
|
1 |
Persistent and new. |
|
2 |
Persistent and completely loaded. |
|
3 |
Persistent and changed. |
|
4 |
Persistent and deleted. |
|
10 |
Managed, but without a database connection. |
The diagram shows the typical paths only; the documentation’s table lists every combination, and a method applied to a state that does not allow it raises an exception. Two details matter in practice. First, after COMMIT WORK the attributes of the object in the program are initial, because IF_OS_STATE~INVALIDATE was called; they are loaded again from the database on the next GET. Second, a persistent object that you only keep a reference to is loaded lazily: an object that refers to another persistent object gets an instance for it, whose attributes are loaded when you first read them.
Creating, reading and changing an object
The next listing creates a claim, in a classic program in which nothing is configured: the first access starts Object Services in compatibility mode, which is explained in the next section. The program must therefore write COMMIT WORK itself.
DATA claim TYPE REF TO zcl_exp_claim_persistent.
TRY.
claim = zca_exp_claim_persistent=>agent->create_persistent(
i_claim_id = 'C0000001' ).
claim->set_status( 'N' ).
COMMIT WORK.
CATCH cx_os_object_existing.
MESSAGE 'Claim C0000001 exists already' TYPE 'E'.
ENDTRY.
Source: Keyword Documentation for Standard ABAP: OS - Components of the Persistence Service and Keyword Documentation for Standard ABAP: OS - Saving Persistent Objects. Requires an SAP system; not run here.
Read it from the top. zca_exp_claim_persistent=>agent is the agent; its method creates a managed object whose key is C0000001. set_status is a generated SET method: it changes the object and marks it as changed, but does not touch the database. Only COMMIT WORK does. Without that statement nothing is stored, and the object disappears when the program ends. CREATE_PERSISTENT itself never reads the table, so the exception CX_OS_OBJECT_EXISTING is raised when the same key is created twice in the program; a row that exists in the database shows up as an error only at the commit, as the table of methods said.
Loading and changing an existing claim uses GET_PERSISTENT:
DATA claim TYPE REF TO zcl_exp_claim_persistent.
TRY.
claim = zca_exp_claim_persistent=>agent->get_persistent( i_claim_id = 'C0000001' ).
DATA(status) = claim->get_status( ).
IF status = 'N'.
claim->set_status( 'A' ).
ENDIF.
COMMIT WORK.
CATCH cx_os_object_not_found.
MESSAGE 'Claim C0000001 not found' TYPE 'E'.
ENDTRY.
Source: Keyword Documentation for Standard ABAP: OS - Components of the Persistence Service. Requires an SAP system; not run here.
Three things are enough to read such code: the agent hands out the object, GET and SET methods read and change its state, and a commit saves it. The key attribute claim_id has only a GET method, so claim->set_claim_id( ... ) does not exist; a different claim is a different object.
Deleting is agent->delete_persistent( i_claim_id = 'C0000001' ) followed by COMMIT WORK; the row is removed at the commit and the object is no longer managed.
The Keyword Documentation states what Object Services do not do: there is no lock concept in the persistence service. Two programs can load the same claim, both change it, and the second commit overwrites the first. If that matters, the program has to set an SAP lock around the work, as described on Lock Objects and the SAP Lock Concept.
The transaction service
The persistence service decides what is stored; the transaction service decides when. It does not introduce a new transaction concept. The documentation says it is based on the SAP LUW and encapsulates the classic bundling: internally it still registers update function modules and COMMIT WORK runs them. It lets you write
t->start( ).
" change objects
t->end( ).
instead of registering update modules yourself.
Transaction modes
A program that uses persistent objects always has a transaction mode, and the whole behaviour of this section depends on it.
| Mode | E_EXTERNAL_COMMIT |
What triggers the save |
|---|---|---|
Compatibility mode |
|
The program writes |
Object-oriented mode |
|
|
To choose the mode explicitly, call CL_OS_SYSTEM=>INIT_AND_SET_MODES before the first access to the persistence service. It runs at most once per program; a second call raises an exception, and if the service was already started implicitly, the explicit call has no effect. LOAD-OF-PROGRAM or the constructor of an application class is a good place. A transaction that is flagged as an OO transaction (the field "OO transaction model") in the ABAP Workbench starts the program in object-oriented mode.
The same call sets the update mode with the parameter I_UPDATE_MODE.
| Value | Update |
|---|---|
|
Asynchronous, in the update task. |
|
Synchronous. |
|
Local. |
|
Direct save at |
Synchronous and local updates cannot be set in compatibility mode, because there you use COMMIT WORK AND WAIT or SET UPDATE TASK LOCAL. The method IF_OS_TRANSACTION~SET_MODE_UPDATE changes the update mode once, after the top-level transaction has started.
Running a transaction
A program does not use classes for the manager and the transaction; it uses the interfaces IF_OS_TRANSACTION_MANAGER and IF_OS_TRANSACTION. CL_OS_SYSTEM=>GET_TRANSACTION_MANAGER( ) returns the manager, and the manager’s CREATE_TRANSACTION returns a transaction.
CLASS lcl_claims DEFINITION.
PUBLIC SECTION.
CLASS-METHODS class_constructor.
CLASS-METHODS approve IMPORTING claim_id TYPE zexp_claim-claim_id.
ENDCLASS.
CLASS lcl_claims IMPLEMENTATION.
METHOD class_constructor.
cl_os_system=>init_and_set_modes( i_external_commit = oscon_false
i_update_mode = oscon_dmode_default ).
ENDMETHOD.
METHOD approve.
DATA(tm) = cl_os_system=>get_transaction_manager( ).
DATA(t) = tm->create_transaction( ).
TRY.
t->start( ).
DATA(claim) = zca_exp_claim_persistent=>agent->get_persistent( i_claim_id = claim_id ).
claim->set_status( 'A' ).
t->end( ).
CATCH cx_os_error.
t->undo( ).
ENDTRY.
ENDMETHOD.
ENDCLASS.
Source: Keyword Documentation for Standard ABAP: OS - Transaction Mode and Keyword Documentation for Standard ABAP: OS - Components of the Transaction Service. Requires an SAP system; not run here.
Here the class constructor puts the program in object-oriented mode before any persistent object is touched. In approve, START makes the transaction the top-level one, the object is changed, and END raises COMMIT WORK implicitly, which starts the update and invalidates the objects. Any CX_OS_ERROR (the common super class of the checked Object Services exceptions) leads to UNDO, which cancels the changes and returns the objects to their state before the transaction; in an object-oriented top-level transaction, UNDO also raises ROLLBACK WORK.
The rules of a transaction in short:
-
A transaction can be started once. After
ENDorUNDOits object is no longer usable; create a new transaction (the same variable can hold it). -
A transaction started while another is running is a subtransaction. It never causes
COMMIT WORK; its changes are written when the top-level transaction ends.UNDOof a subtransaction cancels only its own changes. -
A program has only one top-level transaction at a time. In compatibility mode it is the implicit one.
-
END_AND_CHAINandUNDO_AND_CHAINend the transaction and start a new one at once, and the persistent objects are not invalidated, which saves reloading them (object-oriented mode only). -
GET_STATUSreturnsOSCON_TSTATUS_NEW,_RUNNING,_END_REQ,_FIN_SUCCESS,_FIN_UNDOor_FIN_ABORT; the last means thatUNDOran but the objects could not be reset, because the undo mechanism was switched off withSET_MODE_UNDO_RELEVANT. -
Events
SAVE_REQUESTED,SAVE_PREPAREDandFINISHEDtell a program when saving starts, when it is done and when the transaction is over.
Classic and object-oriented code can be mixed. In a classic program, the first use of a persistent object creates an implicit top-level transaction in compatibility mode, and a classic ROLLBACK WORK undoes all changes in it. In an object-oriented program, classic update modules still run, as long as none of the called code writes COMMIT WORK.
A check agent is an object that implements IF_OS_CHECK; its method IS_CONSISTENT returns OSCON_FALSE for an inconsistent object. A transaction registers it with REGISTER_CHECK_AGENT, and the transaction service calls it before the update. A result of false raises an exception, so a claim with a negative total can be refused before it is written. The simplest place for the check is the persistent class itself.
The query service
GET_PERSISTENT finds one object by its business key. When the program has only a condition, such as "all submitted claims", it uses a query. A query is an object that implements IF_OS_QUERY and is a container for two strings: a filter condition and a sort condition. It does not run itself: you pass it to IF_OS_CA_PERSISTENCY~GET_PERSISTENT_BY_QUERY of a class agent, and the result is an internal table of object references.
DATA claims TYPE osreftab.
TRY.
DATA(qm) = cl_os_system=>get_query_manager( ).
DATA(query) = qm->create_query( i_filter = `status = par1`
i_ordering = `claim_id ascending` ).
claims = zca_exp_claim_persistent=>agent->if_os_ca_persistency~get_persistent_by_query(
i_query = query
i_par1 = 'S' ).
LOOP AT claims INTO DATA(ref).
DATA(claim) = CAST zcl_exp_claim_persistent( ref ).
" use claim->get_...( )
ENDLOOP.
CATCH cx_os_query_error.
MESSAGE 'Query is invalid' TYPE 'E'.
ENDTRY.
Source: Keyword Documentation for Standard ABAP: OS - Components of the Query Service and Keyword Documentation for Standard ABAP: OS - Query Conditions. Requires an SAP system; not run here.
The filter status = par1 compares the attribute status with a parameter. Without a parameter list the predefined names PAR1, PAR2 and PAR3 can be used, filled from I_PAR1 to I_PAR3 of the agent method (only for elementary values; an internal table I_PARAMETER_TAB carries more). The result table has type OSREFTAB, a table of REF TO object, so each entry is cast to the persistent class.
The syntax of the condition follows the ABAP SQL WHERE condition, with references to objects added:
| Element | Form |
|---|---|
Comparison |
|
|
|
|
|
|
|
Boolean |
|
The sort condition is a list of attributes, each followed by ASCENDING or DESCENDING, such as price ASCENDING date DESCENDING. Only public attributes of elementary type can be used, and the attributes in a query must exist in the persistent class as public persistent attributes, or an exception is raised.
Facts to know before relying on a query:
-
The query runs against the database. Unsaved changes to persistent objects in the current transaction are ignored when the result set is determined.
-
Internally the system builds a
SELECTwith aWHEREcondition, so the query is dynamic. The documentation tells you to put outside values into parameters, not to concatenate them into the condition, to avoid SQL injection; where concatenation cannot be avoided, escape withCL_ABAP_DYN_PRGor the functionescape. -
The query string is parsed on every execution. A query can instead be built from expression objects with
IF_OS_QUERY_EXPR_FACTORY(CREATE_OPERATOR_EXPR,CREATE_LIKE_EXPR,CREATE_AND_EXPRand so on) to be reused without parsing. -
GET_PERSISTENT_BY_QUERYand the table variants of the get methods need a persistent class that was generated for multiple access. -
Parameters
I_SUBCLASSESandI_UPTOdecide whether instances of subclasses are loaded and how many objects at most.
Errors and pitfalls
The Object Services exceptions all begin with CX_OS_. The checked ones (category CX_DYNAMIC_CHECK) descend from CX_OS_ERROR and must be handled by the caller; the unchecked ones (CX_NO_CHECK) report system problems, such as CX_OS_DB_INSERT and its siblings for database errors.
| Exception or symptom | Cause |
|---|---|
|
A load by key found no row. |
|
An object with this key is already managed in the program. |
|
A method was applied to an object in a state that does not allow it, for example |
|
A check agent returned false at the end of the transaction. |
|
Wrong attribute, wrong syntax in the filter, or wrong parameters in a query. |
Runtime error at |
|
Exception from the second |
The method may be called once per program, and an implicit call may already have happened. |
Attributes are initial after saving |
The commit invalidates the objects; read them again through the GET methods or the agent. |
Other traps: a program that bypasses the persistent class and writes the table with ABAP SQL gets objects that still hold old data; two programs with the same claim loaded overwrite each other because the service has no locks; and the objects of a class must be created with the agent, never with NEW.
The same task in current ABAP
For a new application, the claim would not be a persistent class. The simplest form, which works in Standard ABAP, is one ABAP SQL statement over the table:
UPDATE zexp_claim SET status = 'A'
WHERE claim_id = @claim_id AND status = 'N'.
IF sy-subrc = 0.
COMMIT WORK.
ENDIF.
Source: ABAP Keyword Documentation: UPDATE writable_obj. Requires an SAP system; not run here.
The statement says which rows change and under which condition, in one place and without an object in between. In a transactional application, RAP goes a step further: the claim is a business object with an entity, an action to approve, a lock and validations, all declared in the behavior definition, with locking handled by the framework (compare Lock Objects and the SAP Lock Concept) and the commit handled by the framework. Compared with Object Services, you gain draft handling, authorization checks, OData services and Fiori elements apps, and you lose nothing that matters for new development. The RAP pages follow in this section. Use the Object Services page when you read or maintain old code; keep new work out of it.
Summary
-
Object Services let a program handle table rows as objects: a persistent class, mapped to tables in the Mapping Assistant, with a generated class agent that creates, loads and deletes the objects.
-
The persistence service stores objects at
COMMIT WORK; the transaction service bundles changes in object-oriented transactions withSTART,ENDandUNDO, in compatibility mode (you commit) or object-oriented mode (ENDcommits); the query service finds objects with a string condition passed toGET_PERSISTENT_BY_QUERY. -
Objects are identified by a business key, an instance GUID, or both; the service has no locks, so concurrent changes need an SAP lock.
-
All exceptions begin with
CX_OS_;CX_OS_ERRORis the common checked superclass. -
Object Services are legacy: support of the Mapping Assistant has stopped, the main classes are classic APIs and not released for ABAP Cloud, and RAP business objects are the current model.
References
-
Keyword Documentation for Standard ABAP: Object Services (OS)
-
Keyword Documentation for Standard ABAP: OS - Persistence Service
-
Keyword Documentation for Standard ABAP: OS - Persistence Service, Overview
-
Keyword Documentation for Standard ABAP: OS - Components of the Persistence Service
-
Keyword Documentation for Standard ABAP: OS - Management States in the Persistence Service
-
Keyword Documentation for Standard ABAP: OS - Object-Relational Mapping
-
Keyword Documentation for Standard ABAP: OS - Inheritance of Persistent Classes
-
Keyword Documentation for Standard ABAP: OS - Persistent Object References
-
Keyword Documentation for Standard ABAP: OS - Saving Persistent Objects
-
Keyword Documentation for Standard ABAP: OS - Transaction Service
-
Keyword Documentation for Standard ABAP: OS - Components of the Transaction Service
-
Keyword Documentation for Standard ABAP: OS - Transaction Mode
-
Keyword Documentation for Standard ABAP: OS - Nesting and Chaining Transactions
-
Keyword Documentation for Standard ABAP: OS - Transaction Service and Updates
-
Keyword Documentation for Standard ABAP: OS - Query Service, Overview
-
Keyword Documentation for Standard ABAP: OS - Components of the Query Service
-
Keyword Documentation for Standard ABAP: OS - Query Conditions
-
Keyword Documentation for Standard ABAP: OS - Sort Conditions
-
Keyword Documentation for Standard ABAP: OS - Exception Classes
-
SAP/abap-atc-cr-cv-s4hc: Cloudification Repository (objectClassifications_SAP.json)
-
Complete ABAP, Kiran Bandari, SAP PRESS (consulted for concept coverage, section 11.2)