OOPS ABAP: Persistence class
Persistence class:

- The purpose of persistence class is to perform data base operations like insert, update,delete.
- The persistence class name must start with CL.
- While creating the persistence class create, 2 helper class automatically.Those helper classes are CA and CB.
- CA is called as actor class or agent class and CB is called base class.
Steps:
- Go to 'SE24' and enter the persistence class name starts with CL ( Eg: ZCL_PER_STU) .
- Click on create and enter short description and select radio button 'Persistence class'.
- Click on enter and some methods are automatically created.
- Click on 'GOTO' menu button and select 'Persistence representant'.
- Enter table name and press enter.
- Double click on field names and click on 'Set Attribute values' button.
- Click on save and back.
- Activate.(You will get the popup saying Actor class.press enter)(Actor calss ZCA_PER_STU and ZCB_PER_STU automatically created)
Agent attribute of the Agent class:
- The static attribute AGENT is a reference variable to agent class (CA).
- First time this attribute is accessed in an ABAP program, the static constructor of the agent class creates exactly one instance of this class, to which the attribute AGENT then points.
- There is only one object of the agent class through out a particular session.
Program: To perform database operations on Custom table.
*&---------------------------------------------------------------------*
*& Report ZVB_DBOP_STU
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zvb_dbop_stu.
TABLES:zvb_stu.
DATA:lo_obj1 TYPE REF TO zcl_per_stu,
lo_agent TYPE REF TO zca_per_stu.
DATA:lv_id TYPE zvb_stu-zstu_id,
lv_name TYPE zvb_stu-zstu_name,
lv_dep TYPE zvb_stu-zstu_dep.
PARAMETERS:p_insert TYPE c RADIOBUTTON GROUP g1 DEFAULT 'X',
p_update TYPE c RADIOBUTTON GROUP g1,
p_delete TYPE c RADIOBUTTON GROUP g1.
PARAMETERS:p_id TYPE zvb_stu-zstu_id OBLIGATORY,
p_name TYPE zvb_stu-zstu_name,
p_dep TYPE zvb_stu-zstu_dep.
AT SELECTION-SCREEN.
IF p_insert = 'X'.
* SELECT SINGLE zstu_id
* FROM zvb_stu
* INTO lv_id
* WHERE zstu_id = p_id.
** ENDSELECT.
lo_agent = zca_per_stu=>agent.
TRY.
CALL METHOD lo_agent->get_persistent
EXPORTING
i_zstu_id = p_id
RECEIVING
result = lo_obj1.
CATCH cx_os_object_not_found.
ENDTRY.
IF lo_obj1 IS NOT INITIAL.
MESSAGE 'Record already exited' TYPE 'E'.
ENDIF.
ELSEIF p_delete = 'X'.
lo_agent = zca_per_stu=>agent.
TRY.
CALL METHOD lo_agent->get_persistent
EXPORTING
i_zstu_id = p_id
RECEIVING
result = lo_obj1.
CATCH cx_os_object_not_found.
ENDTRY.
IF lo_obj1 IS INITIAL.
MESSAGE 'Record not exited' TYPE 'E'.
ENDIF.
ELSEIF p_update = 'X'.
lo_agent = zca_per_stu=>agent.
TRY.
CALL METHOD lo_agent->get_persistent
EXPORTING
i_zstu_id = p_id
RECEIVING
result = lo_obj1.
CATCH cx_os_object_not_found.
ENDTRY.
IF lo_obj1 IS INITIAL.
MESSAGE 'Record not exited' TYPE 'E'.
ENDIF.
IF lo_obj1 IS NOT INITIAL.
TRY.
CALL METHOD lo_obj1->get_zstu_name
RECEIVING
result = lv_name.
CATCH cx_os_object_not_found.
ENDTRY.
TRY.
CALL METHOD lo_obj1->get_zstu_dep
RECEIVING
result = lv_dep.
CATCH cx_os_object_not_found.
ENDTRY.
ENDIF.
ENDIF.
AT SELECTION-SCREEN OUTPUT.
IF p_update = 'X'.
p_name = lv_name.
p_dep = lv_dep.
ENDIF.
START-OF-SELECTION.
IF p_insert = 'X'.
CLEAR:lo_agent,lo_obj1.
lo_agent = zca_per_stu=>agent.
TRY.
CALL METHOD lo_agent->create_persistent
EXPORTING
i_zstu_id = p_id
RECEIVING
result = lo_obj1.
CATCH cx_os_object_existing.
ENDTRY.
TRY.
CALL METHOD lo_obj1->set_zstu_name
EXPORTING
i_zstu_name = p_name.
CATCH cx_os_object_not_found.
ENDTRY.
TRY.
CALL METHOD lo_obj1->set_zstu_dep
EXPORTING
i_zstu_dep = p_dep.
CATCH cx_os_object_not_found.
ENDTRY.
COMMIT WORK.
MESSAGE 'Record inserted' TYPE 'S'.
ELSEIF p_update = 'X'.
lo_agent = zca_per_stu=>agent.
TRY.
CALL METHOD lo_agent->get_persistent
EXPORTING
i_zstu_id = p_id
RECEIVING
result = lo_obj1.
CATCH cx_os_object_not_found.
ENDTRY.
TRY.
CALL METHOD lo_obj1->set_zstu_name
EXPORTING
i_zstu_name = p_name.
CATCH cx_os_object_not_found.
ENDTRY.
TRY.
CALL METHOD lo_obj1->set_zstu_dep
EXPORTING
i_zstu_dep = p_dep.
CATCH cx_os_object_not_found.
ENDTRY.
COMMIT WORK.
MESSAGE 'Record updated' TYPE 'S'.
ELSEIF p_delete = 'X'.
lo_agent = zca_per_stu=>agent.
TRY.
CALL METHOD lo_agent->delete_persistent
EXPORTING
i_zstu_id = p_id.
CATCH cx_os_object_existing.
ENDTRY.
COMMIT WORK.
MESSAGE 'Record deleted' TYPE 'S'.
ENDIF.
*& Report ZVB_DBOP_STU
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zvb_dbop_stu.
TABLES:zvb_stu.
DATA:lo_obj1 TYPE REF TO zcl_per_stu,
lo_agent TYPE REF TO zca_per_stu.
DATA:lv_id TYPE zvb_stu-zstu_id,
lv_name TYPE zvb_stu-zstu_name,
lv_dep TYPE zvb_stu-zstu_dep.
PARAMETERS:p_insert TYPE c RADIOBUTTON GROUP g1 DEFAULT 'X',
p_update TYPE c RADIOBUTTON GROUP g1,
p_delete TYPE c RADIOBUTTON GROUP g1.
PARAMETERS:p_id TYPE zvb_stu-zstu_id OBLIGATORY,
p_name TYPE zvb_stu-zstu_name,
p_dep TYPE zvb_stu-zstu_dep.
AT SELECTION-SCREEN.
IF p_insert = 'X'.
* SELECT SINGLE zstu_id
* FROM zvb_stu
* INTO lv_id
* WHERE zstu_id = p_id.
** ENDSELECT.
lo_agent = zca_per_stu=>agent.
TRY.
CALL METHOD lo_agent->get_persistent
EXPORTING
i_zstu_id = p_id
RECEIVING
result = lo_obj1.
CATCH cx_os_object_not_found.
ENDTRY.
IF lo_obj1 IS NOT INITIAL.
MESSAGE 'Record already exited' TYPE 'E'.
ENDIF.
ELSEIF p_delete = 'X'.
lo_agent = zca_per_stu=>agent.
TRY.
CALL METHOD lo_agent->get_persistent
EXPORTING
i_zstu_id = p_id
RECEIVING
result = lo_obj1.
CATCH cx_os_object_not_found.
ENDTRY.
IF lo_obj1 IS INITIAL.
MESSAGE 'Record not exited' TYPE 'E'.
ENDIF.
ELSEIF p_update = 'X'.
lo_agent = zca_per_stu=>agent.
TRY.
CALL METHOD lo_agent->get_persistent
EXPORTING
i_zstu_id = p_id
RECEIVING
result = lo_obj1.
CATCH cx_os_object_not_found.
ENDTRY.
IF lo_obj1 IS INITIAL.
MESSAGE 'Record not exited' TYPE 'E'.
ENDIF.
IF lo_obj1 IS NOT INITIAL.
TRY.
CALL METHOD lo_obj1->get_zstu_name
RECEIVING
result = lv_name.
CATCH cx_os_object_not_found.
ENDTRY.
TRY.
CALL METHOD lo_obj1->get_zstu_dep
RECEIVING
result = lv_dep.
CATCH cx_os_object_not_found.
ENDTRY.
ENDIF.
ENDIF.
AT SELECTION-SCREEN OUTPUT.
IF p_update = 'X'.
p_name = lv_name.
p_dep = lv_dep.
ENDIF.
START-OF-SELECTION.
IF p_insert = 'X'.
CLEAR:lo_agent,lo_obj1.
lo_agent = zca_per_stu=>agent.
TRY.
CALL METHOD lo_agent->create_persistent
EXPORTING
i_zstu_id = p_id
RECEIVING
result = lo_obj1.
CATCH cx_os_object_existing.
ENDTRY.
TRY.
CALL METHOD lo_obj1->set_zstu_name
EXPORTING
i_zstu_name = p_name.
CATCH cx_os_object_not_found.
ENDTRY.
TRY.
CALL METHOD lo_obj1->set_zstu_dep
EXPORTING
i_zstu_dep = p_dep.
CATCH cx_os_object_not_found.
ENDTRY.
COMMIT WORK.
MESSAGE 'Record inserted' TYPE 'S'.
ELSEIF p_update = 'X'.
lo_agent = zca_per_stu=>agent.
TRY.
CALL METHOD lo_agent->get_persistent
EXPORTING
i_zstu_id = p_id
RECEIVING
result = lo_obj1.
CATCH cx_os_object_not_found.
ENDTRY.
TRY.
CALL METHOD lo_obj1->set_zstu_name
EXPORTING
i_zstu_name = p_name.
CATCH cx_os_object_not_found.
ENDTRY.
TRY.
CALL METHOD lo_obj1->set_zstu_dep
EXPORTING
i_zstu_dep = p_dep.
CATCH cx_os_object_not_found.
ENDTRY.
COMMIT WORK.
MESSAGE 'Record updated' TYPE 'S'.
ELSEIF p_delete = 'X'.
lo_agent = zca_per_stu=>agent.
TRY.
CALL METHOD lo_agent->delete_persistent
EXPORTING
i_zstu_id = p_id.
CATCH cx_os_object_existing.
ENDTRY.
COMMIT WORK.
MESSAGE 'Record deleted' TYPE 'S'.
ENDIF.
Comments
Post a Comment