Working with Persistent Objects | 11.4
ZCA_0S_A0DRESS
and
ZCA_0S_PERS0N).
These references were then used to call the
CREATE_PERSI STENT
method generated in the agent classes. Here, notice that the
interface of the method has been enhanced to allow you to initialize value and
reference attributes upon creation.
To actually store the objects in the database, the
COMMIT WORK
statement had to be
issued. Note that the Persistence Service will not check to see if
a
persistent object
that is mapped using business keys already exists before issuing an
INSERT
state-
ment behind the scenes. Consequently, it is possible that the
COMMIT WORK
state-
ment might generate an exception of type
CX_OS_OBOECT_EXISTING.
However, because this exception is triggered in an update module, this exception
type would never be caught in a
CATCH
block such as the one shown in Listing
11.1. Therefore, it is a good idea to get into the habit of checking to see if persis-
tent objects of this type already exist using the
GET_PERSISTENT
method before
tiying to create a new persistent object with a particular business key.
11.4.2 Reading Persistent Objects Using the Query Service
If
a
persistence class is mapped using business keys, the Class Builder generates a
method called
GET_PERSISTENT
in the agent class. The signature of the importing
parameters for this method will match the semantic primary key fields defined in
the target database table. Therefore, you can call this method to retrieve a partic-
ular persistent object using a business key.
Frequently, however, you might not have the full primary key at your disposal
when you need to look up a persistent object. Moreover, if you have mapped
your persistent classes by an instance-GUID, you need another way to access the
persistent objects because it is unlikely thatyou will happen to have a hexadeci-
mal key field just lying around. You can achieve this advanced kind of lookup
behavior by using the Query Service.
As part of the ABAP Object Services framework, the Query Service allows you to
search for and retrieve one or more persistent objects via queries based on logical
expressions. These logical expressions are similar in nature to the ones thatyou are
probably accustomed to using with the SQL
SELECT
statement. However, in terms
of the Queiy Service, these expressions are encapsulated inside an object that
implements the
IF_OS_OUERV
interface.
295