Managed RAP Part 1: Tables, CDS Data Model and Your First Behavior Definition
Free sample lesson · open to everyone
Build the foundation of a managed RAP business object step by step: persistence tables, a root/child CDS data model, and a managed behavior definition — and understand exactly what the framework does for you.
In this lessonShow sectionsHide sections
The situation: a real project scenario
A team migrating a custom requisition transaction to RAP built an "unmanaged-style" managed BO: every handler method issued its own INSERT or UPDATE on the custom table, and one method called COMMIT WORK. Users saw half-saved requests when a later validation failed, and created/changed timestamps were inconsistent because some code paths forgot to set them.
The data mapped cleanly to two custom tables, so the fix was to let the framework do its job: declare persistent table and mapping in the BDEF, annotate the admin fields with @Semantics, delete the hand-written CRUD and the COMMIT WORK, and keep only real business logic (numbering, validations, the approval action). About 60% of the behavior class disappeared, and the half-save problem stopped because nothing reached the database until every validation passed.
Explanation
Across this five-part series you build one complete business object: a Purchase Request with a header and items.
Each part adds one layer to the same object, so the objects created in earlier parts carry forward: Part 1 covers tables, the CDS data model, and the first behavior definition, where CRUD works with zero code; Part 2 adds numbering, locking, ETag concurrency and authorization; Part 3 covers determinations, validations, actions and feature control for operations like Submit, Approve and Reject; Part 4 adds composition (header plus items), create-by-association and draft; and Part 5 covers the projection layer, testing, performance and clean-core rules.
After Part 5, the Service Bindings series continues the story by exposing the same object as a Fiori Elements app.
Managed RAP sits within a layered structure for building a transactional app. Database tables are where the data is stored. The CDS data model, built from view entities, gives the data its business shape: a root entity, child entities, and the relationships between them.
The behavior definition (BDEF) states what may happen to the data — create, update, delete, actions, validations, locks. The behavior implementation, an ABAP class called the behavior pool, holds the code for anything that isn't standard. Finally, the projection, service definition and service binding determine what gets exposed to a UI or an API.
"Managed" is a decision about the behavior-definition and behavior-implementation layers: it means the RAP framework itself implements the standard transactional work, and you only write code for business-specific logic.
The framework owns the standard create, update and delete operations; the transactional buffer, which holds changes in memory until save; locking; ETag comparison; filling in administrative fields like created-by/at and changed-by/at; writing to the database and committing; and the draft tables when draft is enabled. You own the data model — tables, keys, CDS — the behavior definition, the numbering strategy if UUIDs aren't used, the determinations, validations and actions, authorization and feature control, and the messages.
Every RAP request moves through two phases. In the interaction phase, the consumer sends create, update or action requests; RAP runs the standard operations and calls your handler methods, and every change lands in the transactional buffer without anything being written to the database yet.
In the save sequence, triggered when the consumer commits — a user clicking Save, or code calling COMMIT ENTITIES — RAP runs the on-save determinations, then the validations, and only then writes to the database and commits; if a validation fails, nothing is written.
This is exactly why a managed behavior class must never contain COMMIT WORK, ROLLBACK WORK, or direct INSERT/UPDATE statements on its own tables — the framework owns the transaction, the LUW.
The first step is to create the persistence tables. In ADT, create two database tables, ZPR_HEAD for the request header and ZPR_ITEM for its items, using UUID keys of type sysuuid_x16, since managed RAP can generate these itself. Add the administrative fields using the standard data elements shown in the code example — abp_creation_user, abp_creation_tstmpl, abp_locinst_lastchange_user, abp_locinst_lastchange_tstmpl, abp_lastchange_tstmpl. ZPR_ITEM carries parent_uuid as the foreign key back to its header.
The second step is to create the CDS data model. ZR_PurchReq is the root view entity, declared with "define root view entity", and it declares "composition [0..*] of ZR_PurchReqItem as _Item", meaning items belong to the request and cannot exist without it; ZR_PurchReqItem in turn declares "association to parent ZR_PurchReq as _Request".
Columns get renamed to readable CamelCase names, which become the business object's field names and later the OData property names. The @Semantics annotations on the admin fields tell RAP which field means "created by", "last changed at" and so on, so that managed RAP can fill them in automatically on every change.
The third step is to create the behavior definition.
Right-click ZR_PurchReq and choose New Behavior Definition with implementation type Managed, then work through each line: "managed implementation in class zbp_r_purchreq unique" names the behavior pool that will hold the code; "strict ( 2 )" turns on the strictest syntax checks, which should always be used for new development; "persistent table zpr_head" tells the framework where to write; "lock master" together with "lock dependent by _Request" locks the root and has items locked through it; "authorization master ( global, instance )" checks authorization on the root and has it inherited by items — strict mode requires this to be declared, and it's implemented in Part 2; "etag master LocalLastChangedAt" sets up optimistic concurrency, also covered in Part 2; "create; update; delete;" together with "association _Item { create; }" state the allowed operations, with items only creatable through their parent; "field ( readonly, numbering : managed ) RequestUUID" has RAP generate the UUID key on create; and "mapping for zpr_head" maps CDS names like RequestUUID to table columns like request_uuid, which the framework needs in order to write CamelCase fields into snake_case columns.
The fourth step is to generate the behavior pool, using the ADT quick fix (Ctrl+1) on the class name to generate ZBP_R_PURCHREQ, which contains a local handler class (lhc_…) with empty authorization methods; for Part 1, no further code is needed — create, update and delete already work.
The fifth step is to test without a UI: create a class implementing if_oo_adt_classrun and run the EML snippet from the end of the code example with F9. EML (Entity Manipulation Language) is how ABAP code talks to a RAP business object; outside the business object, COMMIT ENTITIES is called to trigger the save sequence, but inside handler methods it never is.
After Part 1 you have a working business object that creates, changes and deletes purchase requests and items, with keys and admin fields maintained automatically, using zero lines of CRUD code. Part 2 adds a readable request number, locking behavior, concurrency control and authorization.
Animated technical walkthrough
Chapter-specific · Managed RAP · Part 1What managed RAP really saves
Stage 1 of 4
Connect the persistent data model
The root CDS entity describes the BO data; the table holds active persisted records.
Example objects are conceptual; framework-managed persistence requires a valid behavior model and mapping. This is a teaching model, not an actual SAP transaction trace.
Code example
// ===== 1. Table ZPR_HEAD (header) =====@EndUserText.label : 'Purchase request header'@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE // structure is closed to customer enhancement@AbapCatalog.tableCategory : #TRANSPARENT // plain DB table (as opposed to cluster/pooled)@AbapCatalog.deliveryClass : #A // application table: customer data, not SAP-delivered content@AbapCatalog.dataMaintenance : #RESTRICTED // no generic SM30 maintenance - only via the RAP BOdefine table zpr_head { key client : abap.clnt not null; // MANDT - every table needs it for client isolation key request_uuid : sysuuid_x16 not null; // UUID key lets records be created before a human-readable number exists request_id : abap.numc(10); description : abap.char(80); requester : syuname; status : abap.char(1); @Semantics.amount.currencyCode : 'zpr_head.currency_code' // links the amount to its currency field so UIs format/convert correctly total_amount : abap.curr(15,2); currency_code : abap.cuky; created_by : abp_creation_user; created_at : abp_creation_tstmpl; last_changed_by : abp_locinst_lastchange_user; local_last_changed_at : abp_locinst_lastchange_tstmpl; // per-instance timestamp, used later as the etag master field last_changed_at : abp_lastchange_tstmpl; // overall timestamp, used later as the total etag field} // ===== 2. Table ZPR_ITEM (items) =====define table zpr_item { key client : abap.clnt not null; key item_uuid : sysuuid_x16 not null; parent_uuid : sysuuid_x16; // foreign key back to the header - no DB foreign key, the composition in the CDS view enforces the relation item_no : abap.numc(4); item_text : abap.char(60); @Semantics.quantity.unitOfMeasure : 'zpr_item.unit' // links the quantity to its unit field quantity : abap.quan(13,3); unit : abap.unit(3); @Semantics.amount.currencyCode : 'zpr_item.currency_code' price : abap.curr(15,2); currency_code : abap.cuky; local_last_changed_at : abp_locinst_lastchange_tstmpl;} // ===== 3. Root view entity ZR_PurchReq =====@AccessControl.authorizationCheck: #NOT_REQUIRED // tutorial only - use #CHECK + a DCL role in real projects@EndUserText.label: 'Purchase Request'define root view entity ZR_PurchReq as select from zpr_head composition [0..*] of ZR_PurchReqItem as _Item // header/item (parent/child) relationship - items are created, updated and deleted together with the header{ key request_uuid as RequestUUID, request_id as RequestID, description as Description, requester as Requester, status as Status, @Semantics.amount.currencyCode: 'CurrencyCode' total_amount as TotalAmount, currency_code as CurrencyCode, @Semantics.user.createdBy: true // framework fills this automatically on create created_by as CreatedBy, @Semantics.systemDateTime.createdAt: true // framework fills this automatically on create created_at as CreatedAt, @Semantics.user.localInstanceLastChangedBy: true last_changed_by as LastChangedBy, @Semantics.systemDateTime.localInstanceLastChangedAt: true // candidate for "etag master" in the behavior definition local_last_changed_at as LocalLastChangedAt, @Semantics.systemDateTime.lastChangedAt: true // candidate for "total etag" in the behavior definition last_changed_at as LastChangedAt, _Item} // ===== 4. Child view entity ZR_PurchReqItem =====@AccessControl.authorizationCheck: #NOT_REQUIRED@EndUserText.label: 'Purchase Request Item'define view entity ZR_PurchReqItem as select from zpr_item association to parent ZR_PurchReq as _Request on $projection.RequestUUID = _Request.RequestUUID{ key item_uuid as ItemUUID, parent_uuid as RequestUUID, item_no as ItemNo, item_text as ItemText, @Semantics.quantity.unitOfMeasure: 'Unit' quantity as Quantity, unit as Unit, @Semantics.amount.currencyCode: 'CurrencyCode' price as Price, currency_code as CurrencyCode, @Semantics.systemDateTime.localInstanceLastChangedAt: true local_last_changed_at as LocalLastChangedAt, _Request} // ===== 5. Behavior definition ZR_PurchReq =====managed implementation in class zbp_r_purchreq unique; // managed: framework does the DB access; unique: no duplicate key within one transactional bufferstrict ( 2 ); // enforces API consistency rules required by Fiori elements and newer RAP features define behavior for ZR_PurchReq alias PurchaseRequestpersistent table zpr_headlock master // exclusive lock on the root while someone is editingauthorization master ( global, instance ) // both create-level (global) and per-instance checks are implementedetag master LocalLastChangedAt // used to detect "someone else already changed this since you read it"{ create; update; delete; association _Item { create; } field ( readonly, numbering : managed ) RequestUUID; // key is generated by the framework, never set by the caller field ( readonly ) RequestID, Status, TotalAmount, CreatedBy, CreatedAt, LastChangedBy, LocalLastChangedAt, LastChangedAt; mapping for zpr_head // CDS element name (left) = DB table column (right) { RequestUUID = request_uuid; RequestID = request_id; Description = description; Requester = requester; Status = status; TotalAmount = total_amount; CurrencyCode = currency_code; CreatedBy = created_by; CreatedAt = created_at; LastChangedBy = last_changed_by; LocalLastChangedAt = local_last_changed_at; LastChangedAt = last_changed_at; }} define behavior for ZR_PurchReqItem alias Itempersistent table zpr_itemlock dependent by _Request // items share the header's lock - no separate lock neededauthorization dependent by _Request // items inherit the header's authorization decisionetag master LocalLastChangedAt{ update; delete; association _Request; field ( readonly, numbering : managed ) ItemUUID; field ( readonly ) RequestUUID, LocalLastChangedAt; mapping for zpr_item { ItemUUID = item_uuid; RequestUUID = parent_uuid; ItemNo = item_no; ItemText = item_text; Quantity = quantity; Unit = unit; Price = price; CurrencyCode = currency_code; LocalLastChangedAt = local_last_changed_at; }} " ===== 6. Test it with EML (class implementing if_oo_adt_classrun, run with F9) =====MODIFY ENTITIES OF zr_purchreq ENTITY PurchaseRequest CREATE FIELDS ( Description CurrencyCode ) WITH VALUE #( ( %cid = 'REQ1' Description = 'Laptops for new joiners' CurrencyCode = 'INR' ) ) MAPPED DATA(mapped) FAILED DATA(failed) REPORTED DATA(reported). COMMIT ENTITIES. " allowed here (outside the BO) - never inside a handler methodout->write( mapped-purchaserequest ).Watch out
Common mistakes
- Writing INSERT/UPDATE/MODIFY statements on the persistent table inside handler methods — the framework already does this at save time, and doing it yourself bypasses the buffer and validations.
- Calling COMMIT WORK or ROLLBACK WORK in a behavior class. The framework owns the LUW; a manual commit can persist half-validated data.
- Forgetting the mapping block when CDS names differ from column names, then wondering why fields are not saved.
- Declaring the child entity as a normal association instead of "composition" / "association to parent" — then it is not part of the business object and cannot share locks, ETags or draft.
- Omitting the @Semantics annotations on admin fields, so CreatedBy/LastChangedAt stay empty.
- Leaving strict ( 2 ) out, which hides syntax problems that surface later during upgrades.
Do this
Best practices
- Start every new table-based business object as managed; move to unmanaged or unmanaged save only for a proven persistence reason (see the Unmanaged RAP series).
- Use UUID technical keys with numbering : managed, plus a separate human-readable number (Part 2).
- Always use strict ( 2 ) and the standard abp_* data elements for administrative fields.
- Name layers consistently: ZR_ for the base business object views, ZC_ for projections (Part 5), ZBP_ for behavior pools.
- Keep persistence tables free of UI-only fields; UI concerns belong in the projection layer.
- Test the BO with EML in a console class before building any UI — it isolates backend problems from UI problems.
In the interview
Interview angle
Typical questions: "What is the difference between managed and unmanaged RAP?", "What does the framework do for you in managed RAP?", "Why can't you use COMMIT WORK in a behavior implementation?"
A strong answer: in managed RAP the framework implements standard CRUD, the transactional buffer, locking, ETag handling, admin fields and the save sequence; the developer declares the contract in the BDEF and implements only business logic. Explain the two phases (interaction phase into the buffer, save sequence to the database) and that the framework owns the LUW, so a manual commit would break atomicity. Senior candidates add when they would not choose managed: when an existing API must own persistence, which leads to unmanaged or managed with unmanaged save.
Practice this lesson
Quick check
0 of 3 questions attempted
Question 1 of 3
In managed RAP, where are create and update changes held during the interaction phase, before the user saves?
Question 2 of 3
What is the purpose of the "mapping for zpr_head" block in a managed behavior definition?
Question 3 of 3
Which declaration lets the managed RAP framework generate a UUID key automatically when an instance is created?
Practise this in an interview
Answer in your own words and get instant feedback, the way an interviewer would judge it.
1Walk me through the layers of a RAP application, from the database table to the Fiori app. What is the job of each layer?
2In managed RAP, what exactly does the framework do for you, and why must a behavior implementation never contain COMMIT WORK or direct INSERT/UPDATE statements on its own tables?
Try a related Code Lab
This published exercise covers the same SAP topic. Apply the ideas in a hands-on scenario, then return to review your answers.
Open: Reading a Key Before Early Numbering Has Set It →See it in code
Each example starts from a requirement, shows the design, then the code step by step with a plain-language explanation of every part.
- Build a managed RAP business object: Leave Request with approve action
- RAP additional save: write an audit log row on every create, update and delete
- Clean core: read products with the released CDS views I_Product and I_ProductDescription
- AMDP and a CDS table function: aggregate orders in SQLScript on HANA
Was this lesson helpful?
Keep exploring
Interview questions on this topic
Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.