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

Reg : Persistence class

Former Member
0 Likes
1,081

Hello All,

I have created a persistence class where there are few key fields.

When I am selecting the fields for class attribute from table fields,

I observed that key fields are only for read only.

So, for key fields we have only GET_ method.

We can update a records, it it already exist in the data base.

But how can we insert a new record If we have only a read access for key fields.

Please let me know if there is any other way for doing it.

Thank you in adavcne.

With regards,

Sandeep Akella

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
886

Have you used CREATE_PERSISTENT method of Class Actor ? Read Generating a Persistent Object - ABAP - Object Services - SAP Library  for reference.

Regards, Vinod

6 REPLIES 6
Read only

Former Member
0 Likes
887

Have you used CREATE_PERSISTENT method of Class Actor ? Read Generating a Persistent Object - ABAP - Object Services - SAP Library  for reference.

Regards, Vinod

Read only

0 Likes
886

Hello Vinod,

Thank you for your reply.

But how can I update multiple records into data base like for example

header data  1 record

item data     10- records

all these have do be updated or none of them should e updated,

Do we need to use the concept fo end_and_chain method for this.

Can you please give me inputs on this.

with regards,

Sandeep Akella

Read only

0 Likes
886

Hello Sandeep,

end_and_chain - Is to be used when the same persistent object is used in the next transaction after the completion of the current one.  Persistent objects are set to a not loaded state (OSCON_OSTATUS_NOT_LOADED), once a transaction is completed. The above method however ensures that they are not set to an intiial and not loaded state at the end of the transaction, thereby saving reloading from the DB again; hence a perofrmance gain.

For your scenario, You could combine one header and its items logically as a single transaction, or you could create a parent transaction with the header and the items as subtransaction. In the latter case you would have to end the item transaction first , followed by the header transaction. I guess you could use a single transaction for both , especially for ease and simplicity.

Hope this helps.

Thanks,

Venkat. 

Message was edited by: Venkat Gowrishankar

Read only

0 Likes
886

Hello Venkat,

Thank you for your reply.

in object oriented mode the first called transaction will be parent transaction.

Then how to call the sub transaction for this.

How can I insert multiple record in same transaction.

   Can you please give me a code snippet so that it will be easy for me to understand.

With regards,

Sandeep Akella

Read only

0 Likes
886

Hello Sandeep,

Once you start a transaction, all other transactions that are started before the completion of the parent transaction are treated as sub transactions.

Here is a code snippet. My test table has only one field 'COUNTER'.

TYPE-POOLS  : oscon.

  DATA : lr_actor         TYPE REF TO zca_ztest_table,
              lr_pers           TYPE REF TO zcl_ztest_table,
              lr_tr_mgr        TYPE REF TO if_os_transaction_manager,
              lr_trans           TYPE REF TO if_os_transaction,
              lr_trans_sub   TYPE REF TO if_os_transaction.

INITIALIZATION.
cl_os_system=>init_and_set_modes( i_external_commit = oscon_false ).

START-OF-SELECTION.
  lr_tr_mgr = cl_os_system=>get_transaction_manager( ).
  lr_actor  = zca_ztest_table=>agent.

  TRY.
*Parent transaction.
      lr_trans = lr_tr_mgr->create_transaction( ).
      lr_trans->start( ).


      CALL METHOD lr_actor->create_persistent
        EXPORTING
          i_counter = '16' " Dummy value.
        RECEIVING
          result    = lr_pers.

*Sub transaction

      lr_trans_sub = lr_tr_mgr->create_transaction( ).
      lr_trans_sub->start( ).


      CALL METHOD lr_actor->create_persistent
        EXPORTING
          i_counter = '17'
        RECEIVING
          result    = lr_pers.

*End of sub transaction.
      lr_trans_sub->end( ).

*End of Parent transaction.
      lr_trans->end( ).

    CATCH cx_os_object_existing .

*Error handling...

     WRITE: 'Error Occured in creating object'.
  ENDTRY.

More info on the transaction service could be find in the below URL.

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

Hope this helps.

Thanks,

Venkat.

Message was edited by: Venkat Gowrishankar

Read only

manish_shankar
Participant
0 Likes
886

Hi Sandeep,

For each persistent class ZCL_classname, Class builder generates two further classes, ZCA_classname : Actor Class and ZCB_classname : Agent Class.

For each (mapped) fields Getter method <GET_FIELDNAME> will be generated. Since you cant change the value of key fields, so for each (mapped)  non-key fields Setter method <SET_FIELDNAME> will be generated.

To insert a new record you have to use attribute <AGENT> of Agent Persistent Class ZCA_classname, and call the method CREATE_PERSISTENT.

DATA : PERS TYPE REF TO  ZCL_classname .
    CALL METHOD ZCA_
classname=>AGENT->CREATE_PERSISTENT
           EXPORTING
             I_keyfield    = P_keyfield
           RECEIVING
             RESULT     = PERS .

It will create key field. For other fields set the values using setter method.

   CALL METHOD PERS->SET_EMPNAME
           EXPORTING
             I_nonkey = P_nonkey.

Hope it'll help you.

Thanks,

Manish Shankar.