12-15-2008 6:55 PM
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
12-15-2008 7:07 PM
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
12-15-2008 7:07 PM
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
12-15-2008 7:39 PM
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
12-15-2008 8:59 PM
Can you please provide the link of the book which you are referring? I am not able to find it out.
Regards,
Naimesh Patel
12-15-2008 10:02 PM
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