Understanding Persistence

40
Blackvard Management Consulting Understanding Persistence Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

Transcript of Understanding Persistence

Blackvard Management ConsultingUnderstanding Persistence

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

Blackvard Management Consultants

www.blackvard.comCopyright © Blackvard Management Consulting – All rights reserved

Erin Lett is the Operations Manager for Blackvard Management Consulting. She holds a Bachelor’s Degree from Stetson University in Communications and has been working in the SAP, eLearning, and Software Development industries for the past 6 years.

For further information please visit:

www.blackvard.com

[email protected]

Copyright © Blackvard Management Consulting- All rights reserved www.blackvard.com

Your Host

Erin Lett

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

Agenda

What Will Be Covered:

1. SAP Persistence Overview

1. Concepts Used In Persistence

2. Transient vs. Persistent Data

3. Data In Object-oriented Programming

4. Persistence Service/Persistent Classes/Managed Objects

2. Components of Persistence

1. Get A Flight Object

2. Create A Flight Object

3. About Us

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

What is SAP Persistence Service?

SAP Persistence Service forms a bridge between relational databases & ABAP Objects.

SAP Persistence Service allows ABAP Programmers to work w/ relational database data in an object-oriented way.

Databases

Persistent Object ServicesCreate & Initialize Objects Save & Delete Data In The Object

ABAP Programs

Persistence Service as a Logical Layer

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

SAP Persistence Service isn’t exactly like ORM. New database tables, database

views or object model structures are not automatically created by the system.

What is SAP Persistence Service?

SAP Persistence Service is more an application architecture layer.

Enables users to work w/ relational database as ABAP Objects.

Object-Relational Mapping (ORM) tools allow for classes w/ attributes. Used in other programming languages such as Java or C#. Framework automatically generates corresponding database tables in databases &

represents the data like objects.

JAVA code to deliver a complex query, written in SQL

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

The nature & longevity of data found in ABAP programs gives that data a unique name.

Transient Data

Transient Data – The data lives only as long as its context. That is, as long as its associated procedure (for local procedure data). Or as long as its object (for attributes of classes). Or its program (for global program data).

Once the data is out of the context, the value no longer exists. Example:

If you click SAVE -> the document will be persisted to a folder on your hard drive. Click Don’t Save or Cancel -> the transient data (e.g. in memory) is gone, but currently you can edit the file.

ABAP programs work w/ local program data in the program’s internal session.

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

Persistent Data

Persistent Data - If the data can be preserved beyond the context or runtime of the program.

Persistent data generally occurs as the content of database tables. Can also occur as content of files on applications & presentation servers. Hard drive or flash memory (c.p. the example from previous slide – if you store the

file on a hard drive or in a database, the transient data will be persisted. Else the transient data is gone).

“Save”

“Don’t Save” or

“Cancel”

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

Transient & Persistent Data

The system loads persistent data into transient data objects belonging to the ABAP program, while the program is being executed.

Once processed, the system stores the data in persistent form again. Once in the ABAP program (transiently)/Once in the appropriate storage medium (persistently)

A typical example would be obtaining data from a database table using SELECT statement in a transient work area, modifying the work area & updating the database table (UPDATE).

The contents of transient & persistent data are different in the interim of this process.

JAVA code to deliver a complex query, written in SQL

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

In Object-oriented Applications, data occurs only as object attributes.

Data In Object-oriented Programming

Objects are a collection of functions (in methods) & data (in attributes). Object descriptions occur persistently as a piece of source code. Attributes exist only as long as the object.

ABAP Objects are transient by nature & alive from the time of creation, via “CREATE OBJECT”, until deleted by the “Garbage Collector”. To work w/ persistent data in objects, access must be programmed to where those objects are stored w/in the class methods.

?

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

In Object-oriented Business Application programming, it is pointless to transfer the classical separation of data & functions to the methods.

Data In Object-oriented Programming

Instead, save the data & functions persistently w/in the object. A program can then leave an object in a specific state. Second program could continue working w/ the object in that state.

Classes of objects are persistent already, but saving the attributes of an object persistently & then referencing them to the appropriate class is possible.

Example:

Saved original file on the hard drive Open & unsaved document with changes

Ref

eren

ce

Orig

inal

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

Persistence Service For Persistent Objects

Persistent Object Services (POS) are considered to be logical software layers between the database & the ABAP program.

Save object attributes w/ a unique identity & then reload them when needed.

Like data objects in ABAP programs, ABAP Objects are always transient. No persistent objects exist in ABAP objects.

The Object Services Persistence Service allows application developers to work w/ persistent objects.

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

Persistent Service ensures an object is initialized in a specific state & saves that state when needed.

Similar to that of transient & persistent data.

Concepts Used In Persistence

Object state, when instantiated, reflects the state of the current database data. Object state changes in the ABAP Program are not immediately written to the database. This is done after the appropriate request has been made (COMMIT WORK).

Persistent object exists as original & copy in 1+ ABAP programs. If several programs use the Persistence Service to instantiate objects of the same class before

one of the programs has changed the state, all objects will have the same initial state.

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

Object Classes must be created as Persistent Classes in the Class Builder.

Persistent Classes

The term Persistent Class means that the objects of that class & their state are managed by the Persistence Service.

The objects of these classes are instantiated in the ABAP program w/ a method of the service, ensuring the initialization is correct.

When a Persistent Class is created, it generates a Class Actor or Class Agent. Manages the objects of Persistent Classes. Persistent Classes can contain key attributes & identity. Persistence Service ensures that persistent objects contain unique content

(e.g. Primary Keys of database tables or unique object IDs like GUID).

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

Persistent Class Objects are managed by the Persistence Service. These objects are instantiated w/ a method of class actor. Managed objects are either persistent or transient.

Managed Objects

Persistent Objects must be managed by the Persistence Service. Connects the object w/ the database.

Transient Objects of Persistent Classes are also managed by the service. Example: Persistence Service ensures an object is unique w/in a program.

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

SAP Persistence Service is made up of global classes & interfaces.

Components Of Persistence

The upper four interfaces consist of Persistent Classes.

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

The lower three classes are specific & generated automatically for each Persistent Class created.

Components Of Persistence

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

ZCL_MY_FLIGHT Persistent Class must implement IF_OS_STATE interface.

Ensures access to all persistent objects. This class has numerous GET/SET methods & attributes, as many fields exist in its connected persistent structure.

Generated by the Class Builder.

.

Components Of Persistence

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

Two Agent Classes are generated by Class Builder for each Persistent Class. Those two agent classes are Abstract & Final.

ZCA_MY_FLIGHT is one of the agent classes & it’s responsible for managing the ZCL_MY_FLIGHT persistent object and serves as a data repository.

Components Of Persistence

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

ZCB_MY_FLIGHT, the Super Class of ZCA_MY_FLIGHT, is also generated by the Class Builder & implements the following interfaces:

IF_OS_FACTORY/ IF_OS_CA_PERSISTENCY/ IF_OS_CA_INSTANCE.

Components Of Persistence

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

Begin by using the SE24 transaction, in Class Builder, to create a new Class with the name ZCL_MY_FLIGHT.

Since it will be in the Persistent Class of the database table called SFLIGHT.

Fill in all required fields and then select the Class Type Persistent Class & click Save.

Select where you wish to store the new class & click Save again.

Create Persistent Class

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

After saving the new class, verify your Persistent Class, by clicking the Interfaces tab.

It implements the interface IF_OS_STATE.

Interfaces

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

Under the Friends tab, it also displays as a friend of the abstract agent class, ZCB_MY_FLIGHT.

Friends

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

Now connect the Persistent Class to an ABAP Dictionary object.

Set the Persistence Mapping by clicking the Persistence button.

Set Persistence Representation

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

Now assign a Table/View/Structure to the Class. Select the database table that consists of flights, called SFLIGHT. Click the Continue icon to confirm.

Assign Table

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

After assigning the table, Class Builder loads metadata into the lower Table/Fields section.

Double-click on any one of the fields to load it into the Maintain Area.

Maintain Properties of Attributes

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

Using the Maintain Area, you can now modify the ID, Description, Field Visibility, etc.

After completing the maintenance, simply press Enter to assign it to the Persistent Class as an attribute.

Add Attribute To Persistent Class

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

After pressing Enter, the field appears under the Class as an Attribute & loads the next field into the Maintain Area accordingly.

Select each column/field you want to add to your class. Double-click on the name, then click the black arrow icon to add it to the class.

Add Attribute To Persistent Class

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

If all fields are not required, the unused columns can remain in the lower area and be added later as needed.

After applying the workflow to each field, the following appears:

Add Attribute To Persistent Class

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

After Saving the Persistence Mapping, return to the Class Definition using SE80 or SE24.

Verify the Attributes & Methods.

New Attributes

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

Along with the new attributes, GET & SET methods are also generated. Class Builder will activate the Actor Classes, along with the Persistent

Class. Save your work! The Persistent Class is complete & can now be used.

New GET/SET Methods

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

In the core of the TRY-CATCH block, reference the Agent Class & Save it in the reference variable lo_flight_agent by querying the class attribute of the zca_my_flight called agent.

Get A Flight Object

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

Ask the agent to retrieve a persistent from the database using the “BA” business key.

Save the receiving reference of the flight instance in the reference variable, in the local object called “lo_flight”.

You now have an initialized instance of the zcl_my_flight & can call its GET- and SET methods, e.g. such as GET_CARRNAME().

Get A Flight Object

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

This process is very effective & reusable between various clients such as: ABAP classes, ABAP programs, Function Modules, etc.

Now verify the results:

Get A Flight Object

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

Create a flight object using the Persistent Class by referencing the Agent Class & Saving it in the variable lo_flight_agent.

Ask the agent to create a persistent in the database with the key parameters of:

i_CARRID, i_CONNID and i_FLDATE

(the key values of the

database table SFLIGHT)

Set a new price (set_price) Call the COMMIT statement. Save your work & Activate. Run the report.

Create A Flight Object

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

Here you can see the results of the coding in the first line: Our newly created entry appears in database table SFLIGHT (check by using transaction SE11 or SE16n).

Create A Flight Object

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

Using Persistent Classes, you can take steps towards more transparent & well separated classes based on responsibilities such as:

Data Layers/Data Access Layers/Business Layers/UI Layers & Application Layers

Summary

Classes are reusable & you only need to implement them once in order to reuse them as required.

Although there are missing features, this initiative proves that the ABAP language is evolving & moving forward.

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

Have Additional Questions?

Want To get trained?

Please Set Up A Consultation.

Email: [email protected]

Require A Consultation?

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

Technical project lead and ABAP architect responsible for quality in technical scope and budget in a global roll-out of SAP Logistics applications (SAP LE / LO)

Conducting multiple SAP ABAP and SAP HANA® trainings for various US companies

Implementation of a standard SAP software solution for Spend Management within SAP AG & ARIBA (annual spend volume 3 Bill. EUR) which can be used in all SAP systems

Improved claims management using SAP FS-CM which is generating annual savings of 15 Mio € for a huge German public healthcare organization

Implemented a global solution for procurement processes at BMW AG using SAP SRM / B2B

Blueprinting and implementation of SAP software for banking credit cancelations for VOLKSWAGEN

Key Achievements of Blackvard Management Consulting in Previous Projects

What We’ve Accomplished

Blackvard Management Consultants

www.blackvard.comCopyright © Blackvard Management Consulting – All rights reserved

Short Bio:

Lukas M. Dietzsch is managing director at Blackvard Management Consulting, LLC. He is holding a Master’s degree in Information Technology and is an experienced IT solution architect and project lead.

His strong background in adapting to requirements and standards in different industries and on various platforms are valuable assets for Blackvard customers.

He is repeatedly commended by customers for driving efficient solutions for complex problems in globally distributed team environments and meeting tough deadlines.

For further information please visit:

www.blackvard.com

Lukas M. Dietzsch

[email protected]

Copyright © Blackvard Management Consulting- All rights reserved www.blackvard.com

Managing Director

Copyright © Blackvard Management Consulting – All rights reserved www.blackvard.com

An overview of current and previous customers:

Customers That Recommend Blackvard