Application Development 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: 

persistent classes and object references

Former Member
0 Kudos

hello,

i've got two persistent classes which are in an 1:n relationship . lets say its a car-class and a wheel-class to keep the excample simple. both classes have a GUID as their unique identifier and the wheel-class has two additional GUID-attributes - one for an object reference and one for the class reference.

now my question is: how do i actually 'link' those objects together? i want to create four wheel objects which belong to a certain car-object and then i also want to be able to search through all my wheel-objects to get those four wheel-objects which belong to a special car-object.

I couldnt find any demo-class which shows how the objects are actually set in a relationship, just how you create simple persistent objects and how they can be found with the help of sap-query

thx in advance,

dsp

1 ACCEPTED SOLUTION

naimesh_patel
Active Contributor
0 Kudos

I would suggest this:

You should wrap the Car Persistence as well as the Wheel Persistence in one model Class.

Like:

In Car Model Class, you should have one attribute which will hold the Persistent object for the Car. Alongwith this car persistent object, you shoulde create an internal table which will hold the persistent object for the Wheels. Create a method to Instantiate the Car persistent as well as the Wheel Persistent objects.

From your Outside application, create an object of this Car Model Class.

Regards,

Naimesh Patel

4 REPLIES 4

naimesh_patel
Active Contributor
0 Kudos

I would suggest this:

You should wrap the Car Persistence as well as the Wheel Persistence in one model Class.

Like:

In Car Model Class, you should have one attribute which will hold the Persistent object for the Car. Alongwith this car persistent object, you shoulde create an internal table which will hold the persistent object for the Wheels. Create a method to Instantiate the Car persistent as well as the Wheel Persistent objects.

From your Outside application, create an object of this Car Model Class.

Regards,

Naimesh Patel

0 Kudos

hi,

your suggestion sounds like a possible solution, but the book "abap advanced techniques" by galileo press somehow promises all this happens "automatically" if you create an object-idendity for the referenced objekt. but sadly they dont explain how this works.

i could easily do a workaround by just saving the guid of the car-object in an attribute of the wheel objects and then search them by this guid, but i dont think thats the way it should be done.

here is some example code:


data: car type ref to zcl_car,
      car_agent type ref to zca_car.

data: wheel type ref to zcl_wheel
      wheel_agent type ref to zca_weel.


car_agent = zca_car=>agent.
wheel_agent = zca_wheel=>agent.

car = wheel_agent->create_persistent( i_name = 'bmw' ).

wheel = wheel_agent->create_persistent( i_price = '200'
i_guid_car = car ).

commit work.

now there are two objects in my tables and the two GUID-fields of zcl_wheel are referencing the car object. really nice, just like i wanted.

but the only way to get the cars wheels is with the method

 get_persistent( guid_car = 'thecarsguid') 

is this how it is supposed to be done? i'm not sure.

Edited by: dsp on Dec 15, 2008 9:40 PM

0 Kudos

Can you please provide the link of the book which you are referring? I am not able to find it out.

Regards,

Naimesh Patel

0 Kudos

Hi dsp,

yes you should create an attribute of the wheel class that contains the GUID of the car class.

The example you gave...

car = wheel_agent->create_persistent( i_name = 'bmw' ).
 
wheel = wheel_agent->create_persistent( i_price = '200'
i_guid_car = car ).

...is not valid because the reference "car" points to an instance of the car class, which is transient, and not to any of it's persistent attributes.

The suggestion to wrap the two persistent classes inside a business object class is a pretty standard way of abstracting the persistence layer and a good idea. It does not however "automatically" help you establish and maintain relationships between persistent objects.

If you are into reading books, try "[Next Generation ABAP Development|http://www.sappress.com/product.cfm?account=&product=H1986]]" by Thomas Jung and Rich Heilman.

Cheers

Graham Robbo