Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
SAP ABAP Advanced Cookbook

You're reading from   SAP ABAP Advanced Cookbook Featuring over 80 sophisticated recipes, this is a superb tutorial for ABAP developers and consultants. It teaches you advanced SAP programming using the high level language through diagrams, step-by-step instructions, and real-time examples.

Arrow left icon
Product type Paperback
Published in Dec 2012
Publisher Packt
ISBN-13 9781849684880
Length 316 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Rehan Zaidi Rehan Zaidi
Author Profile Icon Rehan Zaidi
Rehan Zaidi
Arrow right icon
View More author details
Toc

Table of Contents (22) Chapters Close

SAP ABAP Advanced Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. ABAP Objects 2. Dynamic Programming FREE CHAPTER 3. ALV Tricks 4. Regular Expressions 5. Optimizing Programs 6. Doing More with Selection Screens 7. Smart Forms – Tips and Tricks 8. Working with SQL Trace 9. Code Inspector 10. Simple Transformations 11. Sending E-mail Using BCS Classes 12. Creating and Consuming Web Services 13. SAP Interactive Forms by Adobe 14. Web Dynpro for ABAP 15. Floorplan Manager Index

Creating a persistent object


ABAP objects provide a persistent object service that allows the developer to store objects in the database. The values of the attributes of the object are stored in appropriate fields of the database table specified. This recipe shows how to define persistent classes and then how to call them in your application programs.

Getting ready

Prior to storing objects in the database, a suitable database table with the name ZEMP_TABLE is created to store the values of the objects' attributes. Two fields are defined, NUMBER1 and NAME (the field name NUMBER was not allowed, so NUMBER1 has been used as the field name). Refer to the following screenshot:

How to do it...

Once the database table is defined, a persistence class must be defined. In order to define persistent classes, follow these steps:

  1. Call transaction SE24. Enter a suitable name of the persistent class to be created. We will create a class by entering the name ZCL_MY_PERSIST. Enter the name in the Class field and click on the Create button.

  2. Enter a suitable description in the field provided. Make sure that the Persistent Class indicator is selected, and click on Save.

  3. The programmer may only modify the methods HANDLE_EXCEPTION and INIT.

  4. Click on the Persistence button. Then, enter the name of the table that was created for storage of data( in our case, we will enter the name ZEMP_TABLE). Refer to the following screenshot:

  5. This will take you to the mapping editor. The lower part of the screen will show Table/Fields. Double-click each of the field that is to be included and stored as attributes of the persistent class. The selected field appears in the area earlier (for example, the NUMBER1 field as shown in the following screenshot). Click on the Set attribute values button to include the field.

  6. This will transfer the selected field in the topmost area of the editor.

  7. Similarly, the NAME field must be included.

  8. All the mapped fields will appear at the top area of the mapper. The Number1 field will appear as a business key, as show in the following screenshot:

  9. Upon activation of the persistence class, the system asks for activation of the actor class as well. Click on Yes, as shown in the following screenshot:

  10. The class ZCL_MY_PERSIST is created and necessary methods needed for the persistence service are included. An actor class is also created with the class. The agent class has been generated by the name ZCA_MY_PERSIST. There is one base agent class generated as a result. In total, three classes are generated, the persistent class, the agent class, and the base class of the agent.

  11. The class ZCL_MY_PERSIST contains methods for setting and getting the values of the attributes NAME and NUMBER1. Note that no SET method is generated for the key field, in our case NUMBER1.

  12. The agent class provides number of useful methods related to the persistent property. Important methods, such as create_persistent, delete_persistant, and get_persistent are provided. The methods are implemented in the superclass zcb_my_persist of the agent class zca_my_persist.

How it works...

During the generation of the persistent class zcl_my_persist, two additional classes are generated. These are the actor (agent) and the base agent classes having the names zca_my_persist and zcb_my_persist respectively. The base agent class is generated as abstract (that is, no instance can be constructed from it), and cannot be modified. It is created in a separate pool class from zcl_my_persist. The agent class zca_my_persist may be extended, as well as the loading and saving methods may be modified.

The instantiation mode of the persistence class may be set as abstract or protected. In our recipe, we have chosen the instantiation mode as protected (which means that only instances may be created from within the class or its subclasses). However, making the instantiation mode of a persistent class as protected makes the generated base agent class a friend of the persistent class (in the world of ABAP objects, a friend or its subclasses may create instances of the class in question).

The coding for this recipe declares two references, emp and agent, to the persistent class zcl_my_persist and the agent class zca_my_persist, respectively. Next, the static factory method agent is called for the class zca_my_persist (agent class). The reference returned is stored in the variable agent.

The agent class contains the method create_persistent required for storing the data into the database (this is analogous to the concept of insertion in database table).

The most important part is the calling of the create_persistent method that is passed the number and name that is to be stored. The employee with the number 00000017 and name John Reed is created and reference is returned in emp. Finally, the COMMIT WORK method stores the data of the emp object into the table created earlier in this recipe. One row with the number and a name is added to the table ZEMP_TABLE.

For reading the stored value related to the employee number 00000017, a number variable is declared and assigned the value 00000017. The static method agent of the zca_my_persist class is called in order to get a reference to the agent. The get_persistent method is then called and the number (in our case, 00000017) is passed. This method returns the entire object emp pertaining to the employee number. You may then call the get_name method of the zcl_my_persist class for the emp object in order to retrieve the employee name.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image