Monday, February 7, 2011

Persistent Objects: Using Business Key Identity

Objective

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

Step: 1 ->Create a Persistent Class

Go to Class Builder (TCode SE24)

Give persistent class name for e.g. ZCL_PERSIST_01 and hit the create button

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

Step: 2 -> Persistent Mapping or Mapping

Utilities->Persistence Representation

Give the table name. For e.g. ZSTUDENT01 and hit the enter button

Table fields appear below the mapping screen.

Double Click the table field and then press the upward arrow button

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

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

Step: 3 -> 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_GET_PERSISTENT
*& Published @ SAPTechnical.com
*&---------------------------------------------------------------------*
*&Author : Abdul Hakim
*&Development Language: ABAP
*&System Release: SAP Netweaver 2004
*&Title: Persistent Object using Business Key Object Identity!!
*&---------------------------------------------------------------------*
REPORT  Z_GET_PERSISTENT.
selection-screen begin of block blk1 with frame title tit1.
parameters: sno like zstudent01-sno obligatory,
sname like zstudent01-sname obligatory,
mark1 like zstudent01-mark1 obligatory,
mark2 like zstudent01-mark2 obligatory.
selection-screen end of block blk1.
selection-screen begin of block blk2 with frame title tit2.
parameters: r1 type c radiobutton group rad1,
r2 type c radiobutton group rad1,
r3 type c radiobutton group rad1.
selection-screen end of block blk2.
*---------------------------------------------------------------------*
* CLASS lcl_class1 DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
class lcl_class1 definition.
public section.
data: agent type ref to zca_persist_01,
students type ref to zcl_persist_01.
    data result1 type ref to zcl_persist_01.
methods: fetch_persistent importing im_sno like sno
im_sname like sname,
create_persistent importing im_sno like sno
im_sname like sname
im_mark1 like mark1
im_mark2 like mark2,
delete_persistent importing im_sno like sno
im_sname like sname,
output.
private section.
data: sno type zstudent01-sno,
sname type zstudent01-sname,
mark1 type zstudent01-mark1,
mark2 type zstudent01-mark2.
endclass.                    "lcl_class1 DEFINITION
*---------------------------------------------------------------------*
* CLASS lcl_class1 IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
class lcl_class1 implementation.
method fetch_persistent.
    agent = zca_persist_01=>agent.
    try.
agent->get_persistent( exporting i_sno = im_sno
i_sname = im_sname
receiving result = students ).
.
sname = students->get_sname( ).
sno = students->get_sno( ).
mark1 = students->get_mark1( ).
mark2 = students->get_mark2( ).
if r1 eq 'X'.
output( ).
endif.
CATCH CX_OS_OBJECT_NOT_FOUND .
MESSAGE 'Object doesn''t exists' TYPE 'I' DISPLAY LIKE 'E'.
    endtry.
endmethod. "fetch_persistent
  method output.
write:/ sno,
sname,
mark1,
mark2.
endmethod. "output
  method create_persistent.
fetch_persistent( exporting im_sname = im_sname
im_sno = im_sno ).
try.
agent->create_persistent( exporting i_mark1 = im_mark1
i_mark2 = im_mark2
i_sname = im_sname
i_sno = im_sno
receiving result = students ).
commit work.
write 'Object Created'.
CATCH CX_OS_OBJECT_EXISTING .
MESSAGE 'Object already exists' TYPE 'I' DISPLAY LIKE 'E'.
    endtry.
  endmethod.                    "create_persistent
  method delete_persistent.
fetch_persistent( exporting im_sname = im_sname
im_sno = im_sno ).
try.
agent->delete_persistent( exporting i_sname = im_sname
i_sno = im_sno ).
commit work.
write 'Object Deleted'.
CATCH CX_OS_OBJECT_NOT_EXISTING .
MESSAGE 'Object doesn''t exists' TYPE 'I' DISPLAY LIKE 'E'.
    endtry.
endmethod. "delete_persistent
endclass.                    "lcl_class1 IMPLEMENTATION
data ref_class1 type ref to lcl_class1.
*---------------------------------------------------------------------*
* Load-of-Program
*---------------------------------------------------------------------*
load-of-program.
tit1 = text-001.
tit2 = text-001.
*---------------------------------------------------------------------*
* Start-of-Selection
*---------------------------------------------------------------------*
start-of-selection.
create object ref_class1.
  if r1 eq 'X'.
ref_class1->fetch_persistent( exporting im_sno = sno
im_sname = sname ).
elseif r2 eq 'X'.
ref_class1->create_persistent( exporting im_sno = sno
im_sname = sname
im_mark1 = mark1
im_mark2 = mark2 ).
else.
ref_class1->delete_persistent( exporting im_sno = sno
im_sname = sname ).
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