Using Persistent Class For CRUD (Create, Read, Update and Delete) Operations
Using Persistent Class For CRUD (Create, Read, Update and Delete) Operations
Community
Former Member
November 6, 2012 | 6 minute read
Like
Delete) Operations
Introduction
In contrast to the traditional SELECT and UPDATE query, SAP also provides the
facility to use PERSISTENT Classes in order to perform CRUD(Create, Read,
Update and Delete) operations.
The following blog will take you through the steps on how to perform CRUD
operations on a TABLE using persistent objects.
Step by Step
https://blogs.sap.com/2012/11/06/using-persistent-class-for-crudcreate-read-update-and-delete-operations/ 1/10
1/8/23, 6:21 PM Using Persistent Class for CRUD(Create, Read, Update and Delete) Operations | SAP Blogs
I have selected an already created table in my system, you can create your own
and use and existing one.
Figure 1
Persistent classes are like normal classes and are created using the class builder
(se24). The only difference while creating a persistent class is the tick mark
Persistent Class, which identifies the class as a persistent class.
Goto SE24 write a name and then press the create button, select the Class type
as Persistent Class and press SAVE as shown in Figure 2.
On Save system will generate two more classes along with the Persistent Class
Agent class ZCA_ASSET_VERF_DOC_HEADER and the
Base class ZCB_ASSET_VERF_DOC_HEADER
https://blogs.sap.com/2012/11/06/using-persistent-class-for-crudcreate-read-update-and-delete-operations/ 2/10
1/8/23, 6:21 PM Using Persistent Class for CRUD(Create, Read, Update and Delete) Operations | SAP Blogs
Figure 2
This is done since the persistent class is kept by the system as a managed object
and its class cannot be instantiated directly using the CREATE OBJECT
command. So when ever CREAT_PERSISTENT, GET_PERSISTENT etc methods of
the agent class are called, a new instance of the persistent class
will be created and returned.
https://blogs.sap.com/2012/11/06/using-persistent-class-for-crudcreate-read-update-and-delete-operations/ 3/10
1/8/23, 6:21 PM Using Persistent Class for CRUD(Create, Read, Update and Delete) Operations | SAP Blogs
Figure 3
Add the fields you want to make persistent by double clicking the fields in the
bottom section and then pressing the arrow button as shown in Figure 4.
Figure 4
Tips: Click the Generator Settings button in the toolbar and select the
Generate Methods for Query checkbox as shown in Figure 5, this will generate
the GET_PERSISTENT_BY_QUERY method as well.
https://blogs.sap.com/2012/11/06/using-persistent-class-for-crudcreate-read-update-and-delete-operations/ 4/10
1/8/23, 6:21 PM Using Persistent Class for CRUD(Create, Read, Update and Delete) Operations | SAP Blogs
Figure 5
On saving you will see that the system will generate Setter and Getter methods
for all selected fields as shown in Figure 6.
Figure 6
Activate the class and you can now use it to Create, Read, Update and Delete data
from ZASTVERFH table.
Test program to Create, Read, Update and Delete from table using
Persistent Class
Create:
TRY .
lo_astverf_obj = zca_asset_verf_doc_header=>agent->create_persistent(
i_physinv =
lv_physinv
i_gjahr = lv_gjahr ).
https://blogs.sap.com/2012/11/06/using-persistent-class-for-crudcreate-read-update-and-delete-operations/ 5/10
1/8/23, 6:21 PM Using Persistent Class for CRUD(Create, Read, Update and Delete) Operations | SAP Blogs
COMMIT WORK.
CATCH cx_os_object_existing.
ENDTRY.
Update:
For updation everything remains the same only you need to replace the
CREATE_PERSISTENT method with the GET_PERSISTENT method and the
exception will change from cx_os_object_existing to cx_os_object_not_found.
TRY.
lo_astverf_obj = zca_asset_verf_doc_header=>agent->get_persistent(
COMMIT WORK.
CATCH cx_os_object_not_found.
ENDTRY.
Read:
Code for Read is same as update, the only change is we will use GETTER methods
instead of SETTER methods.
TRY .
lo_astverf_obj = zca_asset_verf_doc_header=>agent->get_persistent(
zastverfh–crtdate = lo_astverf_obj->get_crtdate( ).
zastverfh–crttime = lo_astverf_obj->get_crttime( ).
zastverfh–crtuname = lo_astverf_obj->get_crtuname( ).
zastverfh–hstatus = lo_astverf_obj->get_hstatus( ).
CATCH cx_os_object_not_found.
ENDTRY.
https://blogs.sap.com/2012/11/06/using-persistent-class-for-crudcreate-read-update-and-delete-operations/ 6/10
1/8/23, 6:21 PM Using Persistent Class for CRUD(Create, Read, Update and Delete) Operations | SAP Blogs
Delete:
TRY .
zca_asset_verf_doc_header=>agent->delete_persistent(
COMMIT WORK.
CATCH cx_os_object_not_existing.
ENDTRY.
TRY .
lt_obj = zca_asset_verf_doc_header=>agent-
>if_os_ca_persistency~get_persistent_by_query(
i_par1 = ‘5%’
i_query = cl_os_system=>get_query_manager( )->create_query(
i_filter = ‘PHYSINV LIKE PAR1’ ) ).
https://blogs.sap.com/2012/11/06/using-persistent-class-for-crudcreate-read-update-and-delete-operations/ 7/10
1/8/23, 6:21 PM Using Persistent Class for CRUD(Create, Read, Update and Delete) Operations | SAP Blogs
ENDLOOP.
ENDIF.
CATCH cx_os_object_not_found.
ENDTRY.
Alert Moderator
Assigned Tags
Retagging required
abap persistence
crud operations
khusro habib
persistence
View more...
https://blogs.sap.com/2012/11/06/using-persistent-class-for-crudcreate-read-update-and-delete-operations/ 8/10
1/8/23, 6:21 PM Using Persistent Class for CRUD(Create, Read, Update and Delete) Operations | SAP Blogs
Related Questions
OO Design question's
By Former Member Jun 23, 2009
2 Comments
Joseph BERTHE
November 24, 2013 at 2:29 pm
Hello,
However, in the Read Multiple Records section, I tried to use the parameter PAR1 but it seams it doesn't
work, in DEBUG, the query is something like this :
Regards,
Like 0 | Share
https://blogs.sap.com/2012/11/06/using-persistent-class-for-crudcreate-read-update-and-delete-operations/ 9/10
1/8/23, 6:21 PM Using Persistent Class for CRUD(Create, Read, Update and Delete) Operations | SAP Blogs
Former Member
October 24, 2017 at 2:35 am
In 'Linking the table with the persistent class' step to get to the screen in the Figure 3, Menu: Goto -->
Persistence Representant.
Like 0 | Share
Find us on
Newsletter Support
https://blogs.sap.com/2012/11/06/using-persistent-class-for-crudcreate-read-update-and-delete-operations/ 10/10