Monday, February 7, 2011

Persistent Objects: Using GUID Object Identity

Objective

To Store the attributes of the Objects persistently in the database.

Persistent Object’s Identity

Every Persistent Object has a unique identity with which it can be accessed. There are 2 types of Object identity

  1. Business Key
  2. GUID( Global Unique Identifier )

For Persistent Objects using Business key Identity please check my previous article, “Persistent Objects: Using Business Key identity”

This article will focus only on Persistent Object using GUID.

Step: 1 -> Create a database table

This table should contain a key field of type OS_GUID.

Step: 2 ->Create a Persistent Class

Go to Class Builder (tcode SE24)

Give persistent class name for eg ZCL_PERSIST_02 and hit the create button

In the next screen select the class type as Persistent Class and then hit Save Button.

Step: 3 -> Persistent Mapping or Mapping

Goto->Persistence Representation

Give the table name. For eg ZSTUDENT02 and hit the enter button

Table fields appear in the lower half of the tool screen.

Double Click the table field

Press the upward arrow button





Add the remaining fields as well. Screen looks like this now. Press Save Button

Activate the Class. Press the Yes Button to activate the class actor as well.

Unlike Business Key, GUID is not an attribute of the Persistent Class.

Step: 4 -> Write a Program to create / fetch / delete the Persistent Object

Our Program Selection-Screen looks like below

Here I am creating a new student. Specify the value and hit the execute button.

Output:

Go to SE16 and check the entries

Source Code
*&-------------------------------------------------------------------*
*& Report Z_PERSISTENT_GUID
*&
*&-------------------------------------------------------------------*
*&Author : Abdul Hakim
*&Development Language: ABAP
*&System Release: SAP Netweaver 2004
*&Title: Persistent Object using GUID Object Identity!!
*&-------------------------------------------------------------------*
REPORT  Z_PERSISTENT_GUID.
selection-screen begin of block b1 with frame title tit1.
parameters: sno like zstudent02-sno,
sname like zstudent02-sname,
mark1 like zstudent02-mark1,
mark2 like zstudent02-mark2,
guid like zstudent02-guid.
selection-screen end of block b1.
selection-screen begin of block b2 with frame title tit2.
parameters: r1 radiobutton group rad1,
r2 radiobutton group rad1,
r3 radiobutton group rad1.
selection-screen end of block b2.
data: agent type ref to zca_persist_02,
students type ref to zcl_persist_02.
data: result1 type ref to object,
result2 type ref to zcl_persist_02.
*-------------------------------------------------------------------*
* Load-of-Program
*-------------------------------------------------------------------*
load-of-program.
tit1 = text-001.
tit2 = tit1.
*-------------------------------------------------------------------*
* At Selection Screen
*-------------------------------------------------------------------*
at selection-screen.
if ( r2 eq 'X' ).
if sno is initial or sname is initial.
MESSAGE 'Enter the values in Sno/Sname fields'
TYPE 'E' DISPLAY LIKE 'E'.
endif.
endif.
*-------------------------------------------------------------------*
* Start-of-Selection
*-------------------------------------------------------------------*
start-of-selection.
agent = zca_persist_02=>agent.
  if r1 eq 'X'.
    TRY.
CALL METHOD AGENT->IF_OS_CA_PERSISTENCY~GET_PERSISTENT_BY_OID
EXPORTING
I_OID = guid
RECEIVING
RESULT = result1.
        result2 ?= result1.
sno = result2->get_sno( ).
sname = result2->get_sname( ).
mark1 = result2->get_mark1( ).
mark2 = result2->get_mark2( ).
        
write:/ sno,
sname,
mark1,
mark2.
      CATCH CX_OS_OBJECT_NOT_FOUND .
* CATCH CX_OS_CLASS_NOT_FOUND .
MESSAGE 'Object doesn''t exists' TYPE 'I' DISPLAY LIKE 'E'.
    ENDTRY.
  elseif r2 eq 'X'.
TRY.
CALL METHOD AGENT->CREATE_PERSISTENT
EXPORTING
I_MARK1 = mark1
I_MARK2 = mark2
I_SNAME = sname
I_SNO = sno
RECEIVING
RESULT = students.
commit work.
write 'Object Created'.
      CATCH CX_OS_OBJECT_EXISTING .
MESSAGE 'Object already exists' TYPE 'I' DISPLAY LIKE 'E'.
    ENDTRY.
  else.
    TRY.
CALL METHOD AGENT->IF_OS_CA_PERSISTENCY~GET_PERSISTENT_BY_OID
EXPORTING
I_OID = guid
RECEIVING
RESULT = result1.
CATCH CX_OS_OBJECT_NOT_FOUND .
* CATCH CX_OS_CLASS_NOT_FOUND .
MESSAGE 'Object doesn''t exists' TYPE 'I' DISPLAY LIKE 'E'.
    ENDTRY.
result2 ?= result1.
    TRY.
CALL METHOD AGENT->IF_OS_FACTORY~DELETE_PERSISTENT
EXPORTING
I_OBJECT = result2.
commit work.
write 'Object Deleted'.
      CATCH CX_OS_OBJECT_NOT_EXISTING .
MESSAGE 'Object doesn''t exists' TYPE 'I' DISPLAY LIKE 'E'.
    ENDTRY.
endif.

No comments:

Tutorials on SAP-ABAP

Adobe Interactive Forms Tutorials

Business Server Pages (BSP)

Userexits/BADIs

Web Dynpro for ABAP (Step by step procedure for web dynpro,Tutorials on Web Dynpro,)

ALV Tutorials

Blog Archive

goodsites