Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Why do we need Persistent Classes ?

SuhaSaha
Product and Topic Expert
Product and Topic Expert
4,831

Dear OO Gurus,

I just finished the chapter on "Persistent Objects" in "ABAP Objects - ABAP Programming in SAP NW by Horst Keller".

I could really appreciate its utility though. Googled a bit, read a few blogs on SCN but still the utility remains blurred.

I would like to know what is the advantage of Persistence Classes over Open SQL.

BR,

Suhas

1 ACCEPTED SOLUTION
Read only

Former Member
2,046

I would like to know what is the advantage of Persistence Classes over Open SQL.

Let's first start with what SAP says about the [persistence service|http://help.sap.com/saphelp_nw70ehp2/helpdata/en/f5/a36828bc6911d4b2e80050dadfb92b/frameset.htm]:

The Persistence Service lets the ABAP programmer work with relational database data in an object-oriented way.

So we're talking here about an [ORM|http://en.wikipedia.org/wiki/Object-relational_mapping] framework. I.e. the persistent storage of data is realized in SAP via a [relational database|http://en.wikipedia.org/wiki/Relational_database], where the data model is not object oriented. Let's say that you have created a class sales order and want to store and retrieve sales order objects (i.e. specific instances of this class). If you have a relational database, you have to model a corresponding schema for representing your sales order class and then "handcode" the translation from database tables into sales order objects and vice versa.

Now imagine a world where you have a nice ORM, that does the work for you. E.g. look at web frameworks like [grails|http://www.grails.org/] making heavy use of [convention over configuration|http://en.wikipedia.org/wiki/Convention_over_configuration], which allow you to easily generate database representations from your objects (without tons of coding/effort!). Essentially you can start off with your class definitions, define some attributes and the framework (ORM) will automatically generate the corresponding database tables for you.

Contrast that with SAP (see online help on [mapping assistant for persistence classes|http://help.sap.com/saphelp_nw70ehp2/helpdata/en/d9/b84508bc9411d4b2e80050dadfb92b/frameset.htm]):

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 in general the ORM is supposed to make the developer's life easier by providing a framework for translating objects into (relational) database tables. However, sometimes the automatic translation/generation along with the default access methods is not optimal and that's where you actually go back to code your own SQL statements for doing the database-to-object conversions and vice versa. So the SAP persistence classes still use OpenSQL under the hood, but in some cases (e.g. mass processing), you might want to tune the used SQL statements yourself...

I apologize for the long excursion, but in the end my view on persistence classes is quite simple: Use them if they make your life more simple and your coding less complex, without causing any bigger performance hit - otherwise stick to handcoded SQL statements... (long live [KISS|http://en.wikipedia.org/wiki/KISS_principle]!)

Cheers, harald

6 REPLIES 6
Read only

Former Member
0 Likes
2,046

Hi,

The main advantage is you need not to fetch the data from the database in case of persistance class Every time..

As the data is already fetched using SELECT.

http://help.sap.com/saphelp_nw2004s/helpdata/en/f5/a3682ebc6911d4b2e80050dadfb92b/content.htm

Thanks..

Read only

Former Member
2,047

I would like to know what is the advantage of Persistence Classes over Open SQL.

Let's first start with what SAP says about the [persistence service|http://help.sap.com/saphelp_nw70ehp2/helpdata/en/f5/a36828bc6911d4b2e80050dadfb92b/frameset.htm]:

The Persistence Service lets the ABAP programmer work with relational database data in an object-oriented way.

So we're talking here about an [ORM|http://en.wikipedia.org/wiki/Object-relational_mapping] framework. I.e. the persistent storage of data is realized in SAP via a [relational database|http://en.wikipedia.org/wiki/Relational_database], where the data model is not object oriented. Let's say that you have created a class sales order and want to store and retrieve sales order objects (i.e. specific instances of this class). If you have a relational database, you have to model a corresponding schema for representing your sales order class and then "handcode" the translation from database tables into sales order objects and vice versa.

Now imagine a world where you have a nice ORM, that does the work for you. E.g. look at web frameworks like [grails|http://www.grails.org/] making heavy use of [convention over configuration|http://en.wikipedia.org/wiki/Convention_over_configuration], which allow you to easily generate database representations from your objects (without tons of coding/effort!). Essentially you can start off with your class definitions, define some attributes and the framework (ORM) will automatically generate the corresponding database tables for you.

Contrast that with SAP (see online help on [mapping assistant for persistence classes|http://help.sap.com/saphelp_nw70ehp2/helpdata/en/d9/b84508bc9411d4b2e80050dadfb92b/frameset.htm]):

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 in general the ORM is supposed to make the developer's life easier by providing a framework for translating objects into (relational) database tables. However, sometimes the automatic translation/generation along with the default access methods is not optimal and that's where you actually go back to code your own SQL statements for doing the database-to-object conversions and vice versa. So the SAP persistence classes still use OpenSQL under the hood, but in some cases (e.g. mass processing), you might want to tune the used SQL statements yourself...

I apologize for the long excursion, but in the end my view on persistence classes is quite simple: Use them if they make your life more simple and your coding less complex, without causing any bigger performance hit - otherwise stick to handcoded SQL statements... (long live [KISS|http://en.wikipedia.org/wiki/KISS_principle]!)

Cheers, harald

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
2,046

Hi Harald,

As usual you simply awe me.

I liked your example of "class sales order and want to store and retrieve sales order objects (i.e. specific instances of this class)", gave a very clear picture about the persistent class instances

I second your views on SQL v/s Persistent Classes ... And who doesn't like the K.I.S.S principle

Wish i could give you 20 p*ints

Cheers,

Suhas

Read only

naimesh_patel
Active Contributor
0 Likes
2,046

One more point I want to iterate is: There are no Persistent objects in ABAP objects. From the SAP help:

The Persistence Service for Persistent Objects

Technically speaking, ABAP Objects are always transient, just like the data objects in ABAP programs. There are no persistent objects in ABAP Objects. However, the Persistence Service within Object Services allows application developers to work with persistent objects. The Persistence Service can be thought of as a software layer between the ABAP program and the data repository (that is, the database), which allows you to save the attributes of objects with a unique identity, and then load them again when you need them.

http://help.sap.com/saphelp_nw04/helpdata/en/f5/a3682ebc6911d4b2e80050dadfb92b/frameset.htm

Regards,

Naimesh Patel

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
2,046

>

> One more point I want to iterate is: There are no Persistent objects in ABAP objects.

Typo ... In my OP it should read "Persistent Classes" instead of "Persistent Objects".

Thanks for pointing it out.

BR,

Suhas

Read only

Astashonok
Participant
0 Likes
2,046

I believe persistence classes do the same function as CDS views/Fiori now, it the predecessor technology in some sense. It allows you to save in database and manipulate custom data models in an object oriented way.